Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9b11d5
commit 57f0a04
Showing
39 changed files
with
6,447 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export declare function argsert(callerArguments: any[], length?: number): void; | ||
export declare function argsert(expected: string, callerArguments: any[], length?: number): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { YError } from './yerror.js'; | ||
import { parseCommand } from './parse-command.js'; | ||
const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']; | ||
export function argsert(arg1, arg2, arg3) { | ||
function parseArgs() { | ||
return typeof arg1 === 'object' | ||
? [{ demanded: [], optional: [] }, arg1, arg2] | ||
: [ | ||
parseCommand(`cmd ${arg1}`), | ||
arg2, | ||
arg3, | ||
]; | ||
} | ||
try { | ||
let position = 0; | ||
const [parsed, callerArguments, _length] = parseArgs(); | ||
const args = [].slice.call(callerArguments); | ||
while (args.length && args[args.length - 1] === undefined) | ||
args.pop(); | ||
const length = _length || args.length; | ||
if (length < parsed.demanded.length) { | ||
throw new YError(`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`); | ||
} | ||
const totalCommands = parsed.demanded.length + parsed.optional.length; | ||
if (length > totalCommands) { | ||
throw new YError(`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`); | ||
} | ||
parsed.demanded.forEach(demanded => { | ||
const arg = args.shift(); | ||
const observedType = guessType(arg); | ||
const matchingTypes = demanded.cmd.filter(type => type === observedType || type === '*'); | ||
if (matchingTypes.length === 0) | ||
argumentTypeError(observedType, demanded.cmd, position); | ||
position += 1; | ||
}); | ||
parsed.optional.forEach(optional => { | ||
if (args.length === 0) | ||
return; | ||
const arg = args.shift(); | ||
const observedType = guessType(arg); | ||
const matchingTypes = optional.cmd.filter(type => type === observedType || type === '*'); | ||
if (matchingTypes.length === 0) | ||
argumentTypeError(observedType, optional.cmd, position); | ||
position += 1; | ||
}); | ||
} | ||
catch (err) { | ||
console.warn(err.stack); | ||
} | ||
} | ||
function guessType(arg) { | ||
if (Array.isArray(arg)) { | ||
return 'array'; | ||
} | ||
else if (arg === null) { | ||
return 'null'; | ||
} | ||
return typeof arg; | ||
} | ||
function argumentTypeError(observedType, allowedTypes, position) { | ||
throw new YError(`Invalid ${positionName[position] || 'manyith'} argument. Expected ${allowedTypes.join(' or ')} but received ${observedType}.`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Dictionary, RequireDirectoryOptions, PlatformShim } from './typings/common-types.js'; | ||
import { Middleware } from './middleware.js'; | ||
import { Positional } from './parse-command.js'; | ||
import { UsageInstance } from './usage.js'; | ||
import { ValidationInstance } from './validation.js'; | ||
import { YargsInstance, Options, OptionDefinition, Context, Arguments, DetailedArguments } from './yargs-factory.js'; | ||
export declare type DefinitionOrCommandName = string | CommandHandlerDefinition; | ||
export declare function command(yargs: YargsInstance, usage: UsageInstance, validation: ValidationInstance, globalMiddleware: Middleware[] | undefined, shim: PlatformShim): CommandInstance; | ||
export interface CommandInstance { | ||
addDirectory(dir: string, context: Context, req: Function, callerFile: string, opts?: RequireDirectoryOptions): void; | ||
addHandler(cmd: string | CommandHandlerDefinition | DefinitionOrCommandName[], description?: CommandHandler['description'], builder?: CommandBuilderDefinition | CommandBuilder, handler?: CommandHandlerCallback, commandMiddleware?: Middleware[], deprecated?: boolean): void; | ||
cmdToParseOptions(cmdString: string): Positionals; | ||
freeze(): void; | ||
getCommandHandlers(): Dictionary<CommandHandler>; | ||
getCommands(): string[]; | ||
hasDefaultCommand(): boolean; | ||
reset(): CommandInstance; | ||
runCommand(command: string | null, yargs: YargsInstance, parsed: DetailedArguments, commandIndex?: number): Arguments | Promise<Arguments>; | ||
runDefaultBuilderOn(yargs: YargsInstance): void; | ||
unfreeze(): void; | ||
} | ||
export interface CommandHandlerDefinition extends Partial<Pick<CommandHandler, 'deprecated' | 'description' | 'handler' | 'middlewares'>> { | ||
aliases?: string[]; | ||
builder?: CommandBuilder | CommandBuilderDefinition; | ||
command?: string | string[]; | ||
desc?: CommandHandler['description']; | ||
describe?: CommandHandler['description']; | ||
} | ||
export interface CommandBuilderDefinition { | ||
builder?: CommandBuilder; | ||
deprecated?: boolean; | ||
handler: CommandHandlerCallback; | ||
middlewares?: Middleware[]; | ||
} | ||
export declare function isCommandBuilderDefinition(builder?: CommandBuilder | CommandBuilderDefinition): builder is CommandBuilderDefinition; | ||
export interface CommandHandlerCallback { | ||
(argv: Arguments): any; | ||
} | ||
export interface CommandHandler { | ||
builder: CommandBuilder; | ||
demanded: Positional[]; | ||
deprecated?: boolean; | ||
description?: string | false; | ||
handler: CommandHandlerCallback; | ||
middlewares: Middleware[]; | ||
optional: Positional[]; | ||
original: string; | ||
} | ||
export declare type CommandBuilder = CommandBuilderCallback | Dictionary<OptionDefinition>; | ||
interface CommandBuilderCallback { | ||
(y: YargsInstance): YargsInstance | void; | ||
} | ||
export declare function isCommandBuilderCallback(builder: CommandBuilder): builder is CommandBuilderCallback; | ||
export declare function isCommandHandlerDefinition(cmd: DefinitionOrCommandName | [DefinitionOrCommandName, ...string[]]): cmd is CommandHandlerDefinition; | ||
interface Positionals extends Pick<Options, 'alias' | 'array' | 'default'> { | ||
demand: Dictionary<boolean>; | ||
} | ||
export interface FinishCommandHandler { | ||
(handlerResult: any): any; | ||
} | ||
export {}; |
Oops, something went wrong.