commander.js icon indicating copy to clipboard operation
commander.js copied to clipboard

Understanding basic usage

Open rvnath opened this issue 10 years ago • 2 comments

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?

rvnath avatar May 01 '16 11:05 rvnath

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);

pedrorfernandes avatar May 01 '16 17:05 pedrorfernandes

Got it. I was missing the [port] part in square brackets.

rvnath avatar May 02 '16 18:05 rvnath