custom-command-function.js
This file
Path
examples/custom-command-function.js
36 lines · 985 bytes · JavaScript
Last changed by
createCommand factory routine (#1191)John Geebefore adoption8c3dd6f
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637#!/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 creationprogram.createCommand = (name) => { const cmd = commander.createCommand(name); cmd.option('-d,--debug', 'output options'); return cmd;};
program .command('serve') .option('--port <port-number>', 'specify port number', 80) .action((cmd) => { if (cmd.debug) { console.log('Options:'); console.log(cmd.opts()); console.log(); }
console.log(`Start serve on port ${cmd.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