Kind: Class
Source: lib/option.js
Option defines a command-line option, including its flags, default value, environment variable mapping, validation rules, and parsing behavior. It is added to a command and supplies metadata used while parsing arguments and building help output.
Methods
| Method | Signature | Returns |
|---|---|---|
default | default(value: undefined, description: undefined) | void |
preset | preset(arg: undefined) | void |
conflicts | conflicts(names: undefined) | void |
implies | implies(impliedOptionValues: undefined) | void |
env | env(name: undefined) | void |
argParser | argParser(fn: undefined) | void |
makeOptionMandatory | makeOptionMandatory(mandatory: undefined) | void |
hideHelp | hideHelp(hide: undefined) | void |
_collectValue | _collectValue(value: undefined, previous: undefined) | void |
choices | choices(values: undefined) | void |
name | name() | void |
attributeName | attributeName() | void |
helpGroup | helpGroup(heading: undefined) | void |
is | is(arg: undefined) | void |
isBoolean | isBoolean() | void |
Where it refuses work
Optionstops the work withInvalidArgumentErrorwhen!this.argChoices.includes(arg).Optionstops the work with an early return whenprevious === this.defaultValue || !Array.isArray(previous).Optionstops the work with an early return whenthis.variadic.Optionstops the work with an early return whenthis.long.Optionstops the work with an early return whenthis.negate.
Diagram
mermaidgraph LR Command[Command] -->|addOption| Option[Option] Environment[Environment variable] -->|env| Option Option -->|flags, defaults, rules| Parser[Argument parser] Parser -->|stores parsed value| CommandOptions[command.opts()]
Usage
jsconst { Command, Option } = require('commander');
const program = new Command();
const formatOption = new Option(
'-f, --format <type>',
'select output format'
)
.choices(['text', 'json'])
.default('text')
.env('OUTPUT_FORMAT');
const quietOption = new Option('-q, --quiet', 'suppress output')
.conflicts('verbose');
program
.addOption(formatOption)
.addOption(quietOption);
program.parse();
const options = program.opts();
console.log(options.format);
AI Coding Instructions
- Create options with
new Option(flags, description)before adding them to aCommand. - Use
choices()for accepted argument values andargParser()when values need custom conversion. - Use
conflicts()andimplies()to describe relationships between option names, not flag strings. - Set
env()when an option may read its value from an environment variable. - Use
hideHelp()only for options that should still parse but should not appear in generated help.
How it works
I’ll inspect the option-token parsing branch to document the actual preconditions for required, optional, variadic, boolean, and negated flags.
Relationships
- IMPORTS →
InvalidArgumentError
Used by
2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (2)
program—index.js:7Command—lib/command.js:14
Was this page helpful?