-
-
Notifications
You must be signed in to change notification settings - Fork 141
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 support for groups in the default HelpPrinter #135
Conversation
model.go
Outdated
@@ -203,6 +203,17 @@ func (n *Node) Path() (out string) { | |||
return strings.TrimSpace(out) | |||
} | |||
|
|||
// ClosestGroup finds the first non-empty group in this node and its ancestors. | |||
func (n *Node) ClosestGroup() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially thought that subcommands would automatically inherit the group of their parents, from there:
Lines 76 to 79 in 2479d83
// Assign parent if it's not already set. | |
if subf.tag.Group == "" { | |
subf.tag.Group = tag.Group | |
} |
But subcommands are actually escaping the for
loop from here:
Line 62 in 2479d83
continue |
I didn't want to mess too much with this part, so I added this ClosestGroup()
function to do the job.
Looks good! That's a really great idea about the titles, the main reason I hadn't kept going with this is that putting long group titles in struct tags is not awesome. I think, as you say, a function option such as |
Sounds good, I'll keep going then! |
@alecthomas Ready for review, let me know if I missed something 👍 |
help.go
Outdated
@@ -115,18 +116,18 @@ func printApp(w *helpWriter, app *Application) { | |||
} | |||
} | |||
|
|||
func printCommand(w *helpWriter, app *Application, cmd *Command) { | |||
func printCommand(w *helpWriter, app *Application, cmd *Command, groups map[string]Group) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per other comment, the groups should be accessible through the model, in which case this should be unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing for all the other locations.
options.go
Outdated
@@ -208,6 +208,25 @@ func ConfigureHelp(options HelpOptions) Option { | |||
}) | |||
} | |||
|
|||
// Group holds metadata about a node group used when printing help. | |||
type Group struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go into model.go
, and be returned by the existing group related model methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify, you want something like this?
type Node struct {
...
- Group string
+ Group *Group
}
type Group struct {
+ // Key is the `group` field tag value used to identify this group.
+ Key string
Title string
Header string
}
And have the key -> Group
resolved during parsing in build.go
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly.
}) | ||
expected := `Usage: test-app <command> | ||
|
||
A test app. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting looks good!
Nice, thanks! |
This is awesome. |
🎉 This PR makes me really happy, it came exactly at the right time for me 😄 Thank you @mickael-menu! |
Fix #134
@alecthomas It is still lacking support for grouped flags, I wanted to check with you that this PR is welcome and on the right path first.
Here's what's implemented so far:
I think it could be useful to add some group metadata as well. In this case, the
group
tag would be a key referencing a declared Group metadata. We could store:start a working area (see also: git help tutorial)
I'm not sure what would be the best place for such metadata. Maybe an
Option
?