sindresorhus/meow

Support `required` flags #51

blond posted onGitHub

Some options must always be specified.

I want to have a way to specify the required options in API.

Example

const cli = meow(`
    Usage
      $ process

    Options
      -i, --input   Input file
      -o, --output  Output file

    Examples
      $ process --input=lib/file.js --output=dest/file.js
`, {
    required: ['input', 'output']
});

A program without required options must quit with an error message.

$ process --output=dest/file.js

Missing required option:
    --input, -i  Input file

In most cases I would handle this in the API. Any reason you need to do it in the CLI?

posted by kevva over 8 years ago

I agree with @kevva. I believe this should be handled by the code that is called. That allows the most flexibility with informative custom error messages.

posted by SamVerschueren over 8 years ago

Going to close for now. If there are some good arguments for having this as an option we might re-open it again.

posted by kevva over 7 years ago

I'm gonna reopen this as I think it could be useful for CLI only tools (ones that don't have an API).

It could be specified like:

flags: {
    rainbow: {
        type: 'boolean',
        alias: 'r',
        required: true
    }
}

We should be clear in the docs that API level option validation should be preferred.

The required option could also accept a function that could receive the arguments and do its own check whether it's required. For example, if the flag is only required when another flag is specified.

posted by sindresorhus over 7 years ago

@issuehuntfest has funded $60.00 to this issue. See it on IssueHunt

posted by IssueHuntBot about 6 years ago

If a required field takes a function, should the expected return value of the function be a boolean? A quick example could be helpful as I am currently working in this.

Thanks

posted by HackAfro about 6 years ago

@hackafro has submitted a pull request. See it on IssueHunt

posted by IssueHuntBot about 6 years ago

should the expected return value of the function be a boolean?

Yes

A quick example could be helpful as I am currently working in this.

{
    flags: {
        rainbow: {
            type: 'boolean',
            required: (input, flags) => flags.unicorn === '🦄'
        }
    }
}

Note, I'm not exactly sure what the function arguments should be. I'm open to suggestions there. I guess the input and flags. Anything else?


I also think we should call the option isRequired, not required.

posted by sindresorhus about 6 years ago

If anyone wants to work on this, see the previous attempt here and the feedback given: https://github.com/sindresorhus/meow/pull/110

posted by sindresorhus about 5 years ago

@sindresorhus has rewarded $54.00 to @sbencoding. See it on IssueHunt

  • :moneybag: Total deposit: $60.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $6.00
posted by issuehunt-app[bot] almost 5 years ago

Can we get this, but for input (non-flag arguments)?

posted by NSExceptional about 3 years ago

Fund this Issue

$60.00
Rewarded

Rewarded pull request

Other pull requests

Recent activities

sbencoding was rewarded by sindresorhus for sindresorhus/meow# 51
almost 5 years ago
sbencoding submitted an output to  sindresorhus/ meow# 51
about 5 years ago