-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Built-in default subcommand support #4442
Comments
This helps when there are required arguments. Instead of making them
The usage that clap is generating is more accurate. It is saying you can specify a subcommand or specify the arguments directly.
This seems unrelated to default subcommands. If they aren't defined on the subcommand, then they won't show up. Should these be marked
#3135 m might be relevant for this
Feel free to create a separate issue for the error message |
For how uncommon default subcommands seem to be, the level of machinery seems to be a fair match. We are trying to balance with ease of use with binary size / build times. We do this by providing lower level building blocks that allow people to build what they need while providing an easy-to-use path for the common cases. If there seems to be enough interest and someone creates a plan for this, then it can move forward. This is unlikely to be a focus area for maintainers. In coming up with a plan, keep in mind that the derive API delegates all of the important machinery to the builder API, so we'll need a proposal that works at the builder level. It will need to analyze the different cases to make sure they work well with each other. |
Closing in favor of #3857 so we keep the conversation in one place. We can re-open if there is a design and interst. |
Thanks, I didn't see #3857 when searching |
Please complete the following tasks
Clap Version
4.0.18
Describe your use case
I have a command which supports subcommands for different operations, but also has a sensible 'default' operation which I'd like to run if a subcommand is omitted. Ideally, I'd like to be able to specify arguments for the default command as well.
Option
when flattening; I'm not entirely sure how this relates but supposedly it helps a bitgit
-like example, but still requires some manual juggling instead of remaining within the#[derive(...)]
APIDescribe the solution you'd like
I think my ideal solution looks something like this, with a command attribute like
#[command(default)]
to declare the default variant to use for parsing:Additional context
Following the directions in discussion #4134, I produced the following solution:
This workaround has the following deficiencies, most of which I suspect relate to the necessary
args_conflicts_with_subcommands
call:The
--help
text prints like this:I would prefer
Usage: my-exe [OPTIONS] [COMMAND]
to reflect that the options can always be given and that the command is optional.my-exe default-command --help
doesn't display the common options, even though they (should be) valid.Arguments before the subcommand fail to parse:
Note the weird message that suggests the command we typed. Following the suggestion to add
--
before the subcommand name gives the same message.Arguments common to all subcommands can't be mixed before/after the subcommand name.
my-exe default-command --tracing-filter=trace
gives an error saying that--tracing-filter
is not expected/invalid in this context.It's unwieldy. Defining a wrapper type, using
flatten
like this, and needing to convert between types orunwrap_or
fields manually all feels rather janky.The text was updated successfully, but these errors were encountered: