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

Way to disable built-in default values #53

Open
julien-f opened this issue Apr 21, 2021 · 4 comments
Open

Way to disable built-in default values #53

julien-f opened this issue Apr 21, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@julien-f
Copy link

getopts assigns default values to string (''), number (0) and boolean (false) options.

Issues:

  1. Not compatible with JS default values:
const { foo = 'bar' } = getopts(argv, { string: [ 'foo' ] });
  1. Cannot distinguish between explicit options and default values:
const { baz } = getopts(argv, { string: [ 'baz' ] });
if (baz === undefined) {
  throw new Error('--baz must be passed explicitely');
}

With modern JS syntaxes like destructuring, default values and the nullish coalescing operator (??), I don't see much use default values being handled by getopts in the future.

In the meantime, a simple option like builtInDefaults: false would be good enough to cover my use case 🙂

What's your opinion on this?

@jorgebucaran jorgebucaran added the enhancement New feature or request label Apr 23, 2021
@jorgebucaran
Copy link
Owner

Just a couple of issues to be aware of: opts.string allows you to treat contiguous characters as a single value and not individual options.

getopts(["-atabc"], {
  string: ["t"],
}) //=> { _:[], a:true, t:"abc" }

Similarly, opts.boolean says we should parse the next argument as an operand instead of as a value.

getopts(["-t", "alpha"], {
  boolean: ["t"],
}) //=> { _:["alpha"], t:true }

As long as we don't interfere with that, I'm down for it. We could even make it the default behavior since builtInDefaults: true may not be too useful in the future as you say.

@julien-f
Copy link
Author

I just took a quick glance, but putting an if (builtInDefaults) around these two lines appear to do the job:

getopts/index.js

Lines 189 to 190 in 35dfad8

for (let key in bools) if (out[key] === undefined) out[key] = false
for (let key in strings) if (out[key] === undefined) out[key] = ""

@jorgebucaran
Copy link
Owner

Looks like a clean solution! Do you happen to know what other parsers out there, e.g., yargs do? If a similar feature/option already exists in another parser it may be worthwhile naming ours the same or similarly (although not necessarily).

@julien-f
Copy link
Author

julien-f commented May 2, 2021

After a quick test, it seems that by default, yargs does not set default values to boolean/string/number options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants