Skip to main content
Latest
class Command
extends EventEmitter
import { Command } from "https://deno.land/x/license@1.0.3/deps/cmd.ts";

Constructors

new
Command(name?: string)

Initialize a new Command.

Index Signatures

[key: string]: any

Properties

args: string[]
commands: Command[]

Methods

Display an error message if a mandatory option does not have a value. Lazy calling after checking for help flags from leaf subcommand.

_dispatchSubcommand(
commandName: string,
operands: any,
unknown?: any,
)
_executeSubCommand(subcommand: Command, ...args: string[])

Execute a sub-command executable.

_exit(
exitCode: number,
code: string,
message: string,
)

Call process.exit, and _exitCallback if defined.

_findCommand(name?: string)

Find matching command.

_findOption(arg?: string)

Return an option matching arg if any.

_getOptionValue(key: string)

Retrieve option value

Output help information and exit. Display for error situations.

_optionEx(
config: any,
flags: string,
description?: string,
fn?: any,
defaultValue?: any,
)

Internal implementation shared by .option() and .requiredOption()

_parseCommand(operands: any, unknown: any)

Process arguments in context of this command.

_parseExpectedArgs(args: any[])

Parse expected args.

For example ["[type]"] becomes [{ required: false, name: 'type' }].

_setOptionValue(key: string, value: any)

Store option value

action(fn: (...args: any[]) => void | Promise<void>)

Register callback fn for the command.

Examples:

 program
   .command('help')
   .description('display verbose help')
   .action(function() {
      // output help here
   });
addCommand(cmd: Command, opts?: CommandOptions)

Add a prepared subcommand.

See .command() for creating an attached subcommand which inherits settings from its parent.

addHelpCommand(enableOrNameAndArgs?: boolean | string, description?: string)

Override default decision whether to add implicit help command.

addHelpCommand() // force on addHelpCommand(false); // force off addHelpCommand('help [cmd]', 'display help for [cmd]'); // force on with custom detais

alias(alias: string)

Set an alias for the command.

You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.

aliases(aliases: string[])

Set aliases for the command.

Only the first alias is shown in the auto-generated help.

allowUnknownOption(arg?: boolean)

Allow unknown options on the command line.

arguments(desc: string)

Define argument syntax for the command.

command(
nameAndArgs: string,
actionOptsOrExecDesc?: CommandOptions | string,
execOpts?: ExecutableCommandOptions,
)

Define a command.

There are two styles of command: pay attention to where to put the description.

Examples:

 // Command implemented using action handler (description is supplied separately to `.command`)
 program
   .command('clone <source> [destination]')
   .description('clone a repository into a newly created directory')
   .action((source, destination) => {
     console.log('clone command called');
   });

 // Command implemented using separate executable file (description is second parameter to `.command`)
 program
   .command('start <service>', 'start named service')
   .command('stop [service]', 'stop named service, or all if no name supplied');

Return command help documentation.

createCommand(name?: string)

Factory routine to create a new unattached command.

See .command() for creating an attached subcommand, which uses this routine to create the command. You can override createCommand to customise subcommands.

description(str: string, argsDescription?: { [argName: string]: string; })

Set the description to str.

exitOverride(fn?: (err: CommanderError) => never | void)

Register callback to use as replacement for calling process.exit.

help(cb?: (str: string) => string)

Output help information and exit.

Return program help documentation.

helpOption(flags?: string, description?: string)

You can pass in flags and a description to override the help flags and help description for your command.

Return the largest arg length.

Return the largest command length.

Return the largest option length.

missingArgument(name: string)

Argument name is missing.

Option does not have a value, and is a mandatory option.

name(str?: string)

Get or set the name of the command

option<T>(
flags: string,
description?: string,
fn?: (value: string, previous: T) => T | RegExp,
defaultValue?: T,
)

Define option with flags, description and optional coercion fn.

The flags string should contain both the short and long flags, separated by comma, a pipe or space. The following are all valid all will output this way when --help is used.

"-p, --pepper" "-p|--pepper" "-p --pepper"

Examples:

// simple boolean defaulting to undefined
program.option('-p, --pepper', 'add pepper');

program.pepper
// => undefined

--pepper
program.pepper
// => true

// simple boolean defaulting to true (unless non-negated option is also defined)
program.option('-C, --no-cheese', 'remove cheese');

program.cheese
// => true

--no-cheese
program.cheese
// => false

// required argument
program.option('-C, --chdir <path>', 'change the working directory');

--chdir /tmp
program.chdir
// => "/tmp"

// optional argument
program.option('-c, --cheese [type]', 'add cheese [marble]');

Return help for options.

optionMissingArgument(option: Option, flag?: string)

Option is missing an argument, but received flag or nothing.

Return an object containing options as key-value pairs

outputHelp(cb?: (str: string) => string)

Output help information for this command.

When listener(s) are available for the helpLongFlag those callbacks are invoked.

Return the pad width.

parse(argv?: string[], parseOptions?: ParseOptions)

Parse argv, setting options and invoking commands when defined.

The default expectation is that the arguments are from node and have the application as argv[0] and the script being run in argv[1], with user parameters after that.

Examples:

 program.parse(process.argv);
 program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
 program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
parseAsync(argv?: string[], parseOptions?: ParseOptions)

Parse argv, setting options and invoking commands when defined.

Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.

The default expectation is that the arguments are from node and have the application as argv[0] and the script being run in argv[1], with user parameters after that.

Examples:

 program.parseAsync(process.argv);
 program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
 program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
parseOptions(argv: string[]): ParseOptionsResult

Parse options from argv removing known options, and return argv split into operands and unknown arguments.

Examples:

argv => operands, unknown --known kkk op => [op], [] op --known kkk => [op], [] sub --unknown uuu op => [sub], [--unknown uuu op] sub -- --unknown uuu op => [sub --unknown uuu op], []

passCommandToAction(value?: boolean)

Whether to pass command to action handler, or just the options (specify false).

Return prepared commands.

requiredOption<T>(
flags: string,
description: string,
fn: (value: string, previous: T) => T,
defaultValue?: T,
)
storeOptionsAsProperties(value?: boolean)

Whether to store option values as properties on command object, or store separately (specify false). In both cases the option values can be accessed using .opts().

Unknown command.

unknownOption(flag: string)

Unknown option flag.

usage(str?: string)

Set / get the command usage str.

version(
str: string,
flags?: string,
description?: string,
)

Set the program version to str.

This method auto-registers the "-V, --version" flag which will print the version number when passed.

You can optionally supply the flags and description to override the defaults.