import { Command } from "https://deno.land/x/cmd@v1.2.0/mod.ts";
Properties
Methods
Display an error message if a mandatory option does not have a value. Lazy calling after checking for help flags from leaf subcommand.
Call process.exit, and _exitCallback if defined.
Internal implementation shared by .option() and .requiredOption()
Parse expected args
.
For example ["[type]"]
becomes [{ required: false, name: 'type' }]
.
Register callback fn
for the command.
Examples:
program
.command('help')
.description('display verbose help')
.action(function() {
// output help here
});
Add a prepared subcommand.
See .command() for creating an attached subcommand which inherits settings from its parent.
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
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.
Set aliases for the command.
Only the first alias is shown in the auto-generated help.
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');
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.
Set the description to str
.
Register callback to use as replacement for calling process.exit.
You can pass in flags and a description to override the help flags and help description for your command.
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]');
Option
is missing an argument, but received flag
or nothing.
Output help information for this command.
When listener(s) are available for the helpLongFlag those callbacks are invoked.
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]
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]
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], []
Whether to pass command to action handler, or just the options (specify false).
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().
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.