Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
1691344
commit 1345f98
Showing
27 changed files
with
763 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// const commander = require('commander'); // (normal include) | ||
const commander = require('../'); // include commander in git clone of commander repo | ||
const program = new commander.Command(); | ||
|
||
// Commander supports nested subcommands. | ||
// .command() can add a subcommand with an action handler or an executable. | ||
// .addCommand() adds a prepared command with an actiomn handler. | ||
|
||
// Example output: | ||
// | ||
// $ node nestedCommands.js brew tea | ||
// brew tea | ||
// $ node nestedCommands.js heat jug | ||
// heat jug | ||
|
||
// Add nested commands using `.command()`. | ||
const brew = program.command('brew'); | ||
brew | ||
.command('tea') | ||
.action(() => { | ||
console.log('brew tea'); | ||
}); | ||
brew | ||
.command('tea') | ||
.action(() => { | ||
console.log('brew tea'); | ||
}); | ||
|
||
// Add nested commands using `.addCommand(). | ||
// The command could be created separately in another module. | ||
function makeHeatCommand() { | ||
const heat = new commander.Command('heat'); | ||
heat | ||
.command('jug') | ||
.action(() => { | ||
console.log('heat jug'); | ||
}); | ||
heat | ||
.command('pot') | ||
.action(() => { | ||
console.log('heat pot'); | ||
}); | ||
return heat; | ||
} | ||
program.addCommand(makeHeatCommand()); | ||
|
||
program.parse(process.argv); |
Oops, something went wrong.