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 isMultiple option for flags #143

Merged
merged 20 commits into from May 5, 2020

Conversation

ulken
Copy link
Contributor

@ulken ulken commented Apr 11, 2020

Differs in two ways from previous attempts:

  1. Instead of setting the last value if a flag is used multiple times without multiple set, it throws an error. Makes more sense to me and is how I interpret "we should allow only one value by default".
  2. It supports boolean and number arrays in addition to string.

Fixes #111
Closes #123


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


index.js Outdated Show resolved Hide resolved
- Break out into separate PR
@ulken ulken mentioned this pull request Apr 15, 2020
@ulken
Copy link
Contributor Author

ulken commented Apr 15, 2020

Extracted unrelated change into separate PR: #145

index.d.ts Outdated
@@ -24,14 +25,16 @@ declare namespace meow {
- `type`: Type of value. (Possible values: `string` `boolean` `number`)
- `alias`: Usually used to define a short flag alias.
- `default`: Default value when the flag is not specified.
- `multiple`: Indicates a flag can be set multiple times. Returns an array. (Default: false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What returns an array? It's not clear enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point. Maybe drop the word return altogether?

How about Values are turned into an array (in order?)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with the shorter form

index.js Outdated
@@ -71,6 +84,13 @@ const meow = (helpText, options) => {
};
}

if (minimistOptions.array !== undefined) {
minimistOptions.array = arrify(minimistOptions.array).map(flagKey => ({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a short code comments what's happening here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better yet: Poke @vadimdemedes to give his input on supporting this in minimist-options: minimist-options | Support boolean and number arrays. Then there wouldn't be any need for a comment (or code!) at all...

Your input would be highly valuable as well!

index.js Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

The main function is getting huge. Would be great if you could try to modularize some of the new code into separate functions.

@sindresorhus
Copy link
Owner

Should it be named isMultiple instead of multiple? Since there will soon be a isRequired option (#131).

@ulken
Copy link
Contributor Author

ulken commented Apr 23, 2020

The main function is getting huge. Would be great if you could try to modularize some of the new code into separate functions.

Sure, I'll give it a go.

@ulken
Copy link
Contributor Author

ulken commented Apr 23, 2020

Should it be named isMultiple instead of multiple? Since there will soon be a isRequired option (#131).

Personally, I tend to use is/has/should-prefix when it's a function, but not for simple boolean values.

Having that said, I definitely think naming of booleans should follow the same convention. So if isRequired is set in stone, I think this should follow suit.

Also, I think you referred to the wrong issue/PR (think you meant #141). FYI: In the issue you linked to, you are referring to the issue itself instead of the PR (131 vs. 132).

@ulken
Copy link
Contributor Author

ulken commented Apr 23, 2020

@sindresorhus Alright, I've processed your feedback. Let me know what you think.

@ulken ulken requested a review from sindresorhus April 27, 2020 14:47
@ulken
Copy link
Contributor Author

ulken commented Apr 27, 2020

Up-to-date with master and reverted unintentional formatting change

test.js Show resolved Hide resolved
@ulken
Copy link
Contributor Author

ulken commented May 5, 2020

@sindresorhus Up-to-date with master again...

@sindresorhus sindresorhus changed the title Add setting to indicate a flag can be set multiple times Add isMultiple option for flags May 5, 2020
@sindresorhus sindresorhus merged commit c4c5ee2 into sindresorhus:master May 5, 2020
@sindresorhus
Copy link
Owner

Looks great 👌

@ulken
Copy link
Contributor Author

ulken commented May 5, 2020

Cool. Would you be able to ask @vadimdemedes to check out the PR in minimist-options if you talk to him? Won’t respond on GitHub nor Twitter.

Understand he might not want to merge it, just want it to be evaluated.

@sindresorhus
Copy link
Owner

Give it a little bit more time. He's probably busy. Sometimes it takes some weeks for me to look at PRs too.

@ulken
Copy link
Contributor Author

ulken commented May 5, 2020

Hehe, yeah, so I’ve learned 😉

I get that, but he hasn’t responded to a related issue from August (I think) last year.

Oh, well. Thanks anyway.

@sindresorhus
Copy link
Owner

I tried this on a project and it crashes in:

/Users/sindresorhus/dev/oss/capture-website-cli/node_modules/meow/index.js:86␊
    		[flags[flagKey].type || 'string']: true␊
    		                ^␊
    ␊
    TypeError: Cannot read property 'type' of undefined␊
        at arrify.map.flagKey (/Users/sindresorhus/dev/oss/capture-website-cli/node_modules/meow/index.js:86:19)␊
        at Array.map (<anonymous>)␊
        at convertToTypedArrayOption (/Users/sindresorhus/dev/oss/capture-website-cli/node_modules/meow/index.js:84:22)␊
        at meow (/Users/sindresorhus/dev/oss/capture-website-cli/node_modules/meow/index.js:148:25)␊
        at Object.<anonymous> (/Users/sindresorhus/dev/oss/capture-website-cli/cli.js:9:13)␊
        at Module._compile (internal/modules/cjs/loader.js:778:30)␊
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)␊
        at Module.load (internal/modules/cjs/loader.js:653:32)␊
        at tryModuleLoad (internal/modules/cjs/loader.js:593:12)␊
        at Function.Module._load (internal/modules/cjs/loader.js:585:3)␊

When the flag is --hide-elements. I think it doesn't correctly account for camel-casing.

@ulken
Copy link
Contributor Author

ulken commented May 10, 2020

Ouch. OK, I’ll have a looksie later tonight.

@ulken ulken deleted the flag-multiple branch May 10, 2020 20:22
@ulken
Copy link
Contributor Author

ulken commented May 10, 2020

@sindresorhus Pushed fix

@vadimdemedes
Copy link

Excuse me for delay, guys. Change is in master and published!

@ulken
Copy link
Contributor Author

ulken commented May 17, 2020

629af48 😉

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

Successfully merging this pull request may close these issues.

Add setting to indicate a flag can be set multiple times
3 participants