commander.js
commander.js copied to clipboard
Understanding basic usage
I have never been so naive. But I am unable to understand how to use this program for a very simple use case. I wrote a piece of code to use the commandline args using this module as below:
var program = require('commander');
program.version('1.0.0')
.option('-p, --port', 'Registry Server port', 'port')
.option('-h, --host', 'Registry Server host')
.option('-c, --component', 'Registry Component Name to operate upon')
.option('-o, --operation', 'What operation to perform [start|stop]')
.parse(process.argv);
if (program.host) console.log('host:', program.host);
if (program.port) console.log("port:", program.port);
if (program.component) console.log("component:", program.component);
if (program.operation) console.log("operation:", program.operation);
The output shows like this:
node com.js -p 1234 -h local -c ponent -o shutdown host: true port: port component: true operation: true
I was expecting that program.host, program.port etc., will contain the actual values given on command line, instead of the value "true". Am I missing something here?
Try this:
program.version('1.0.0')
.option('-p, --port [port]', 'Registry Server port', 'port')
.option('-h, --host [host]', 'Registry Server host')
.option('-c, --component [component]', 'Registry Component Name to operate upon')
.option('-o, --operation [operation]', 'What operation to perform [start|stop]')
.parse(process.argv);
Got it. I was missing the [port] part in square brackets.