I have a cli script that accepts commandline parameters.
For the pages parameter I would like the behaviour to be:
> script.js
-> pages parameter not set. don't process pages
> script.js -p 100
-> pages parameter set with value 100: process 100 pages
> script.js -p
-> pages parameter set without value: process infinite (however much available) pages
In other words: I want to give an optional parameter a default value. The problem is that I can't figure out how to distinguish between the situation where it's set by the user without value and when it's not set.
When defined like this:
var argv = require('yargs')
// omitted code
.alias('p', 'pages')
.describe('p', 'number of pages; infinite by default')
.default('p', -1)
.number('p')
argv.pages
is always -1
whether I set -p
or not
And when defined like this:
.alias('p', 'pages')
.describe('p', 'number of pages; infinite by default')
.number('p')
typeof argv.pages
is always undefined
, whether I set -p
or not
How can I give a default value while treating the parameter as an optional argument; so, no default when it's not set and only a default when it's set without value.
(of course there is a workaround : I could manually parse the arguments by looping through process.argv, but I want to avoid that as that defeats the purpose of using yargs)