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

Preserve groups containing global keys #395

Merged
merged 1 commit into from
Feb 20, 2016

Conversation

novemberborn
Copy link
Contributor

Heya! I used yargs in a new CLI tool the other day. This tool takes some global options as well as per-command options. I noticed I had to restore the global option groups for each command, which I was not expecting.

With this PR I'm proposing that groups containing global keys are preserved within commands. Please let me know if that's a sensible thing for yargs to do!

One concern raised in #336 is the ability to control the order of the groups in each command's help output. Currently this PR puts the groups with global keys first. Possibly the groups object could be reordered if the group is redeclared without keys.

I also may be misunderstanding the myriad uses of yargs.reset(), though the tests seem to pass aside for one introduced to fix #336. I can fix those tests if people are happy with this approach.

Thanks.

@@ -385,7 +396,6 @@ function Argv (processArgs, cwd) {
return options
}

var groups = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI reset() seems to be called before this is reached. groups should be declared higher up regardless of this PR.

@nexdrew
Copy link
Member

nexdrew commented Feb 19, 2016

@novemberborn Hey thanks! I think this is certainly a reasonable request - the groups for global options should be preserved for commands just like the options themselves are. Since the concept of global options is so new to yargs, I think we just haven't shaken out all the proper uses/bugs for them.

I think this looks good. My only concern is that, per #336 and the failing test, yargs should output the command-specific option groups before it outputs the global option groups, so some reordering seems necessary. Could you possibly work that into this PR and also add a specific test for preserving groups with global options (one that actually uses .global() or global: true - unlike the failing test 😃)?

@novemberborn novemberborn changed the title [WIP] Preserve groups containing global keys Preserve groups containing global keys Feb 19, 2016
@novemberborn
Copy link
Contributor Author

@nexdrew cool, thanks! I've (force-)pushed a commit which addresses your concerns. By default the preserved groups are shown last, but you can add to such a group or indeed bump it up in the command builder.

return yargs(['upload', '-h'])
.command('upload', 'upload something', function (yargs) {
return yargs
.group([], 'Awesome Flags:')
Copy link
Member

Choose a reason for hiding this comment

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

This is an interesting "feature". 😄 I like the functionality, but the API is not very intuitive IMO. That being said, I can't really think of a better alternative, so 👍.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh yea. It's a side-effect of being able to add to preserved groups but I figured it'd be worthwhile adding a test for it.

Copy link
Member

Choose a reason for hiding this comment

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

Without the test, I would not have spotted this. So great idea!

@nexdrew
Copy link
Member

nexdrew commented Feb 19, 2016

LGTM 👍

@novemberborn Thanks for the contribution!

@nexdrew
Copy link
Member

nexdrew commented Feb 20, 2016

@bcoe Any objections here?

@bcoe
Copy link
Member

bcoe commented Feb 20, 2016

@novemberborn @nexdrew I love it, will work on getting a release out tomorrow, and on moving yargs to an org.

bcoe added a commit that referenced this pull request Feb 20, 2016
Preserve groups containing global keys
@bcoe bcoe merged commit 4f32150 into yargs:master Feb 20, 2016
@novemberborn novemberborn deleted the retain-global-groups branch February 21, 2016 16:32
@bcoe
Copy link
Member

bcoe commented Feb 23, 2016

@novemberborn give npm i yargs@next a shot, it has your fixes to yargs-parser and the global groups bug.

@novemberborn
Copy link
Contributor Author

@bcoe works great!

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.

group property displays option twice
3 participants