Module

x/cliffy/command/mod.ts>Command

Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Extremely Popular
Go to Latest
class Command
import { Command } from "https://dotland.deno.dev/x/cliffy@v0.15.0/command/mod.ts";

Base command implementation without pre configured command's and option's.

Type Parameters

optional
O = any
optional
A extends Array<any> = any

Properties

private
_allowEmpty: boolean
private
optional
_globalParent: Command
private
optional
_helpOption: IDefaultOption<O, A> | false
private
_name: string
private
optional
_parent: Command
private
_stopEarly: boolean
private
_useRawArgs: boolean
private
optional
_versionOption: IDefaultOption<O, A> | 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<O, A>
private
hasDefaults: boolean
private
isExecutable: boolean
private
isGlobal: boolean
private
isHidden: boolean
private
literalArgs: string[]
private
options: IOption<O, A>[]
private
rawArgs: string[]
private
throwOnError: boolean
private
types: ITypeMap
private
optional
ver: string

Methods

private
registerDefaults(): this
protected
execute(options: O, ...args: A): Promise<IParseResult<O, A>>

Execute command.

protected
executeExecutable(args: string[])

Execute external sub-command.

protected
findActionFlag(flags: O): IOption | undefined

Execute help command if help flag is set.

protected
getBaseType(name: string): IType | undefined
protected
getGlobalType(name: string): IType | undefined
protected
getType(name: string): IType | undefined
protected
parseArguments(args: string[], flags: O): A

Match commands and arguments from command line arguments.

protected
parseFlags(args: string[]): IFlagsResult<O>

Parse command line args.

protected
parseType(type: ITypeInfo): unknown
protected
shouldThrowErrors(): boolean
protected
validateEnvVars()

Validate environment variables.

action(fn: IAction<O, A>): this

Set command handler.

alias(alias: string): this

Add new command alias.

allowEmpty(allowEmpty?): this

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

arguments(args: string): this

Set command arguments.

command(
name: string,
desc?: string,
override?: boolean,
): this

Add new sub-command.

command(
name: string,
cmd?: Command,
override?: boolean,
): this
complete(
name: string,
complete: ICompleteHandler,
options?: ICompleteOptions,
): this

Register command specific custom type.

default(name: string): this

Set default command. The default command will be called if no action handler is registered.

description(description: IDescription): this

Set command description.

env(
name: string,
description: string,
options?: IEnvVarOptions,
): this

Add new environment variable.

error(error: Error, showHelp?): Error

Handle error. If .throwErrors() was called all error's will be thrown, otherwise Deno.exit(1) will be called.

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.

getArgument(name: string): IArgument | undefined

Get argument.

Get arguments.

getBaseCommand<O = any>(name: string, hidden?: boolean): Command<O> | undefined
getBaseCommands(hidden?: boolean): Command[]
getBaseCompletion(name: string): ICompletion | undefined
getBaseEnvVar(name: string, hidden?: boolean): IEnvVar | undefined
getBaseEnvVars(hidden?: boolean): IEnvVar[]
getBaseOption(name: string, hidden?: boolean): IOption | undefined

Get base option by name.

getBaseOptions(hidden?: boolean): IOption[]
getCommand<O = any>(name: string, hidden?: boolean): Command<O> | undefined

Get sub-command with given name.

getCommands(hidden?: boolean): Command[]

Get sub-commands.

getCompletion(name: string)
getDescription(): string

Get command description.

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

Get environment variable with given name.

getEnvVars(hidden?: boolean): IEnvVar[]

Get environment variables.

getExample(name: string): IExample | undefined

Get example with given name.

Get examples.

getGlobalCommand<O = any>(name: string, hidden?: boolean): Command<O> | undefined
getGlobalCommands(hidden?: boolean): Command[]
getGlobalCompletion(name: string): ICompletion | undefined
getGlobalEnvVar(name: string, hidden?: boolean): IEnvVar | undefined
getGlobalEnvVars(hidden?: boolean): IEnvVar[]
getGlobalOption(name: string, hidden?: boolean): IOption | undefined

Get global option from parent command's by name.

getGlobalOptions(hidden?: boolean): IOption[]
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.

getHelp(): string

Get generated help.

getLiteralArgs(): string[]

Get main command.

getName(): string

Get command name.

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

Get option by name.

getOptions(hidden?: boolean): IOption[]
getParent(): Command | undefined

Get parent command.

getPath(): string

Get full command path of all parent command names and current command name.

getRawArgs(): string[]
getVersion(): string | undefined

Get command version.

global(): this

Make command globally available.

Check if command has arguments.

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

Checks whether the command has a sub-command with given name or not.

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.

Output generated help without exiting.

helpOption(
flags: string | false,
desc?: string,
opts?: IAction<O, A> | ICommandOption<O, A>,
): this
hidden(): this

Hide command from help, completions, etc.

name(name: string): this

Get or set command name.

option(
flags: string,
desc: string,
): this

Add new option (flag).

parse(args?: string[], dry?: boolean): Promise<IParseResult<O, A>>

Parse command line arguments and execute matched command.

removeCommand<O = any>(name: string): Command<O> | undefined

Remove sub-command with given name.

removeOption(name: string): IOption | undefined

Remove option by name.

reset(): this

Reset internal command reference to main command.

select(name: string): this

Reset internal command reference to child command with given name.

stopEarly(stopEarly?): this

If enabled, all arguments starting from the first non option argument will be interpreted as raw argument.

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

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

throwErrors(): this

Throw error's instead of calling Deno.exit() to handle error's manually. This has no effect for parent commands. Only for the command on which this method was called and all child commands.

type(
name: string,
handler: Type<unknown> | ITypeHandler<unknown>,
options?: ITypeOptions,
): this

Register command specific custom type.

useRawArgs(useRawArgs?): this

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): this

Set command version.

versionOption(
flags: string | false,
desc?: string,
opts?: IAction<O, A> | ICommandOption<O, A>,
): this
import Command
import { Command } from "https://dotland.deno.dev/x/cliffy@v0.15.0/command/mod.ts";