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

How to treat all arguments with one hyphen and without equal signs as boolean? #75

Open
EstebanMacanek opened this issue Jan 24, 2025 · 3 comments

Comments

@EstebanMacanek
Copy link

EstebanMacanek commented Jan 24, 2025

According to the README, opts.boolean doesn't work with -f.

This would work, but is there another way that doesn't imply patching?

diff --git a/index.js b/index.js
index 78cafa8..9fda7a1 100644
--- a/index.js
+++ b/index.js
@@ -228,6 +228,7 @@ module.exports = function (args, opts) {
                                        args[i + 1]
                                        && !(/^(-|--)[^-]/).test(args[i + 1])
                                        && !isBooleanKey(key)
+                                       && !flags.allBools
                                ) {
                                        setArg(key, args[i + 1], arg);
                                        i += 1;

@shadowspawn
Copy link
Collaborator

There is not a configuration to change the treatment of automatic short flags.

@shadowspawn
Copy link
Collaborator

Short version: why do you want this?

Long Version

Reference: opts.booleanBoth in #76

In general, I don't want lots of configuration options. For comparison, yarns-parser comes from the same optimist codebase as minimist, and has preserved and/or added lots of configuration options. That isn't the direction I want to follow.

The decision as to whether -f bar is an option and its value, or a "boolean" option and a positional, is interesting to me because it is a big decision in any auto-detect option parser. There are a number of libraries that base their parsing on top of minimist (like Meow) and I have not seen requests for this configuration. I am guessing they focused on improving developer and user experience, and just went with this parsing behaviour from minimist.

The design specification for util:parseArgs in node went the other way. parseArgs assumes auto-detected options are boolean options. parseArgs is loosely of a similar low-level to minimist, except in particular it is strict by default. (docs)

I understand minimist does not allow configuration so auto-detected options with one hyphen and without equal signs are treated as boolean. The key question is, why is this important? What is your use case to justify adding the configuration?

@shadowspawn
Copy link
Collaborator

An aside. I was a little surprised when I first saw the existing boolean:true only affects long options, but is has been that way from when first added over 10 years ago: 450a97f

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

No branches or pull requests

2 participants