Skip to content

Commit

Permalink
Use custom command example to show revert to Commander v6 behaviours (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn authored Dec 21, 2020
1 parent 20cef03 commit 75c2a63
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 34 additions & 20 deletions examples/custom-command-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <number>')
.action(() => {
inspectCommand(program, 'port');
});

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}`);
.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
36 changes: 0 additions & 36 deletions examples/custom-command-function.js

This file was deleted.

0 comments on commit 75c2a63

Please sign in to comment.