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

Print supported values in synopses, when practical #620

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

numist
Copy link
Member

@numist numist commented Mar 2, 2024

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:

USAGE: math <subcommand>

to

USAGE: math <add|multiply|stats>

But does not change the output for commands with a single subcommand or commands with very long subcommands.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

@numist numist requested a review from natecook1000 March 2, 2024 07:03
Copy link
Member

@natecook1000 natecook1000 left a 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

Comment on lines +49 to +56
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)
Copy link
Member

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> ...

Copy link
Member Author

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.

Copy link
Member Author

@numist numist Mar 11, 2024

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>
Copy link
Member

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.

@rauhul
Copy link
Contributor

rauhul commented Mar 5, 2024

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.

@numist
Copy link
Member Author

numist commented Mar 5, 2024

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)

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.

None yet

3 participants