From 75c2a63bafc99cbd8d5815c91e131f5cef7aac67 Mon Sep 17 00:00:00 2001 From: John Gee Date: Tue, 22 Dec 2020 09:44:54 +1300 Subject: [PATCH] Use custom command example to show revert to Commander v6 behaviours (#1421) --- Readme.md | 2 +- examples/custom-command-class.js | 54 ++++++++++++++++++----------- examples/custom-command-function.js | 36 ------------------- 3 files changed, 35 insertions(+), 57 deletions(-) delete mode 100755 examples/custom-command-function.js diff --git a/Readme.md b/Readme.md index c9c6a8c33..476700e4f 100644 --- a/Readme.md +++ b/Readme.md @@ -722,7 +722,7 @@ const program = createCommand(); `createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally when creating subcommands using `.command()`, and you may override it to -customise the new subcommand (examples using [subclass](./examples/custom-command-class.js) and [function](./examples/custom-command-function.js)). +customise the new subcommand (example file [custom-command-class.js](./examples/custom-command-class.js)). ### Import into ECMAScript Module diff --git a/examples/custom-command-class.js b/examples/custom-command-class.js index e76e7a880..dc2cd6c3d 100755 --- a/examples/custom-command-class.js +++ b/examples/custom-command-class.js @@ -3,34 +3,48 @@ // 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 Command6 extends commander.Command { + constructor(name) { + super(name); + + // Revert to Commander v6 behaviours. + this.storeOptionsAsProperties(); + this.allowExcessArguments(); + } -class MyCommand extends commander.Command { createCommand(name) { - const cmd = new MyCommand(name); - cmd.option('-d,--debug', 'output options'); - return cmd; + return new Command6(name); } }; -const program = new MyCommand(); +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}`); +}; + +const program = new Command6('program') + .option('-p, --port ') + .action(() => { + inspectCommand(program, 'port'); + }); + program - .command('serve') - .option('--port ', '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}`); + .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 arguments diff --git a/examples/custom-command-function.js b/examples/custom-command-function.js deleted file mode 100755 index af3125085..000000000 --- a/examples/custom-command-function.js +++ /dev/null @@ -1,36 +0,0 @@ -#!/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 creation -program.createCommand = (name) => { - const cmd = commander.createCommand(name); - cmd.option('-d,--debug', 'output options'); - return cmd; -}; - -program - .command('serve') - .option('--port ', '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 8080