leo / args

Toolkit for building command line interfaces
https://npmjs.com/args
MIT License
461 stars 30 forks source link

Feature: Strict type parsing mode #133

Open DRSDavidSoft opened 5 years ago

DRSDavidSoft commented 5 years ago

Feature Request

I would like my program to throw an error if the user tries to enter something other than an integer, e.g.:

app.js --port NOT_AN_INT

should return an Exception instead of falling back to the default.

Everything works great on either:

Workaround

Currently, I am forced to handle the flag values without the built-in default value parameter, i.e.:

  .option('port', 'foo bar', 8080)

becomes:

  .option('port', 'foo bar')

That way, I can validate the flag value myself, which is suboptimal. I would've preferred if args would take care of that for me, too :)

Possible new feature

Can I suggest implementing any of these methods, please?

Method A (Global options)

Either:

Method B (Per-flag)

preferred method

  .option('port', 'foo bar', 8080, null, { strict: true })

Example

$ app.js --port abracadabra
User error: please enter a valid number for the `port` argument.

$ app.js --port 8081
app.js: Ready, port 8081 was accepted.

I hope you would consider this, thanks :)