commander
Advanced tools
Changelog
[13.1.0] (2025-01-21)
.option('--ws, --workspace')
([#2312])Changelog
[13.0.0] (2024-12-30)
.parse()
with default settings ([#2299]).saveStateBeforeParse()
and .restoreStateBeforeParse()
for use by subclasses ([#2299])styleTitle()
to add color to help using .configureHelp()
or Help subclass ([#2251]).configureOutput()
for getOutHasColors()
, getErrHasColors()
, and stripColor()
([#2251])minWidthToWrap
([#2251])displayWidth()
, boxWrap()
, preformatted()
et al ([#2251])-
([#2270]).parse()
if storeOptionsAsProperties: true
([#2299])this
in parameters for action handler callback ([#2197])Help.wrap()
refactored into formatItem()
and boxWrap()
([#2251])Excess command-arguments
It is now an error for the user to specify more command-arguments than are expected. (allowExcessArguments
is now false by default.)
Old code:
program.option('-p, --port <number>', 'port number');
program.action((options) => {
console.log(program.args);
});
Now shows an error:
$ node example.js a b c
error: too many arguments. Expected 0 arguments but got 3.
You can declare the expected arguments. The help will then be more accurate too. Note that declaring new arguments will change what is passed to the action handler.
program.option('-p, --port <number>', 'port number');
program.argument('[args...]', 'remote command and arguments'); // expecting zero or more arguments
program.action((args, options) => {
console.log(args);
});
Or you could suppress the error, useful for minimising changes in legacy code.
program.option('-p, --port', 'port number');
program.allowExcessArguments();
program.action((options) => {
console.log(program.args);
});
Changelog
[12.1.0] (2024-05-18)
node --eval
and node --print
when call .parse()
with no arguments ([#2164])node:
([#2170])Changelog
[12.0.0] (2024-02-03)
.addHelpOption()
as another way of configuring built-in help option ([#2006]).helpCommand()
for configuring built-in help command ([#2087])passThroughOptions
constraints when using .addCommand
and throw if parent command does not have .enablePositionalOptions()
enabled ([#1937]).storeOptionsAsProperties()
after setting an option value ([#1928])@api private
with documented @private
([#1949]).addHelpCommand()
now takes a Command (passing string or boolean still works as before but deprecated) ([#2087]).addHelpCommand()
passing string or boolean (use .helpCommand()
or pass a Command) ([#2087])program
export instead) ([#2017])global program
If you are using the deprecated default import of the global Command object, you need to switch to using a named import (or create a new Command
).
// const program = require('commander');
const { program } = require('commander');
option and command clashes
A couple of configuration problems now throw an error, which will pick up issues in existing programs:
Changelog
[11.1.0] (2023-10-13)
OptionValueSource
to allow any string, to match supported use of custom sources ([#1983])Command.version()
can also be used as getter ([#1982])Commands.executableDir()
, for when not configured ([#1965])registeredArguments
property on Command
with the array of defined Argument
(like Command.options
for Option
) ([#2010])envVar
, presetArg
([#2019])argChoices
, defaultValue
, defaultValueDescription
([#2019])Command._args
was private anyway, but now available as registeredArguments
([#2010])Changelog
[11.0.0] (2023-06-16)
Changelog
[10.0.1] (2023-04-15)
Option.optionFlags
property from TypeScript definition ([#1844]).implies()
([#1854])