commander
Advanced tools
Changelog
[8.0.0] (2021-06-25)
.argument(name, description)
for adding command-arguments ([#1490])
.createArgument()
factory method ([#1497]).addArgument()
([#1490])Argument
supports .choices()
([#1525]).showHelpAfterError()
to display full help or a custom message after an error ([#1534]).hook()
with support for 'preAction'
and 'postAction'
callbacks ([#1514]).opts()
return type using TypeScript generics ([#1539]).getOptionValue()
and .setOptionValue()
([#1521]).parseAsync()
is now declared as async
([#1513])Help
method .visibleArguments()
returns array of Argument
([#1490])CommanderError
code commander.invalidOptionArgument
renamed commander.invalidArgument
([#1508]).addTextHelp()
callback no longer allows result of undefined
, now just string
([#1516])index.tab
into a file per class ([#1522]).showHelpAfteError()
) ([#1534])Command
property .arg
initialised to empty array (was previously undefined) ([#1529])cmd.description(desc, argDescriptions)
for adding argument descriptions ([#1490])
.argument(name, description)
instead)InvalidOptionArgumentError
(replaced by InvalidArgumentError
) ([#1508])Command
object ([#1520])
program
export)If you have a simple program without an action handler, you will now get an error if there are missing command-arguments.
program
.option('-d, --debug')
.arguments('<file>');
program.parse();
$ node trivial.js
error: missing required argument 'file'
If you want to show the help in this situation, you could check the arguments before parsing:
if (process.argv.length === 2)
program.help();
program.parse();
Or, you might choose to show the help after any user error:
program.showHelpAfterError();
Changelog
[7.2.0] (2021-03-22)
parent
property on Command
([#1475]).attributeName()
on Option
([#1483])Changelog
[7.1.0] (2021-02-15)
.cjs
to list of expected script file extensions ([#1449])process.mainModule
([#1448])command('*')
and call when command line includes options ([#1464])on('command:*', ...)
and call when command line includes unknown options ([#1464])Changelog
[7.0.0] (2021-01-15)
.enablePositionalOptions()
to let program and subcommand reuse same option ([#1427]).passThroughOptions()
to pass options through to other programs without needing --
([#1427]).allowExcessArguments(false)
to show an error message if there are too many command-arguments on command line for the action handler ([#1409]).configureOutput()
to modify use of stdout and stderr or customise display of errors ([#1387]).addHelpText()
to add text before or after the built-in help, for just current command or also for all subcommands ([#1296]).createOption()
to support subclassing of automatically created options (like .createCommand()
) ([#1380])program.opts()
.storeOptionsAsProperties()
.help()
and .outputHelp()
(removed from README) ([#1296])process.stderr.write()
instead of console.error()
.on('--help')
(removed from README) ([#1296]).passCommandToAction()
([#1409])
.allowExcessArguments(false)
The biggest change is the parsed option values. Previously the options were stored by default as properties on the command object, and now the options are stored separately.
If you wish to restore the old behaviour and get running quickly you can call .storeOptionsAsProperties()
.
To allow you to move to the new code patterns incrementally, the action handler will be passed the command twice,
to match the new "options" and "command" parameters (see below).
program options
Use the .opts()
method to access the options. This is available on any command but is used most with the program.
program.option('-d, --debug');
program.parse();
// Old code before Commander 7
if (program.debug) console.log(`Program name is ${program.name()}`);
// New code
const options = program.opts();
if (options.debug) console.log(`Program name is ${program.name()}`);
action handler
The action handler gets passed a parameter for each command-argument you declared. Previously by default the next parameter was the command object with the options as properties. Now the next two parameters are instead the options and the command. If you only accessed the options there may be no code changes required.
program
.command('compress <filename>')
.option('-t, --trace')
// Old code before Commander 7
.action((filename, cmd) => {
if (cmd.trace) console.log(`Command name is ${cmd.name()}`);
});
// New code
.action((filename, options, command) => {
if (options.trace) console.log(`Command name is ${command.name()}`);
});
If you already set .storeOptionsAsProperties(false)
you may still need to adjust your code.
program
.command('compress <filename>')
.storeOptionsAsProperties(false)
.option('-t, --trace')
// Old code before Commander 7
.action((filename, command) => {
if (command.opts().trace) console.log(`Command name is ${command.name()}`);
});
// New code
.action((filename, options, command) => {
if (command.opts().trace) console.log(`Command name is ${command.name()}`);
});
Changelog
[6.2.1] (2020-12-13)