-
Notifications
You must be signed in to change notification settings - Fork 328
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
Print supported values in synopses, when practical #620
base: main
Are you sure you want to change the base?
Conversation
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.
Cool! Does this also change the display for an argument/option when an invalid value is provided or the value is missing?
$ fruit_store purchas apple
$ fruit_store apple --ripeness perf
$ fruit_store apple --ripeness
USAGE: fruit_store [<purchase|sample|return>] <fruit> [--quantity <quantity>] [--ripeness <under|perfect|over>] | ||
|
||
ARGUMENTS: | ||
<action> The transaction type (values: purchase, sample, | ||
return; default: purchase) | ||
<fruit> The fruit to purchase (values: apple, banana, | ||
coconut, dragon-fruit, elderberry, fig, grape, | ||
honeydew) |
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.
It looks like replacing <action>
with its values makes it harder to see the correspondence between the usage string and the help descriptions. Are there existing tools that include values in the usage string like this? What would you think of including the argument name – something like:
USAGE: fruit_store [<action: purchase|sample|return>] <fruit> ...
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'm certain that I've used a cli before that had this feature, but over the past two weeks I could not remember what it was.
It did have the issue you describe though, and I like your solution! Another possibility is that ArgumentParser only expand values when showing a short help (such as gets printed after a ValidationError
) and leaving the behaviour as-is when printing the complete --help
output.
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.
Ohh the tool I'm thinking of is something I use frequently at work. Instead of an expanded --help
, it explains multiple subcommands at once and then refers to a man
page. To paraphrase:
> foo bar --help
Usage:
foo alice --baz <baz_id> [--[no-]qux] [--[no-]thbpt] [--fix]
foo bar --baz <baz_id> [--[no-]qux] [--blarpy <blarpy>] [--minimal | --details] [--[no-]box] [--[no-]link] [--eh|--bee|--see] (--regex|--exact|--contains|--suffix) <search_term>
…
foo sub-commands and options are documented in the manual page (`man foo`).
I was thinking ArgumentParser
's top level help for commands with subcommands is a bit spartan, I wonder if this tool is on to something…
USAGE: math <subcommand> | ||
USAGE: math <add|multiply|stats> |
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.
Likewise, it seems like this loses the context that add/multiply/stats are subcommands.
I'm not fully convinced this is a readability improvement over the valid options appearing below. IMO the synopsis should be succinct and the options below should contain the details. |
Reasonable! What got me started down this path was the idea that it should be possible to write a correct invocation of a command after the briefest possible glance at its usage, which is necessarily at odds with brevity (and introduces repetition). Another possibility is that ArgumentParser could expand values only when the options are not explained ("short help", in this reply to Nate) |
Coming back to this – what would you think about reducing the scope, so that this adds the supported values for options (e.g. |
Done! |
This PR adds support for conditionally expanding argument and subcommand synopses to reflect supported values when there are multiple values and their combined length is not overly onerous.
To use an example from
math
, this PR changes the current:to
But does not change the output for commands with a single subcommand or commands with very long subcommands.
Checklist