-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
boolean silently converts string to true #39
Comments
That's surprising; both forms (equals sign, or space) are standard for double-dashed args, and I'd strongly expect and prefer always using the In other words, both of those should be indistinguishable imo, and it's a bug that they aren't. |
cc @shadowspawn for your thoughts |
--silent A B is IMO correctly doing argv.silent=true (if defined boolean)and argv._ = ["A","B"] |
if |
@ljharb, exactly, when set as boolean, --x A and --x=A behave differently. I understand that if set as boolean it can't take a value, what is non-intuitive is that it does silently discard the string if --x=A but process it as a separate args._. if --x A and what is the most difficult for me is that minimist allows me to identify and raise an alert if --x A, but doesn't allow me to identify that a user wrongly wrote --x=A (where x being boolean) |
ahh, i see what you mean. I would expect |
Minimist is not the right tool for that job. But it is fun exploring anyway! 😆
Short version: no. In more common cases, the behaviour with boolean options is fairly self-consistent. Assigning a value to a boolean option on the command-line means Minimist silently processes it. var parse = require('minimist');
const argv = parse(process.argv.slice(2), {
boolean: 'flag',
string: 'w'
});
console.log(argv);
|
The "silent" part is what feels like a bug to me. It should either reject it or preserve "other" somewhere. |
(Thinking about that...) |
A somewhat related issue it might be enough to offer an option mistyped = (key, value) => {} working similarly to unknown ? |
Thinking about example Possible approaches, and my reservations, leading to a more radical suggestion.
However, this would be a change in existing behaviour to make it possible to detect a special case, without handling the special case. So cost/benefit ratio is dubious.
I feel this might address the underlying problem more directly, which is validating the user supplied arguments with the author supplied configuration. Edit: thinking about API, would be |
I like adding a strict mode, which in a future semver-major could be the default. |
strict:
correction: throw for unknown options, which includes when |
I like it, going back to my experiments, the last bit that isn't yet covered is the number of argv._ arguments
What about an optional parameter to .strict(_length: Integer) ? and throw an Error if argv._.length !== _length OTH, it'd be saving 4 lines, it probably fits in the cosmetic category. |
Good coverage of possible validations! I think past diminishing returns for now and easy for author to check, both against what is expected and to describe what is missing. (e.g. "please specify one or more filenames"). Could revisit after solidify what strict does. For interest:
It is reasonably common for there to be a variadic positional, whether 0-or-more or 1-or-more, so a single length is not enough. For clarity and future expansion I suggest if there are any options, they should be named like say: |
Thinking about strict mode. I think an error for unknown options leads to wanting a more convenient way to mark options as known, but not string or boolean which are special cases. I am wondering about an Touched on indirectly in: #37 (comment) |
Everything is a string unless otherwise specified - string isn't a special case, it's the primary/only case in a shell. |
Or perhaps const parse = require('minimist');
try {
const result = parse(
process.argv.slice(2), {
strict: true,
string: ['str'],
boolean: ['bool'],
known: ['num'],
}
);
console.log(result);
} catch (err) {
console.error(err.message);
}
|
Regarding adding auto/known: now thinking |
Unless I'm missing something, there is the case of --answer = 42 ie. if a param isn't specified as a string or boolean and that it converts to a number, it's parsed as int. My understanding is that it wouldn't be possible in the strict mode (at least without introducing a "number":["answer"]) |
Yes, that's the point of "strict" - nothing's implicit, and numbers are only numbers if you specify them as such. |
but how to specify that a param is a number? you can only define string and boolean as explicit params, not numbers, right? |
Yes indeed. To support a transition from existing non-strict usage expecting number conversions, I am experimenting with adding |
Hi,
me again, in my quest to idiot proof the cli ;)
so I have two params, one foo and one bar
if I run it with
I get
as expected
however, if I run it with
I get
so a --bar=string is silently converted into --bar = true
is there an option to catch these conversions ?
The text was updated successfully, but these errors were encountered: