Skip to content
Myr

custom-command-class.js

This file

Path

examples/custom-command-class.js

36 lines · 970 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// Use a class override of createCommand to customise subcommands,7// in this example by adding --debug option.89class MyCommand extends commander.Command {10 createCommand(name) {11 const cmd = new MyCommand(name);12 cmd.option('-d,--debug', 'output options');13 return cmd;14 }15};1617const program = new MyCommand();18program19 .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-class.js help serve35// node custom-command-class.js serve --debug36// node custom-command-class.js serve --debug --port 8080