13

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?

Jezzamon
  • 1,453
  • 1
  • 15
  • 27
asking
  • 1,435
  • 3
  • 13
  • 21

2 Answers2

22

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')
vinayr
  • 11,026
  • 3
  • 46
  • 42
6

It depends on how you write the arguments.

  • With <> it is required.
  • With [] it is not required.

See exemple.

const commander = require('commander')
    , program = new commander.Command()

program
   .command('autorecord')
   .argument('<title>', 'Title and file name of record') // is required
   .argument('[type]', 'Type of record. "undefined" by default') // is not required