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?
The convenient thing about this library is that, commanderJS will create a default help argument for you. There are different options and flavors you can choose from in commanderJS specific options, where you can choose how you want to customize your command. Finally, add process.agrv in the last line, so Commander can parse user inputs.
Here are the node docs on handling command line args: process.argv is an array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
This article provides a comprehensive hands-on for the various option features of Commander. When building a “good-old” command line interface (CLI) with NodeJS, the Commander package is of first choice. Commander offers you many features to design your CLI. The most comprehensive one is using options for a program or command.
process.argv is an array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
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')
It depends on how you write the arguments.
<>
it is required.[]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With