Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Opt-in behaviour to avoid name pollution (#1102)
* Add object to hold option values separately from properties on command. Return directly from .opts(). * Add configureCommand, and support for passing options rather than command to action handler * Restore original opts() implementation when using old configuration * Use either/or new/old option storage, not both * Turn version test on again, old behaviour restored * Add tests for configureCommand, and fix bugs * Expand .opts tests to include modern configuration * Add TypeScript and inline documentation for configureCommand * Switch from modern:boolean to combo:string * Rework new behaviour with matching named routines. * Add example files for storeOptionsAsProperties * Add usage error, and make value default to true for new routines (so simpler call for that case) * Simpify description * Add section on avoiding option name clashes * Do not use else after a return
- Loading branch information
1 parent
df6284c
commit 81c6e28
Showing
11 changed files
with
408 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# We have not configured eslint for TypeScript so avoid bogus error messages in the test file and typings file. | ||
**/*.ts |
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,40 @@ | ||
#!/usr/bin/env node | ||
|
||
// To avoid possible name clashes, you can change the default behaviour | ||
// of storing the options values as properties on the command object. | ||
// In addition, you can pass just the options to the action handler | ||
// instead of a commmand object. This allows simpler code, and is more consistent | ||
// with the previous behaviour so less code changes from old code. | ||
// | ||
// Example output: | ||
// | ||
// $ node storeOptionsAsProperties-action.js show | ||
// undefined | ||
// undefined | ||
// | ||
// $ node storeOptionsAsProperties-action.js --name foo show --action jump | ||
// jump | ||
// foo | ||
|
||
const commander = require('../'); | ||
const program = new commander.Command(); | ||
|
||
program | ||
.storeOptionsAsProperties(false) // <--- change behaviour | ||
.passCommandToAction(false); // <--- change behaviour | ||
|
||
program | ||
.name('my-program-name') | ||
.option('-n,--name <name>'); | ||
|
||
program | ||
.command('show') | ||
.option('-a,--action <action>') | ||
.action((options) => { // <--- passed options, not Command | ||
console.log(options.action); // <--- matches old code | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
const programOptions = program.opts(); // <--- use opts to access option values | ||
console.log(programOptions.name); |
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,38 @@ | ||
#!/usr/bin/env node | ||
|
||
// To avoid possible name clashes, you can change the default behaviour | ||
// of storing the options values as properties on the command object. | ||
// You access the option values using the .opts() function. | ||
// | ||
// Example output: | ||
// | ||
// $ node storeOptionsAsProperties-opts.js show | ||
// undefined | ||
// undefined | ||
// | ||
// $ node storeOptionsAsProperties-opts.js --name foo show --action jump | ||
// jump | ||
// foo | ||
|
||
const commander = require('../'); | ||
const program = new commander.Command(); | ||
|
||
program | ||
.storeOptionsAsProperties(false); // <--- change behaviour | ||
|
||
program | ||
.name('my-program-name') | ||
.option('-n,--name <name>'); | ||
|
||
program | ||
.command('show') | ||
.option('-a,--action <action>') | ||
.action((cmd) => { | ||
const options = cmd.opts(); // <--- use opts to access option values | ||
console.log(options.action); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
const programOptions = program.opts(); // <--- use opts to access option values | ||
console.log(programOptions.name); |
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,35 @@ | ||
#!/usr/bin/env node | ||
|
||
// The original and default behaviour is that the option values are stored | ||
// as properties on the program (Command). The action handler is passed a | ||
// command object (Command) with the options values also stored as properties. | ||
// This is very convenient to code, but the downside is possible clashes with | ||
// existing properties of Command. | ||
// | ||
// Example output, note the issues in the first call: | ||
// | ||
// $ node storeOptionsAsProperties-problem.js show | ||
// [Function] | ||
// [Function] | ||
// | ||
// $ node storeOptionsAsProperties-problem.js --name foo show --action jump | ||
// jump | ||
// foo | ||
|
||
const commander = require('../'); | ||
const program = new commander.Command(); | ||
|
||
program | ||
.name('my-program-name') | ||
.option('-n,--name <name>'); // Oops, clash with .name() | ||
|
||
program | ||
.command('show') | ||
.option('-a,--action <action>') // Oops, clash with .action() | ||
.action((cmd) => { | ||
console.log(cmd.action); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
console.log(program.name); |
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
Oops, something went wrong.