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

Add a booleanish option #44

Open
jamestalmage opened this issue Jun 22, 2016 · 2 comments
Open

Add a booleanish option #44

jamestalmage opened this issue Jun 22, 2016 · 2 comments

Comments

@jamestalmage
Copy link
Contributor

jamestalmage commented Jun 22, 2016

This came up while implementing the --debug flag for AVA.

Basically, there are two ways to use the debug flag:

# default port (5858)
ava --debug test.js

# specific port
ava --debug=1234 test.js

# not allowed - this won't work
ava --debug 1234 test.js

The reason you can't use the last one is because it's ambiguous whether you meant "run two test files with the default port" or "run one file with port 1234".

When you specify the flag as boolean:

meow(help, {
  boolean: [
    'debug'
  ]
});

You get true or false regardless. However you must specify it as a boolean, or this:

ava --debug test.js

To be interpreted as:

{
  flags: {
    debug: 'test.js'
  }
}

My solution was to set debug up as a boolean, then if I got true, reparse argv to find the port

I think it might be interesting to allow this:

meow(helper, {
  booleanish: {
    debug: {
      true: 5858,
      false: false,
      parse: function (value) {
        value = parseInt(value, 10);

        if (isNaN(value)) {
          throw new Error('must be a number');
        }

        return value;
      }
    }
  }
});

The basic usage of the booleanish option is:

  • true: What gets returned if it is used as a boolean flag --debug
  • false: What gets returned if no flag appears (or --no-debug is used).
  • parse: If they use the --debug=XXX - this function is used to parse the XXX part. It can throw an Error for invalid values, and meow should print the Error message, and exit.

This would greatly ease the creation of dual purpose flags that can be used either as simple booleans, or be supplied with a config value.

@sindresorhus
Copy link
Owner

I like the idea in general and I've had the same problem previously too, but not entirely sold on this exact implementation. Why not just let the user specify an option as both string and boolean in the minimist options and handle it accordingly? Or maybe introduce a new type in the minimist options called any (I prefer it over booleanish):

const cli = meow('help', {
  any: ['debug']
});

parse seems more general and could be added independently of this change, as it could be useful for the string type too.

false: What gets returned if no flag appears (or --no-debug is used).

No flag and --no-debug are different. No flag returns undefined while --no-debug returns false. This distinction is usually not important, but can be.

// @kevva @SamVerschueren

@Mouvedia
Copy link

Why not just let the user specify an option as both string and boolean in the minimist options and handle it accordingly?

Is this supported?

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

3 participants