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

Document external subcommands #3887

Closed
epage opened this issue Jun 30, 2022 Discussed in #3863 · 4 comments
Closed

Document external subcommands #3887

epage opened this issue Jun 30, 2022 Discussed in #3863 · 4 comments
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing

Comments

@epage
Copy link
Member

epage commented Jun 30, 2022

Discussed in #3863

Originally posted by cd-work June 22, 2022
I'd like to write an external subcommand that accepts any parameter and forwards it, however it should also still be documented as subcommand.

I think this is probably a pretty common scenario, since you usually have some way to know which external subcommands are available even if you do not know their exact parameters. I do not want to allow just any subcommand, I have a list which I want to accept with generic parameters, everything else should still cause a failure.

I've tried using ignore_errors(true), but that only seems to work globally and not on the subcommands themselves. Otherwise it could be used to just accept parameters.

I've also used various combinations of allowing hyphen values and multiple values, but none of them seem to allow for the first argument to have two hyphens.

I only care about the subcommands documentation and description on the toplevel, since bin subcommand --help should already be forwarded to the subcommand and not handled by clap. This is also a scenario where built-in subcommands and flags exist already, so it's not only external subcommands I'm worried about.

@epage epage added C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing A-help Area: documentation, including docs.rs, readme, examples, etc... labels Jun 30, 2022
@epage
Copy link
Member Author

epage commented Jun 30, 2022

I only care about the subcommands documentation and description on the toplevel, since bin subcommand --help should already be forwarded to the subcommand and not handled by clap.

Unless you are disabling it, this still leaves bin help subcommand

@emersonford
Copy link
Contributor

emersonford commented Jun 30, 2022

just two cents from someone who's been working on something similar w/ external subcommands, I think a more reasonable approach API might be something like treat_flags_as_positional_args or something of that nature, which could essentially just make the bin subcommand -- this --flag is not treated as a flag behavior the default.

specifically, allow_external_subcommands has the side effect of also handling unrecognized positional args (not just subcommands). if we could tell Clap to treat flags just as positional args, allow_external_subcommand would gracefully handle this situation for free.

would also make it clear that bin help subcommand isn't treated as an external subcommand and I feel like it's a bit clearer than ignore_error

edit: this approach would also solve #3880

@cd-work
Copy link

cd-work commented Jun 30, 2022

The ignore_errors approach just seems like the simplest way to get an acceptable solution without introducing a dedicated API, though I agree that it is not ideal.

If I had to consider an ideal API for my usecase, I think it would be a way to specify a list of named external subcommands with a description of what they do, which is used as documentation on the toplevel, but then also informs clap that these are not going to want their own subcommand docs.

The concrete effect this would have on bin help subcommand still isn't straightforward though. While you could transform it to bin subcommand --help, that's pretty hacky so far from ideal. But just erroring out for external subcommands would probably work just fine, I personally think bin subcommand help makes much more sense to use anyway.

@epage
Copy link
Member Author

epage commented Sep 7, 2022

I believe with #4187 it should now work to define subcommands and then capture all their values with arg.trailing_var_arg(true).allow_hyphen_values(true). You will also need to disable help and version flags on these semi-external subcommands as well.

@epage epage closed this as completed Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants