forked from yargs/yargs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release `v17.7.2` with multiple fixes and improvements for robustness and use with Deno.
- Loading branch information
Showing
42 changed files
with
4,220 additions
and
4,220 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export declare function argsert(callerArguments: any[], length?: number): void; | ||
export declare function argsert(expected: string, callerArguments: any[], length?: number): void; | ||
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 |
---|---|---|
@@ -1,62 +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}.`); | ||
} | ||
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 |
---|---|---|
@@ -1,87 +1,87 @@ | ||
import { Dictionary, RequireDirectoryOptions, PlatformShim } from './typings/common-types.js'; | ||
import { GlobalMiddleware, Middleware } from './middleware.js'; | ||
import { Positional } from './parse-command.js'; | ||
import { UsageInstance } from './usage.js'; | ||
import { ValidationInstance } from './validation.js'; | ||
import { YargsInstance, Options, OptionDefinition, Arguments, DetailedArguments } from './yargs-factory.js'; | ||
export type DefinitionOrCommandName = string | CommandHandlerDefinition; | ||
export declare class CommandInstance { | ||
shim: PlatformShim; | ||
requireCache: Set<string>; | ||
handlers: Dictionary<CommandHandler>; | ||
aliasMap: Dictionary<string>; | ||
defaultCommand?: CommandHandler; | ||
usage: UsageInstance; | ||
globalMiddleware: GlobalMiddleware; | ||
validation: ValidationInstance; | ||
frozens: FrozenCommandInstance[]; | ||
constructor(usage: UsageInstance, validation: ValidationInstance, globalMiddleware: GlobalMiddleware, shim: PlatformShim); | ||
addDirectory(dir: string, 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; | ||
getCommandHandlers(): Dictionary<CommandHandler>; | ||
getCommands(): string[]; | ||
hasDefaultCommand(): boolean; | ||
runCommand(command: string | null, yargs: YargsInstance, parsed: DetailedArguments, commandIndex: number, helpOnly: boolean, helpOrVersionSet: boolean): Arguments | Promise<Arguments>; | ||
private applyBuilderUpdateUsageAndParse; | ||
private parseAndUpdateUsage; | ||
private shouldUpdateUsage; | ||
private usageFromParentCommandsCommandHandler; | ||
private handleValidationAndGetResult; | ||
private applyMiddlewareAndGetResult; | ||
private populatePositionals; | ||
private populatePositional; | ||
cmdToParseOptions(cmdString: string): Positionals; | ||
private postProcessPositionals; | ||
isDefaulted(yargs: YargsInstance, key: string): boolean; | ||
isInConfigs(yargs: YargsInstance, key: string): boolean; | ||
runDefaultBuilderOn(yargs: YargsInstance): unknown | Promise<unknown>; | ||
private moduleName; | ||
private commandFromFilename; | ||
private extractDesc; | ||
freeze(): void; | ||
unfreeze(): void; | ||
reset(): CommandInstance; | ||
} | ||
export declare function command(usage: UsageInstance, validation: ValidationInstance, globalMiddleware: GlobalMiddleware, shim: PlatformShim): CommandInstance; | ||
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 type CommandBuilder = CommandBuilderCallback | Dictionary<OptionDefinition>; | ||
interface CommandBuilderCallback { | ||
(y: YargsInstance, helpOrVersionSet: boolean): 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>; | ||
} | ||
type FrozenCommandInstance = { | ||
handlers: Dictionary<CommandHandler>; | ||
aliasMap: Dictionary<string>; | ||
defaultCommand: CommandHandler | undefined; | ||
}; | ||
export {}; | ||
import { Dictionary, RequireDirectoryOptions, PlatformShim } from './typings/common-types.js'; | ||
import { GlobalMiddleware, Middleware } from './middleware.js'; | ||
import { Positional } from './parse-command.js'; | ||
import { UsageInstance } from './usage.js'; | ||
import { ValidationInstance } from './validation.js'; | ||
import { YargsInstance, Options, OptionDefinition, Arguments, DetailedArguments } from './yargs-factory.js'; | ||
export declare type DefinitionOrCommandName = string | CommandHandlerDefinition; | ||
export declare class CommandInstance { | ||
shim: PlatformShim; | ||
requireCache: Set<string>; | ||
handlers: Dictionary<CommandHandler>; | ||
aliasMap: Dictionary<string>; | ||
defaultCommand?: CommandHandler; | ||
usage: UsageInstance; | ||
globalMiddleware: GlobalMiddleware; | ||
validation: ValidationInstance; | ||
frozens: FrozenCommandInstance[]; | ||
constructor(usage: UsageInstance, validation: ValidationInstance, globalMiddleware: GlobalMiddleware, shim: PlatformShim); | ||
addDirectory(dir: string, 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; | ||
getCommandHandlers(): Dictionary<CommandHandler>; | ||
getCommands(): string[]; | ||
hasDefaultCommand(): boolean; | ||
runCommand(command: string | null, yargs: YargsInstance, parsed: DetailedArguments, commandIndex: number, helpOnly: boolean, helpOrVersionSet: boolean): Arguments | Promise<Arguments>; | ||
private applyBuilderUpdateUsageAndParse; | ||
private parseAndUpdateUsage; | ||
private shouldUpdateUsage; | ||
private usageFromParentCommandsCommandHandler; | ||
private handleValidationAndGetResult; | ||
private applyMiddlewareAndGetResult; | ||
private populatePositionals; | ||
private populatePositional; | ||
cmdToParseOptions(cmdString: string): Positionals; | ||
private postProcessPositionals; | ||
isDefaulted(yargs: YargsInstance, key: string): boolean; | ||
isInConfigs(yargs: YargsInstance, key: string): boolean; | ||
runDefaultBuilderOn(yargs: YargsInstance): unknown | Promise<unknown>; | ||
private moduleName; | ||
private commandFromFilename; | ||
private extractDesc; | ||
freeze(): void; | ||
unfreeze(): void; | ||
reset(): CommandInstance; | ||
} | ||
export declare function command(usage: UsageInstance, validation: ValidationInstance, globalMiddleware: GlobalMiddleware, shim: PlatformShim): CommandInstance; | ||
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, helpOrVersionSet: boolean): 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>; | ||
} | ||
declare type FrozenCommandInstance = { | ||
handlers: Dictionary<CommandHandler>; | ||
aliasMap: Dictionary<string>; | ||
defaultCommand: CommandHandler | undefined; | ||
}; | ||
export {}; |
Oops, something went wrong.