Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
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
Re-export
import { Command } from "https://deno.land/x/cliffy@v0.16.0/command/mod.ts";

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

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

protected
error(error: Error, showHelp?): Error

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

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

Returns the first option which has an action.

protected
getBaseType(name: string): IType | undefined

Get base type by name.

protected
getGlobalType(name: string): IType | undefined

Get global type by name.

protected
getType(name: string): IType | undefined

Get type by name.

protected
parseArguments(args: string[], flags: O): A

Parse command-line arguments.

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

Parse raw command line arguments.

protected
parseType(type: ITypeInfo): unknown

Parse argument type.

protected
shouldThrowErrors(): boolean

Check whether the command should throw errors or exit.

protected
validateEnvVars()

Validate environment variables.

action(fn: IAction<O, A>): 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(args: string): this

Set command arguments:

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

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

Add new sub-command.

command(
name: string,
cmd?: Command,
override?: boolean,
): this

Add new sub-command.

complete(
name: string,
complete: ICompleteHandler,
options?: ICompleteOptions,
): this

Register command specific custom type.

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

Set the long command description.

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

Add new environment variable.

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.

Get arguments.

getBaseCommand<O = any>(name: string, hidden?: boolean): Command<O> | undefined

Get base command by name.

getBaseCommands(hidden?: boolean): Command[]

Get base commands.

getBaseCompletion(name: string): ICompletion | undefined

Get base completion by name.

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.

Get base types.

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

Get command by name.

getCommands(hidden?: boolean): Command[]

Get commands.

getCompletion(name: string)

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.

Get all examples.

getGlobalCommand<O = any>(name: string, hidden?: boolean): Command<O> | undefined

Get global command by name.

getGlobalCommands(hidden?: boolean): Command[]

Get global commands.

getGlobalCompletion(name: string): ICompletion | undefined

Get global completions by name.

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.

Get global types.

getHelp(): 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.

getParent(): Command | undefined

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.

Get types.

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(enable: false): this

Disable help option.

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

Set help option.

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

Set help option.

hidden(): this

Hide command from help, completions, etc.

name(name: string): this

Set command name.

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

Add a new option.

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 by name.

removeOption(name: string): IOption | undefined

Remove option by name.

reset(): this

Reset internal command reference to main command.

select(name: string): this

Set internal command pointer to child command with given name.

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 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 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(enable: false): this

Disable version option.

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

Set version option.

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

Set version option.