Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/opinecli/deps.ts>Command

Opine's application generator
Latest
class Command
Re-export
import { Command } from "https://deno.land/x/opinecli@2.0.0/deps.ts";

Type Parameters

optional
CO extends Record<string, any> | void = any
optional
CA extends Array<unknown> = CO extends number ? any : []
optional
CG extends Record<string, any> | void = CO extends number ? any : void
optional
PG extends Record<string, any> | void = CO extends number ? any : void
optional
P extends Command | undefined = CO extends number ? any : undefined

Properties

private
_allowEmpty: boolean
private
optional
_globalParent: Command
private
optional
_help: IHelpHandler
private
optional
_helpOption: IDefaultOption | false
private
_name: string
private
optional
_parent: P
private
optional
_shouldExit: boolean
private
_stopEarly: boolean
private
optional
_usage: string
private
_useRawArgs: boolean
private
optional
_versionOption: IDefaultOption | false
private
aliases: string[]
private
args: IArgument[]
private
optional
argsDefinition: string
private
cmd: Command
private
commands: Map<string, Command>
private
completions: Map<string, ICompletion>
private
optional
defaultCommand: string
private
desc: IDescription
private
envVars: IEnvVar[]
private
examples: IExample[]
private
optional
fn: IAction
private
hasDefaults: boolean
private
isExecutable: boolean
private
isGlobal: boolean
private
isHidden: boolean
private
literalArgs: string[]
private
options: IOption[]
private
rawArgs: string[]
private
throwOnError: boolean
private
types: Map<string, IType>
private
optional
ver: IVersionHandler

Methods

private
exit(code?)
private
getHelpHandler(): IHelpHandler

Get help handler method.

private
getVersionHandler(): IVersionHandler | undefined

Get help handler method.

private
registerDefaults(): this

Register default options like --version and --help.

protected
error(error: Error): Error

Handle error. If throwErrors is enabled the error will be returned, otherwise a formatted error message will be printed and Deno.exit(1) will be called.

protected
execute(options: PG & CG & CO, ...args: CA): Promise<IParseResult<CO, CA, CG, PG, P>>

Execute command.

protected
executeExecutable(args: string[])

Execute external sub-command.

protected
parseArguments(args: string[], flags: Record<string, unknown>): CA

Parse command-line arguments.

protected
parseEnvVars(): Promise<Record<string, unknown>>

Validate environment variables.

protected
parseFlags(args: string[]): IFlagsResult & { actionOption?: IOption & { action: IAction; }; }

Parse raw command line arguments.

protected
parseType(type: ITypeInfo): unknown

Parse argument type.

protected
shouldExit(): boolean

Check whether the command should exit after printing help or version.

protected
shouldThrowErrors(): boolean

Check whether the command should throw errors or exit.

action(fn: IAction<CO, CA, CG, PG, P>): this

Set command callback method.

alias(alias: string): this

Add new command alias.

allowEmpty(allowEmpty?): this

Don't throw an error if the command was called without arguments.

arguments<A extends Array<unknown> = CA>(args: string): Command<CO, MapArgumentTypes<A>, CG, PG, P>

Set command arguments:

requiredArg:string [optionalArg: number] [...restArgs:string]

command<C extends (CO extends number ? Command : Command<Record<string, any> | void, Array<unknown>, Record<string, any> | void, Merge<PG, CG> | void | undefined, OneOf<P, this> | undefined>)>(
name: string,
cmd: C,
override?: boolean,
): C extends Command<infer O, infer A, infer G, any, any> ? Command<O, A, G, Merge<PG, CG>, OneOf<P, this>> : never

Add new sub-command.

command<A extends Array<unknown> = Array<any>>(
name: string,
desc?: string,
override?: boolean,
): Command<CO extends number ? any : void, MapArgumentTypes<A>, CO extends number ? any : void, Merge<PG, CG>, OneOf<P, this>>

Add new sub-command.

complete(
name: string,
complete: ICompleteHandler<Partial<CO>, Partial<CA>, CG, PG, any>,
options: ICompleteOptions & { global: boolean; },
): this

Register command specific custom type.

complete(
name: string,
complete: ICompleteHandler<CO, CA, CG, PG, P>,
options?: ICompleteOptions,
): this
default(name: string): this

Set default command. The default command is executed when the program was called without any argument and if no action handler is registered.

description(description: IDescription<CO, CA, CG, PG, P>): this

Set the long command description.

env<G extends Record<string, unknown> | void = CG>(
name: string,
description: string,
options?: IEnvVarOptions,
): Command<CO, CA, Merge<CG, MapOptionTypes<G>>, PG, P>

Add new environment variable.

env<O extends Record<string, unknown> | void = CO>(
name: string,
description: string,
options?: IEnvVarOptions,
): Command<Merge<CO, MapOptionTypes<O>>, CA, CG, PG, P>
example(name: string, description: string): this

Add new command example.

executable(): this

Make command executable.

getAliases(): string[]

Get command name aliases.

getArgsDefinition(): string | undefined

Get arguments definition. E.g: input-file:string output-file:string

getArgument(name: string): IArgument | undefined

Get argument by name.

getArguments(): IArgument[]

Get arguments.

getBaseCommand(name: string, hidden?: boolean): Command | undefined

Get base command by name or alias.

getBaseCommands(hidden?: boolean): Array<Command>

Get base commands.

getBaseCompletion(name: string): ICompletion | undefined

Get base completion by name.

getBaseCompletions(): ICompletion[]

Get base completions.

getBaseEnvVar(name: string, hidden?: boolean): IEnvVar | undefined

Get base environment variable by name.

getBaseEnvVars(hidden?: boolean): IEnvVar[]

Get base environment variables.

getBaseOption(name: string, hidden?: boolean): IOption | undefined

Get base option by name.

getBaseOptions(hidden?: boolean): IOption[]

Get base options.

getBaseType(name: string): IType | undefined

Get base type by name.

getBaseTypes(): IType[]

Get base types.

getCommand(name: string, hidden?: boolean): Command | undefined

Get command by name or alias.

getCommands(hidden?: boolean): Array<Command>

Get commands.

getCompletion(name: string): ICompletion | undefined

Get completion by name.

Get completions.

getDescription(): string

Get command description.

getEnvVar(name: string, hidden?: boolean): IEnvVar | undefined

Get environment variable by name.

getEnvVars(hidden?: boolean): IEnvVar[]

Get environment variables.

getExample(name: string): IExample | undefined

Get example with given name.

getExamples(): IExample[]

Get all examples.

getGlobalCommand(name: string, hidden?: boolean): Command | undefined

Get global command by name or alias.

getGlobalCommands(hidden?: boolean): Array<Command>

Get global commands.

getGlobalCompletion(name: string): ICompletion | undefined

Get global completions by name.

getGlobalCompletions(): ICompletion[]

Get global completions.

getGlobalEnvVar(name: string, hidden?: boolean): IEnvVar | undefined

Get global environment variable by name.

getGlobalEnvVars(hidden?: boolean): IEnvVar[]

Get global environment variables.

getGlobalOption(name: string, hidden?: boolean): IOption | undefined

Get global option from parent command's by name.

getGlobalOptions(hidden?: boolean): IOption[]

Get global options.

getGlobalParent(): Command | undefined

Get parent command from global executed command. Be sure, to call this method only inside an action handler. Unless this or any child command was executed, this method returns always undefined.

getGlobalType(name: string): IType | undefined

Get global type by name.

getGlobalTypes(): IType[]

Get global types.

getHelp(options?: HelpOptions): string

Get generated help.

getLiteralArgs(): string[]

Get all arguments defined after the double dash.

Get main command.

getName(): string

Get command name.

getOption(name: string, hidden?: boolean): IOption | undefined

Get option by name.

getOptions(hidden?: boolean): IOption[]

Get options.

Get parent command.

getPath(): string

Get full command path.

getRawArgs(): string[]

Get original command-line arguments.

Get short command description. This is the first line of the description.

getType(name: string): IType | undefined

Get type by name.

getTypes(): IType[]

Get types.

getVersion(): string | undefined

Get command version.

global(): this

Make command globally available.

globalComplete(
name: string,
complete: ICompleteHandler,
options?: Omit<ICompleteOptions, "global">,
): this
globalEnv<G extends Record<string, unknown> | void = CG>(
name: string,
description: string,
options?: Omit<IEnvVarOptions, "global">,
): Command<CO, CA, Merge<CG, MapOptionTypes<G>>, PG, P>
globalOption<G extends Record<string, unknown> | void = CG>(
flags: string,
desc: string,
opts?: Omit<ICommandOption<Partial<CO>, CA, Merge<CG, MapOptionTypes<G>>, PG, P>, "global"> | IFlagValueHandler,
): Command<CO, CA, Merge<CG, MapOptionTypes<G>>, PG, P>
globalType(
name: string,
type: Type<unknown> | ITypeHandler<unknown>,
options?: Omit<ITypeOptions, "global">,
): this

Check if command has arguments.

hasCommand(name: string, hidden?: boolean): boolean

Checks whether a child command exists by given name or alias.

hasCommands(hidden?: boolean): boolean

Checks whether the command has sub-commands or not.

hasEnvVar(name: string, hidden?: boolean): boolean

Checks whether the command has an environment variable with given name or not.

hasEnvVars(hidden?: boolean): boolean

Checks whether the command has environment variables or not.

hasExample(name: string): boolean

Checks whether the command has an example with given name or not.

hasExamples(): boolean

Checks whether the command has examples or not.

hasOption(name: string, hidden?: boolean): boolean

Checks whether the command has an option with given name or not.

hasOptions(hidden?: boolean): boolean

Checks whether the command has options or not.

help(help: string | IHelpHandler<Partial<CO>, Partial<CA>, CG, PG> | HelpOptions): this

Set command help.

helpOption(enable: false): this

Disable help option.

helpOption(
flags: string,
desc?: string,
opts?: ICommandOption<Partial<CO>, CA, CG, PG, P> & { global: true; },
): this

Set global help option.

helpOption(
flags: string,
desc?: string,
opts?: ICommandOption<CO, CA, CG, PG, P>,
): this

Set help option.

helpOption(
flags: string,
desc?: string,
opts?: IAction<CO, CA, CG, PG, P>,
): this

Set help option.

hidden(): this

Hide command from help, completions, etc.

name(name: string): this

Set command name.

noExit(): this

Same as .throwErrors() but also prevents calling Deno.exit after printing help or version with the --help and --version option.

option<G extends Record<string, unknown> | void = CG>(
flags: string,
desc: string,
opts: ICommandOption<Partial<CO>, CA, Merge<CG, MapOptionTypes<G>>, PG, P> & { global: true; } | IFlagValueHandler,
): Command<CO, CA, Merge<CG, MapOptionTypes<G>>, PG, P>

Add a new option.

option<O extends Record<string, unknown> | void = CO>(
flags: string,
desc: string,
opts?: ICommandOption<Merge<CO, MapOptionTypes<O>>, CA, CG, PG, P> | IFlagValueHandler,
): Command<Merge<CO, MapOptionTypes<O>>, CA, CG, PG, P>
parse(args?: string[]): Promise<IParseResult<CO, CA, CG, PG, P>>

Parse command line arguments and execute matched command.

removeCommand(name: string): Command | undefined

Remove sub-command by name or alias.

removeOption(name: string): IOption | undefined

Remove option by name.

reset(): OneOf<P, this>

Reset internal command reference to main command.

select<O extends Record<string, unknown> | void = any, A extends Array<unknown> = any, G extends Record<string, unknown> | void = any>(name: string): Command<O, A, G, PG, P>

Set internal command pointer to child command with given name.

showHelp(options?: HelpOptions): void

Output generated help without exiting.

showVersion(): void

Output generated help without exiting.

stopEarly(stopEarly?): this

Enable stop early. If enabled, all arguments starting from the first non option argument will be passed as arguments with type string to the command action handler.

For example: command --debug-level warning server --port 80

Will result in: - options: {debugLevel: 'warning'} - args: ['server', '--port', '80']

throwErrors(): this

Throw validation error's instead of calling Deno.exit() to handle validation error's manually.

A validation error is thrown when the command is wrongly used by the user. For example: If the user passes some invalid options or arguments to the command.

This has no effect for parent commands. Only for the command on which this method was called and all child commands.

Example:

try {
  cmd.parse();
} catch(error) {
  if (error instanceof ValidationError) {
    cmd.showHelp();
    Deno.exit(1);
  }
  throw error;
}
type(
name: string,
handler: Type<unknown> | ITypeHandler<unknown>,
options?: ITypeOptions,
): this

Register custom type.

usage(usage: string): this

Set the command usage. Defaults to arguments.

useRawArgs(useRawArgs?): Command<CO, Array<string>, CG, PG, P>

Disable parsing arguments. If enabled the raw arguments will be passed to the action handler. This has no effect for parent or child commands. Only for the command on which this method was called.

version(version: string | IVersionHandler<Partial<CO>, Partial<CA>, CG, PG>): this

Set command version.

versionOption(enable: false): this

Disable version option.

versionOption(
flags: string,
desc?: string,
opts?: ICommandOption<Partial<CO>, CA, CG, PG, P> & { global: true; },
): this

Set global version option.

versionOption(
flags: string,
desc?: string,
opts?: ICommandOption<CO, CA, CG, PG, P>,
): this

Set version option.

versionOption(
flags: string,
desc?: string,
opts?: IAction<CO, CA, CG, PG, P>,
): this

Set version option.