custom-command-class.js
This file
Path
examples/custom-command-class.js
50 lines · 1.5 KiB · JavaScript
Last changed by
Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
History of this fileTake it
createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
lines 1–5why, one step back1#!/usr/bin/env node23// const commander = require('commander'); // (normal include)4const commander = require('../'); // include commander in git clone of commander repo5Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63
line 6why, one step back6// Use a class override to customise the command and its subcommands.createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
line 7why, one step back7Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
line 8why, one step back8class CommandWithTrace extends commander.Command {createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
line 9why, one step back9 createCommand(name) {Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
lines 10–13why, one step back10 const cmd = new CommandWithTrace(name);11 // Add an option to subcommands created using `.command()`12 cmd.option('-t, --trace', 'display extra information when run command');13 return cmd;createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
lines 14–16why, one step back14 }15};16Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
line 17why, one step back17function inpectCommand(command) {Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63
line 18why, one step back18 // The option value is stored as property on command because we called .storeOptionsAsProperties()Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
line 19why, one step back19 console.log(`Called '${command.name()}'`);Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63
line 20why, one step back20 console.log(`args: ${command.args}`);Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
line 21why, one step back21 console.log('opts: %o', command.opts());Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63
line 22why, one step back22};createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
line 23why, one step back23Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
lines 24–27why, one step back24const program = new CommandWithTrace('program')25 .option('-v, ---verbose')26 .action((options, command) => {27 inpectCommand(command);createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
lines 28–29why, one step back28 });29Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63
line 30why, one step back30programMake new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
lines 31–34why, one step back31 .command('serve [params...]')32 .option('-p, --port <number>', 'port number')33 .action((params, options, command) => {34 inpectCommand(command);Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63
lines 35–36why, one step back35 });36Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
lines 37–42why, one step back37program38 .command('build <target>')39 .action((buildTarget, options, command) => {40 inpectCommand(command);41 });42createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
lines 43–45why, one step back43program.parse();4445// Try the following:Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4
lines 46–50why, one step back46// node custom-command-class.js --help47// node custom-command-class.js serve --help48// node custom-command-class.js serve -t -p 80 a b c49// node custom-command-class.js build --help50// node custom-command-class.js build --trace foo