Commander.js: My options seem to be undefined

Created on 25 May 2016  路  10Comments  路  Source: tj/commander.js

Trying to use commander like so:

const program = require('commander'),
      Excel     = require('exceljs'),
      colors    = require('colors/safe'),
      inquirer  = require('inquirer'),
      async    = require('async');

var fileName;

program
  .usage('[options] <file>')
  .option('-n', '--po-number <n>', 'The PO number to start at')
  .action(function(options, file) {
    fileName = file;
  })
  .parse(process.argv);

console.log(program.poNumber); // undefined


./index.js -n 636 ~/Docs/POList.xlsx

But program.poNumber is undefined

I've looked at numerous tutorials / docs on github but can't seem to figure out why it's undefined.

Node version 5.4.1

Most helpful comment

this is pretty annoying, if we are not able to pass strings to option, then this library is completely worthless

All 10 comments

馃憤

I'm having the same problem, my options suddenly just get swallowed and aren't available on the program object anymore

Same problem. I'm having an issue with options like -d, --domain and -f, --filename. Not sure why...

@Gacnt your example has an error. The option flags are not separate arguments, it's one argument with the comma in the string. It should be:

  .option('-n, --po-number <n>', 'The PO number to start at')

@jasonbarone using -d, --domain <d> works for me, but the short form with argument -d <d> fails

Having same issue. My options object is undefined. I've tried several combinations.

program.command('dev')
  .description('Start the VT frontend suite')
  .option('-p, --port <port>', 'Port number the express server will listen on.')
  .option('-s, --socketPort <socket>', 'Port number the socket server will listen on.')
  .action((env, options) => {
    console.log(options); //undefined
  });

Found a workaround. Options seem to be available in the first argument, env. Not sure if something is broken or of the README just needs to be updated.

program.command('dev')
  .description('Start the VT frontend suite')
  .option('-p, --port <port>', 'Port number the express server will listen on.')
  .option('-s, --socketPort <socket>', 'Port number the socket server will listen on.')
  .action((env, options) => {
    console.log(env.port); // 1337
  });

@tjdavenport you never specified an env command/argument in your example, so options is the first argument.

...  
  .action((options) => {
    console.log(options.port); // 1337
  });

@bitstrider can you explain what you mean by "you never specified an env command/argument in your example"

The way i got this working on my end was adding -- after the npm start
E.g. npm start -- <option>

this is pretty annoying, if we are not able to pass strings to option, then this library is completely worthless

@bitstrider can you explain what you mean by "you never specified an env command/argument in your example"

dev command in the example above is defined without any argument i.e.:

program.command('dev')

So it won't receive any env argument and the first is an options as intended. If the dev command
were defined like:

program.command('dev <env>')

then, yes, it will receive options as the second argument and the first one will be equal to the value provided per dev.

See #914 for the solution

Was this page helpful?
0 / 5 - 0 ratings

Related issues

san-templates picture san-templates  路  5Comments

shadowspawn picture shadowspawn  路  3Comments

oknoorap picture oknoorap  路  4Comments

snitin315 picture snitin315  路  4Comments

mathiasbynens picture mathiasbynens  路  3Comments