custom-command-class.js
This file
Path
examples/custom-command-class.js
50 lines · 1.5 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 6c06528 point at it.
Add getOptionValue and setOptionValue (#1521)John Geebefore adoption6c06528
lines 1–50why, one step back1#!/usr/bin/env node23// const commander = require('commander'); // (normal include)4const commander = require('../'); // include commander in git clone of commander repo56// Use a class override to customise the command and its subcommands.78class CommandWithTrace extends commander.Command {9 createCommand(name) {10 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;14 }15};1617function inpectCommand(command) {18 // The option value is stored as property on command because we called .storeOptionsAsProperties()19 console.log(`Called '${command.name()}'`);20 console.log(`args: ${command.args}`);21 console.log('opts: %o', command.opts());22};2324const program = new CommandWithTrace('program')25 .option('-v, ---verbose')26 .action((options, command) => {27 inpectCommand(command);28 });2930program31 .command('serve [params...]')32 .option('-p, --port <number>', 'port number')33 .action((params, options, command) => {34 inpectCommand(command);35 });3637program38 .command('build <target>')39 .action((buildTarget, options, command) => {40 inpectCommand(command);41 });4243program.parse();4445// Try the following:46// 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