-
-
Notifications
You must be signed in to change notification settings - Fork 140
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
new HelpOption: Allow full subcommand flag display #306
new HelpOption: Allow full subcommand flag display #306
Conversation
e37690f
to
965b187
Compare
@alecthomas - this is the change as little as possible version, happy to do a pass which removes a little magic (but would remote it broader changes) Also not super convinced about the new |
Converting to draft as it probably needs to (optionally) filter inherited flags, can get super noisy! This would also fix the annoying explicit |
Much improved now Example output: $ go run ./_examples/shell/optionalflags
Usage: help --mandatory <command>
An app demonstrating SubcommandsWithOptionalFlags
Flags:
-h, --help Show context-sensitive help.
--dry-run=STRING optional dry run flag
--mandatory mandatory global flag
Commands:
resource create --mandatory --name=STRING
create a resource
resource delete --mandatory --scope=STRING [--labels=LABELS,... --version=STRING]
delete resource(s)
Run "help <command> --help" for more information on a command. |
@alecthomas happy to create an issue against this if desired, just say the word! |
// FlagSummary for the node. | ||
func (n *Node) FlagSummary(hide bool) 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.
Sorry for the epically late response. Overall the change looks good, but I can't merge this as it's a breaking API change.
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.
Sorry for the epically late response
All good! my turn this time, was on a short break :)
I can't merge this as it's a breaking API change.
FlagSummary
is a fairly short function - so if you don't mind my duplicating it I could turn this into an additive change, ie.
- Additive changes:
- add
Node.FlagSummaryWithOptions(bool, bool)
Node.SummaryWithOptions()
callsNode.FlagSummaryWithOptions(bool, bool)
instead
- add
- Reverted:
Node.FlagSummary()
returns to its original arityNode.Summary()
goes back to callingNode.FlagSummary(bool)
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.
Actually - given the current PR is unacceptable, I'll just make the changes now and see what you think :)
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.
Rebased, reworked changes to avoid API modification (and attempting to minimise duplication where possible)
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 extra explanation re. bonus change in b196f4d : appears that my rebase picked up some change which is invalid with the current golangci-lint (see previous execution here: https://github.com/alecthomas/kong/actions/runs/3126674306/jobs/5072442276)
Seem fine now
5daa214
to
5f9f4af
Compare
b196f4d
to
7df777e
Compare
7df777e
to
1a83a02
Compare
Rebased, reverted the removal of |
By default, only `required` flags are displayed for subcommands For applications with a lot of simple subcommands this obscures basic usage flags == Mitigation - Add a new `HelpOption` called `IncludeOptionalFlagsInSummary` - Move `Node.Summary()` functionality into `Node.SummaryWithOptions(bool)` - Retain `Node.Summary()` as a default usage (forwards default value to `Node.SummaryWithOptions(bool)`) - Modify `help.go@printCommandSummary()` to forward the `HelpOption` to `Node.SummaryWithOptions()` instead of calling `Node.Summary()` - Add tests for default and optional behaviour == Caveats The main issue with this implementation is due to the `Node` receiver functions doing display logic internally. This makes sense for most of the use cases (application error logic), but overly restricts the help configurability As a result: - Must pass awkward `bool` value down from the help context - `FlagSummary()` ends up having to explicitly exclude the `help` flag. == Potential improvements to be made - Remove explicit exclusion of `help` from `FlagSummary()` - Potentially add an equivalent `Node` receiver which just returns an array of flag summaries to be displayed by `help@printCommandSummary`
- Adds `Parent` field to `Value`, allowing us to easily check whether a value originated with a given `Node` - Renames the new `HelpOption` to `SubcommandsWithOptionalFlags` - When the option is enabled - Groups required flags - Groups optional flags (by checking if the flag originated on the current Node) - Prints required flags followed by optional flags surrounded by brackets, eg. `--mandatory [ --optional1 --optional2 ]` - Removes janky check if a flag is `help` - Now just checks if the flag is our own - Added criteria to the test suite to confirm: - Ancestor's optional flags are not included - Ancestor's required flags are included
Addresses requested change here: https://github.com/alecthomas/kong/pull/306/files#r975322296
original PR was from prior to the switch from `require` to `assert` for tests (in this commit 8b82618) Just change to using `assert` in keeping with the rebase
1a83a02
to
3957ac6
Compare
Still find this useful, but seems I'm alone - will close this to (finally) reduce the PR clutter and continue using it from my own fork |
Problem Statement
By default, only
required
flags are displayed for subcommandsFor applications with a lot of simple subcommands this obscures basic usage flags
Example (click to expand)
NB: this is an entirely contrived example, but is loosely based on my real world use case
Before (and still the default behaviour)
For a user to discover that there are optional flags on these subcommands they'd have to know to look
After (and entirely optional)
Now the user can see that
resource delete
has an optional flag without knowing to look for itApproach taken
Add a new
HelpOption
calledSubcommandsWithOptionalFlags
Move
Node.Summary()
functionality intoNode.SummaryWithOptions(bool)
Node.Summary()
as a default usage (forwards defaultvalue to
Node.SummaryWithOptions(bool)
)Adds
Parent
field toValue
, allowing us to easily check whethera value originated with a given
Node
build.go@buildField()
to includeNode
Parent
Modify
help.go@printCommandSummary()
to forward theHelpOption
toNode.SummaryWithOptions()
instead ofcalling
Node.Summary()
When the option is enabled:
the current Node)
by brackets, eg.
--mandatory [ --optional1 --optional2 ]
Removes broken logic from previous commit checking if a flag is named
"help"
Node
Added criteria to the test suite to confirm:
Caveats
The main issue with this implementation is due to the
Node
receiverfunctions doing display logic internally. This makes sense
for most of the use cases (application error logic), but
overly restricts the help configurability
As a result:
bool
value down from the help context(resolved earlier)FlagSummary()
ends up having to explicitly exclude thehelp
flag - this is particularly jankyPotentialImprovementsto bemadeRemove explicit exclusion ofhelp
fromFlagSummary()
Node
receiver which just returns anarray of flag summaries to be displayed by
help@printCommandSummary
Change toFlagSummary()
's arity is a breaking change and unacceptable, seek alternative