commander.js : how to specify required cli argument

asking picture asking · Jun 12, 2014 · Viewed 8.7k times · Source

I'm using commander.js package for parsing command-line arguments: I'd like to make a flag non-optional, the API and tests in the git repo loosely mention making a flag required, but I usually need to be hit over the head with instructions.

Is it actually possible, and will the script throw if the requirement is not met?

Answer

vinayr picture vinayr · Jun 12, 2014

I guess this is not supported by commander.js https://github.com/visionmedia/commander.js/issues/44

But you can do something like this in your program -

if (!program.myoption) 
  throw new Error('--myoption required')