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

Support required flags #51

Closed
blond opened this issue Oct 13, 2016 · 11 comments · Fixed by #141
Closed

Support required flags #51

blond opened this issue Oct 13, 2016 · 11 comments · Fixed by #141
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted
Milestone

Comments

@blond
Copy link

blond commented Oct 13, 2016

Issuehunt badges

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

IssueHunt Summary

sbencoding sbencoding has been rewarded.

Backers (Total: $60.00)

Submitted pull Requests


Tips

@kevva
Copy link
Contributor

kevva commented Oct 14, 2016

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

@SamVerschueren
Copy link
Contributor

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.

@kevva
Copy link
Contributor

kevva commented Sep 23, 2017

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

@kevva kevva closed this as completed Sep 23, 2017
@sindresorhus
Copy link
Owner

sindresorhus commented Nov 26, 2017

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.

@sindresorhus sindresorhus reopened this Nov 26, 2017
@sindresorhus sindresorhus changed the title Required Options Support required flags Nov 26, 2017
@sindresorhus sindresorhus added this to the 6.0.0 milestone Nov 5, 2018
@IssueHuntBot
Copy link

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

@HackAfro
Copy link

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

@IssueHuntBot
Copy link

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

@sindresorhus
Copy link
Owner

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.

@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label May 10, 2019
@sindresorhus
Copy link
Owner

If anyone wants to work on this, see the previous attempt here and the feedback given: #110

@issuehunt-oss
Copy link

issuehunt-oss bot commented May 7, 2020

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

  • 💰 Total deposit: $60.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $6.00

@issuehunt-oss issuehunt-oss bot added 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt and removed 💵 Funded on Issuehunt This issue has been funded on Issuehunt labels May 7, 2020
@NSExceptional
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants