Files
John Gee 1345f98c0f Parse rework for nested commands (#1149)
* First cut at parse rework

- skip asterisk tests
- other tests runnning
- nested commands untested
- lots of details to check

* Add check for requiredOption when calling executable subcommand

* Set program name using supported approach

* Add .addCommand, easy after previous work

* Add support for default command using action handler

- and remove stale _execs

* Add implicitHelpCommand and change help flags description

* Add implicit help command to help

* Turn off implicit help command for most help tests

* .addHelpCommand

* Remove addHelpCommand from tests and make match more narrow

* Use test of complete default help output

* Add tests for whether implicit help appears in help

* Add tests that help command dispatched to correct command

* Add simple nested subcommand test

* Add default command tests for action based subcommand

* Remove mainModule, out of scope for current PR

* Add legacy asterisk handling and tests

* Add more initialisation so object in known state

* Tests for addCommand

* Add first cut at enhanced default error detection

* Add test that addCommand requires name

* Add block on automatic name generation for deeply nested executables

* Add block on automatic name generation for deeply nested executables

* Fix describe name for tests

* Refine unknownCommand handling and add tests

* Add suggestion to try help, when appropriate

* Fix typo

* Move common command configuration options in README, and add isDefault example program

* Add isDefault and example to README

* Add nested commands

* Document .addHelpCommand, and tweaks

* Remove old default command, and rework command:* example

* Document .addCommand

* Remove comment referring to removed code.

* Revert the error tip "try --help", not happy with the wording

* Say "unknown command", like "unknown option"

* Set properties to null rather than undefined in constructor
2020-01-31 20:55:38 +13:00

37 lines
956 B
JavaScript

// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();
// Example program using the command configuration option isDefault to specify the default command.
//
// $ node defaultCommand.js build
// build
// $ node defaultCommand.js serve -p 8080
// server on port 8080
// $ node defaultCommand.js -p 443
// server on port 443
program
.command('build')
.description('build web site for deployment')
.action(() => {
console.log('build');
});
program
.command('deploy')
.description('deploy web site to production')
.action(() => {
console.log('deploy');
});
program
.command('serve', { isDefault: true })
.description('launch web server')
.option('-p,--port <port_number>', 'web port')
.action((opts) => {
console.log(`server on port ${opts.port}`);
});
program.parse(process.argv);