Use custom command example to show revert to Commander v6 behaviours (#1421)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent 20cef03 · Browse files · 3 files·+35 added−57 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+35 added−57 removed
- other:+35 added−57 removed
Provenance
- Commit
75c2a63- Tree
46d93b7- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
20cef03
3 files changed+35 added−57 removed
modified:examples/custom-command-class.js+34 added-20 removed
12345676789910111213141516171819111213202122231718192021222324252624252627282930283132333435363738394041424344454647483435364950#!/usr/bin/env node
// const commander = require('commander'); // (normal include)const commander = require('../'); // include commander in git clone of commander repo
// Use a class override of createCommand to customise subcommands,// in this example by adding --debug option.// Use a class override to customise the command and its subcommands.//// Configuring the command for compatibility with Commander v6 defaults behaviours.
class MyCommand extends commander.Command {class Command6 extends commander.Command { constructor(name) { super(name);
// Revert to Commander v6 behaviours. this.storeOptionsAsProperties(); this.allowExcessArguments(); }
createCommand(name) { const cmd = new MyCommand(name); cmd.option('-d,--debug', 'output options'); return cmd; return new Command6(name); }};
const program = new MyCommand();program .command('serve') .option('--port <port-number>', 'specify port number', 80) .action((options) => { if (options.debug) { console.log('Options:'); console.log(options); console.log(); }function inspectCommand(command, optionName) { // The option value is stored as property on command because we called .storeOptionsAsProperties() console.log(`Inspecting '${command.name()}'`); console.log(`option '${optionName}': ${command[optionName]}`); console.log(`args: ${command.args}`);};
console.log(`Start serve on port ${options.port}`);const program = new Command6('program') .option('-p, --port <number>') .action(() => { inspectCommand(program, 'port'); });
program .command('sub') .option('-d, --debug') .action((options, command) => { inspectCommand(command, 'debug'); });
program.parse();
// We can pass excess arguments without an error as we called .allowExcessArguments()//// Try the following:// node custom-command-class.js help serve// node custom-command-class.js serve --debug// node custom-command-class.js serve --debug --port 8080// node custom-command-class.js --port 80 extra arguments// node custom-command-class.js sub --debug extra argumentsdeleted:examples/custom-command-function.js-36 removed
123456789101112131415161718192021222324252627282930313233343536#!/usr/bin/env node
// const commander = require('commander'); // (normal include)const commander = require('../'); // include commander in git clone of commander repo
// Override createCommand directly to customise subcommands,// in this example by adding --debug option.
const program = commander.createCommand();
// Customise subcommand creationprogram.createCommand = (name) => { const cmd = commander.createCommand(name); cmd.option('-d,--debug', 'output options'); return cmd;};
program .command('serve') .option('--port <port-number>', 'specify port number', 80) .action((options) => { if (options.debug) { console.log('Options:'); console.log(options); console.log(); }
console.log(`Start serve on port ${options.port}`); });
program.parse();
// Try the following:// node custom-command-function.js help serve// node custom-command-function.js serve --debug// node custom-command-function.js serve --debug --port 8080modified:Readme.md+1 added-1 removed
720 unmodified lines721 ```722 723 `createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally724 when creating subcommands using `.command()`, and you may override it to725-customise the new subcommand (examples using [subclass](./examples/custom-command-class.js) and [function](./examples/custom-command-function.js)).725+customise the new subcommand (example file [custom-command-class.js](./examples/custom-command-class.js)).726 727 ### Import into ECMAScript Module728 729 Commander is currently a CommonJS package, and the default export can be imported into an ES Module:148 unmodified lines