Commander.js: What's the meaning of 'true' here?

Created on 8 Mar 2019  路  8Comments  路  Source: tj/commander.js

I follow the sample and give a wrong drink

$ cat a.js
var program = require('commander');

program
  .version('0.1.0')
  .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
  .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
  .parse(process.argv);

console.log(' size: %j', program.size);
console.log(' drink: %j', program.drink);

$ node a.js
 size: "medium"
 drink: undefined

$ node a.js -d abc
 size: "medium"
 drink: true

What's the meaning of true here?

How do I write code to identify, if drink's value is not coke|pepsi|izze, exit(-1).

Most helpful comment

Thanks, @kira1928 馃憤

I never think to write a function (parseDrink) in the part of program.option . I currently add the test to program.drink

All 8 comments

The true means drink was specified as an option, but with no (valid) value. Like calling:

node a.js -d

How do I write code to identify, if drink's value is not coke|pepsi|izze, exit(-1).

I suggest you don't use the pattern in the .option call and instead check the option value yourself after calling parse. I also suggest putting the allowed values in the description so it appears in the help.

should we change true to something else? It really confused me. True makes the feeling, something happened and it is correct.

I would prefer to exit directly, if the input of drink is not in the list. Whatever it shouldn't be true, even undefined is better than true.

If any users of regular expression option read this thread, I am interested in what your code looks like.

@ozbillwang

How do I write code to identify, if drink's value is not coke|pepsi|izze, exit(-1).

How about this one:

var program = require('commander');

var parseDrink = (function() {
  var regex = /^(coke|pepsi|izze)$/i;
  return function(value) {
    if (regex.test(value)) {
      return value;
    } else {
      console.error(`invalid param ${value}`);
      process.exit(-1);
    }
  };
})();

program
  .version("0.1.0")
  .option("-s --size <size>", "Pizza size", /^(large|medium|small)$/i, "medium")
  .option("-d --drink [drink]", "Drink", parseDrink)
  .parse(process.argv);

console.log(" size: %j", program.size);
console.log(" drink: %j", program.drink);
$ node a.js
size: "medium"
drink: undefined
$ node a.js -d abc
invalid param abc
$ echo $?
255
$ node a.js -d
size: "medium"
drink: true
$ node a.js -d coke
size: "medium"
drink: "coke"

Thanks, @kira1928 馃憤

I never think to write a function (parseDrink) in the part of program.option . I currently add the test to program.drink

Found another related issue: #708

Closing this in favour of #708

Thank you for your contributions.

Short version: I have downgraded the regular expression support and removed it from the README (!).

See: https://github.com/tj/commander.js/issues/708#issuecomment-496123000

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mathiasbynens picture mathiasbynens  路  3Comments

shadowspawn picture shadowspawn  路  4Comments

hossamelmansy picture hossamelmansy  路  4Comments

oknoorap picture oknoorap  路  4Comments

RoXioTD picture RoXioTD  路  4Comments