Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for unknown options #25

Closed
mlucool opened this issue Sep 14, 2016 · 6 comments
Closed

Allow for unknown options #25

mlucool opened this issue Sep 14, 2016 · 6 comments

Comments

@mlucool
Copy link

mlucool commented Sep 14, 2016

Would it be possible to allow for unknown options and pass them back in a dict? Something similar in python: https://docs.python.org/dev/library/argparse.html#partial-parsing

@75lb
Copy link
Owner

75lb commented Sep 17, 2016

yes, it's a good question and something other option parsers do.. however, it just doesn't work with this parser.. in particular, it clashes with the defaultOption feature.. for example if you have this option definition:

{ name: 'files', multiple: true, defaultOption: true }

and have this command line scenario:

$ example one.js two.js

then --files (the defaultOption) will gobble up those two filenames.. however, if we allow unknown options and have this command line scenario:

$ example one.js --unknown two.js

then what should happen? Is --unknown a flag? Or should it gobble up two.js (taking it away from --files)? Currently, all options need a definition to avoid non-deterministic confusion like this..

However, i will have a think about it for a future version. It maybe possible to allow unknown options in certain cases (e.g. if no defaultOption was specified). I'm leaving this issue open for further feedback.

@mlucool
Copy link
Author

mlucool commented Sep 17, 2016

Thanks for the quick response! There are multiple ways of doing this. minimist has one approach.

I do agree that default causes problems and because of this, we may need to change how unknown works depending on the mode of default or some other parameter.

For example:
If default was multiple, then it should gobble up all things. In this case, --files would be an array of [one.js, --unknown, two.js]. From there I would grab out the first one (my choice) and pass the rest to minimist (or if there was a similar function here) to understand the extra options. If default was not multiple then the first one could be the default (or the last? settable?) and the rest _unknown. And without a default this is easy :)

To give a concrete use case: I am trying to write a wrapper around another CLI library. I want to do something like mywrapper --foo bar -v libfn --libarg val1 -r
Where --foo and -v are defined, libfn is my default and then I want the rest as unknown. Changing the order (I am torn if I should even allow that) would not change the output mywrapper libfn --libarg val1 -r --foo bar -v would be equivalent.

It's clear an implementation of this could have a lot of corner cases.

@zuanoc
Copy link

zuanoc commented Nov 2, 2016

I have the same issue. It would be nice if we can just skip the unknown parameters.

@75lb
Copy link
Owner

75lb commented Jan 28, 2017

I've implemented a first pass at this feature (partial parsing), it's currently in beta.. to accept unknown options you now pass { partial: true } to the commandLineArgs function.. this is the new method sig (documentation still WIP).

so, to use your example, if we had this script...

const commandLineArgs = require('command-line-args')

const definitions = [
  { name: 'foo', type: String },
  { name: 'verbose', alias: 'v', type: Boolean },
  { name: 'libs', type: String, defaultOption: true }
]

const options = commandLineArgs(definitions, { partial: true })
console.log(options)

... and we installed it as mywrapper, then running this command:

$ mywrapper --foo bar -v libfn --libarg val1 -r

... would output this:

{ foo: 'bar',
  verbose: true,
  libs: 'libfn',
  _unknown: [ '--libarg', 'val1', '-r' ] }

The big changes here are that the script no longer throws with UNKNOWN_OPTION and returns the unknown args in the _unknown property.

Please install the beta and let me know what else needs to happen:

$ npm install command-line-args@^4.0.0-0

I'm considering making partial: true the default mode, not sure yet.

@75lb
Copy link
Owner

75lb commented Jan 31, 2017

Fixed and released in v4.0.0.

@75lb 75lb closed this as completed Jan 31, 2017
@nodkz
Copy link

nodkz commented Feb 3, 2017

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants