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 setting to indicate a flag can be set multiple times #111

Closed
sindresorhus opened this issue Feb 7, 2019 · 11 comments · Fixed by #143
Closed

Add setting to indicate a flag can be set multiple times #111

sindresorhus opened this issue Feb 7, 2019 · 11 comments · Fixed by #143
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented Feb 7, 2019

Issuehunt badges

Currently, if you have:

foo: {
	type: 'string'
}

And on the command-line, you do:

$ cli --foo=a

You get 'a' back, but if you do:

$ cli --foo=a --foo=b

You get and array back ['a', 'b'].

This is surprising and inconsistent.

Instead, we should allow only one value by default, and add a setting to the flags config to allow multiple for a flag, in which case, it will always return an array, even if there's only one flag.

I imagine it could be something like this:

{
	flags: {
		foo: {
			type: 'string',
			multiple: true
		}
	}
}

IssueHunt Summary

ulken ulken has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips

@b4dnewz
Copy link

b4dnewz commented Feb 12, 2019

I also would like to propose this form when using multiple flag arguments:

{
	flags: {
		foo: {
			type: 'string',
			multiple: true
		}
	}
}

cli --foo=a --foo=b
cli --foo=a,b

// ['a', 'b']

if not already supported, and if the multiple property is true, the foo flag value should always be an array even if only one input is passed:

cli --foo=a
// ['a']

@sindresorhus
Copy link
Owner Author

and if the multiple property is true, the foo flag value should always be an array even if only one input is passed

Yes, that's already noted:

and add a setting to the flags config to allow multiple for a flag, in which case, it will always return an array, even if there's only one flag.

@b4dnewz
Copy link

b4dnewz commented Feb 12, 2019

Ah, sorry!
I missed that, looking forward to see this update, when I've some spare time I'll watch at the src code and try to contribute myself. great project, it makes creating cli tools extremely easy and immediate, thanks!

@sindresorhus
Copy link
Owner Author

I also would like to propose this form when using multiple flag arguments:

I considered that form, but it's complicated as it would only work for flags where the value cannot be comma, otherwise it becomes ambiguous. For example, how would it handle this?

{
	flags: {
		foo: {
			type: 'string',
			multiple: true
		}
	}
}

cli --foo="I like unicorns, but not horses"

@sindresorhus
Copy link
Owner Author

sindresorhus commented Feb 12, 2019

The author would have to explicitly opt into supporting it if so, like multiple: 'list'.

@b4dnewz
Copy link

b4dnewz commented Feb 12, 2019

@sindresorhus mmh.. didn't thought about that..

The author would have to explicitly opt into supporting it if so, like multiple: 'list'.

It could be a good solution, do you mean list for using the comma separated values?

@sindresorhus
Copy link
Owner Author

It could be a good solution, do you mean list for using the comma separated values?

Yes

@b4dnewz
Copy link

b4dnewz commented Feb 12, 2019

it make sense, i'm looking to other project (yargs for example) to see how they deal with it but it seems they don't support the comma separated values so my idea probably might be bad and the list option shallow.

@IssueHuntBot
Copy link

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

@ulken
Copy link
Contributor

ulken commented Apr 11, 2020

Third time's a charm? #143

@issuehunt-oss
Copy link

issuehunt-oss bot commented May 5, 2020

@sindresorhus has rewarded $36.00 to @ulken. See it on IssueHunt

  • 💰 Total deposit: $40.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $4.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 5, 2020
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
4 participants