Skip to content
Myr

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 file

Take it

createCommand factory routine (#1191)John Geebefore adoption8c3dd6f

lines 1–36why, one step back
1#!/usr/bin/env node
23// const commander = require('commander'); // (normal include)4const commander = require('../'); // include commander in git clone of commander repo56// Override createCommand directly to customise subcommands,7// in this example by adding --debug option.89const program = commander.createCommand();1011// Customise subcommand creation12program.createCommand = (name) => {13 const cmd = commander.createCommand(name);14 cmd.option('-d,--debug', 'output options');15 return cmd;16};1718program19 .command('serve')20 .option('--port <port-number>', 'specify port number', 80)21 .action((cmd) => {22 if (cmd.debug) {23 console.log('Options:');24 console.log(cmd.opts());25 console.log();26 }2728 console.log(`Start serve on port ${cmd.port}`);29 });3031program.parse();3233// Try the following:34// node custom-command-function.js help serve35// node custom-command-function.js serve --debug36// node custom-command-function.js serve --debug --port 8080