Protected
exitProtected
fsProtected
loggerRest
...args: any[]Define argument syntax for command, adding a prepared argument.
this
command for chaining
Add a prepared subcommand.
See .command() for creating an attached subcommand which inherits settings from its parent.
Optional
opts: CommandOptionsthis
command for chaining
Override default decision whether to add implicit help command.
Optional
enableOrNameAndArgs: string | booleanOptional
description: stringthis
command for chaining
addHelpCommand() // force on
addHelpCommand(false); // force off
addHelpCommand('help [cmd]', 'display help for [cmd]'); // force on with custom details
Add additional text to be displayed with the built-in help.
Position is 'before' or 'after' to affect just this command, and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.
Add a prepared Option.
See .option() and .requiredOption() for creating and attaching an option in a single call.
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.
this
command for chaining
Get alias for the command.
Set aliases for the command.
Only the first alias is shown in the auto-generated help.
this
command for chaining
Get aliases for the command.
Allow excess command-arguments on the command line. Pass false to make excess arguments an error.
Optional
allowExcess: booleanthis
command for chaining
Allow unknown options on the command line.
Optional
allowUnknown: booleanthis
command for chaining
Define argument syntax for command.
The default is that the argument is required, and you can explicitly indicate this with <> around the name. Put [] around the name for an optional argument.
Optional
defaultValue: Tthis
command for chaining
program.argument('<input-file>');
program.argument('[output-file]');
Optional
description: stringOptional
defaultValue: unknownDefine argument syntax for command, adding multiple at once (without descriptions).
See also .argument().
this
command for chaining
program.arguments('<cmd> [env]');
Alter parsing of short flags with optional values.
Optional
combine: booleanthis
command for chaining
// for `.option('-f,--flag [value]'):
.combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
.combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
Define a command, implemented using an action handler.
command name and arguments, args are <required>
or [optional]
and last may also be variadic...
Optional
opts: CommandOptionsconfiguration options
new command
The command description is supplied using .description
, not as a parameter to .command
.
program
.command('clone <source> [destination]')
.description('clone a repository into a newly created directory')
.action((source, destination) => {
console.log('clone command called');
});
Define a command, implemented in a separate executable file.
command name and arguments, args are <required>
or [optional]
and last may also be variadic...
description of executable command
Optional
opts: ExecutableCommandOptionsconfiguration options
this
command for chaining
The command description is supplied as the second parameter to .command
.
program
.command('start <service>', 'start named service')
.command('stop [service]', 'stop named service, or all if no name supplied');
You can customise the help by overriding Help properties using configureHelp(), or with a subclass of Help by overriding createHelp().
Get configuration
The default output goes to stdout and stderr. You can customise this for special applications. You can also customise the display of errors by overriding outputError.
The configuration properties are all functions:
// functions to change where being written, stdout and stderr
writeOut(str)
writeErr(str)
// matching functions to specify width for wrapping help
getOutHelpWidth()
getErrHelpWidth()
// functions based on what is being written out
outputError(str, write) // used for displaying errors, and not used for displaying help
Get configuration
Copy settings that are useful to have in common across root command and subcommands.
(Used internally when adding a command using .command()
so subcommands inherit parent settings.)
Factory routine to create a new unattached argument.
See .argument() for creating an attached argument, which uses this routine to create the argument. You can override createArgument to return a custom argument.
Optional
description: stringFactory routine to create a new unattached option.
See .option() for creating an attached option, which uses this routine to create the option. You can override createOption to return a custom option.
Optional
description: stringSet the description.
this
command for chaining
since v8, instead use .argument to add command argument with description
Get the description.
Enable positional options. Positional means global options are specified before subcommands which lets subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.
The default behaviour is non-positional and global options may appear anywhere on the command line.
Optional
positional: booleanthis
command for chaining
Register callback to use as replacement for calling process.exit.
Optional
callback: ((err) => void)Output help information and exit.
Outputs built-in help, and custom text added using .addHelpText()
.
Optional
context: HelpContextOptional
cb: ((str) => string)since v7
You can pass in flags and a description to override the help flags and help description for your command. Pass in false to disable the built-in help option.
Optional
flags: string | booleanOptional
description: stringAdd hook for life cycle event.
Set the name of the command.
this
command for chaining
Get the name of the command.
Add a listener (callback) for when events occur. (Implemented using EventEmitter.)
Rest
...args: any[]Define option with flags
, description
and optional
coercion fn
.
The flags
string contains the short and/or 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"
Optional
description: stringOptional
defaultValue: string | booleanthis
command for chaining
// simple boolean defaulting to false
program.option('-p, --pepper', 'add pepper');
--pepper
program.pepper
// => Boolean
// simple boolean defaulting to true
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]');
Optional
defaultValue: TOptional
defaultValue: string | booleansince v7, instead use choices or a custom function
Output help information for this command.
Outputs built-in help, and custom text added using .addHelpText()
.
Optional
context: HelpContextOptional
cb: ((str) => string)since v7
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.
Optional
argv: string[]Optional
options: ParseOptionsthis
command for chaining
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]
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.
Optional
argv: string[]Optional
options: ParseOptionsPromise
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]
Parse options from argv
removing known options,
and return argv split into operands and unknown arguments.
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], []
Pass through options that come after command-arguments rather than treat them as command-options, so actual command-options come before command-arguments. Turning this on for a subcommand requires positional options to have been enabled on the program (parent commands).
The default behaviour is non-positional and options may appear before or after command-arguments.
Optional
passThrough: booleanthis
command for chaining
Define a required option, which must have a value after parsing. This usually means the option must be specified on the command line. (Otherwise the same as .option().)
The flags
string contains the short and/or long flags, separated by comma, a pipe or space.
Optional
description: stringOptional
defaultValue: string | booleanOptional
defaultValue: TOptional
defaultValue: string | booleansince v7, instead use choices or a custom function
Store option value.
Store option value and where the value came from.
Display the help or a custom message after an error occurs.
Optional
displayHelp: string | booleanDisplay suggestion of similar commands for unknown commands, or options for unknown options.
Optional
displaySuggestion: booleanWhether 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().
this
command for chaining
Optional
storeAsProperties: booleanSet the command usage.
this
command for chaining
Get the command usage.
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.
Optional
flags: stringOptional
description: stringGenerated using TypeDoc
Base class for all commands