node.jscommand-line-argumentsrequired-fieldnode-commander

commander.js : how to specify required cli argument


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?


Solution

  • 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')