custom-command-class.js
This file
Path
examples/custom-command-class.js
46 lines · 1.4 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than 78e335f point at it.
Fix minor typo (#1579)John Geebefore adoption78e335f
line 1why, one step back1#!/usr/bin/env nodeUse standard import in examples (#2168)John Geebefore adoption41b12cf
line 2why, one step back2const commander = require('commander');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 3–12why, one step back34// Use a class override to customise the command and its subcommands.56class CommandWithTrace extends commander.Command {7 createCommand(name) {8 const cmd = new CommandWithTrace(name);9 // Add an option to subcommands created using `.command()`10 cmd.option('-t, --trace', 'display extra information when run command');11 return cmd;12 }ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e
line 13why, one step back13}Fix minor typo (#1579)John Geebefore adoption78e335f
lines 14–19why, one step back1415function inpectCommand(command) {16 // The option value is stored as property on command because we called .storeOptionsAsProperties()17 console.log(`Called '${command.name()}'`);18 console.log(`args: ${command.args}`);19 console.log('opts: %o', command.opts());ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e
line 20why, one step back20}Fix minor typo (#1579)John Geebefore adoption78e335f
lines 21–34why, one step back2122const program = new CommandWithTrace('program')23 .option('-v, ---verbose')24 .action((options, command) => {25 inpectCommand(command);26 });2728program29 .command('serve [params...]')30 .option('-p, --port <number>', 'port number')31 .action((params, options, command) => {32 inpectCommand(command);33 });34Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 35–37why, one step back35program.command('build <target>').action((buildTarget, options, command) => {36 inpectCommand(command);37});Fix minor typo (#1579)John Geebefore adoption78e335f
lines 38–46why, one step back3839program.parse();4041// Try the following:42// node custom-command-class.js --help43// node custom-command-class.js serve --help44// node custom-command-class.js serve -t -p 80 a b c45// node custom-command-class.js build --help46// node custom-command-class.js build --trace foo