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

Custom help for subcommands #5770

Open
2 tasks done
htngr opened this issue Oct 8, 2024 · 0 comments
Open
2 tasks done

Custom help for subcommands #5770

htngr opened this issue Oct 8, 2024 · 0 comments
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@htngr
Copy link

htngr commented Oct 8, 2024

Please complete the following tasks

Clap Version

4.5.19

Describe your use case

I'm currently trying to implement Canop/clap-help#1 (showing a custom help message for subcommands). Without subcommands I can catch if --help was requested (Parser::try_parse and ErrorKind::DisplayHelp). But with subcommands, I couldn't find a way (without manually reparsing the arguments) to check if help for a subcommand was issued.

Did I miss something?

Describe the solution you'd like

A solution might be to attach the current clap::Command to the clap_builder::error::Error.

Manually adding help flags is tedious and doesn't work if there are required arguments:

#[derive(Parser, Debug)]
#[command(
    name = "subcommand",
    author,
    version,
    about,
    disable_help_subcommand = true,
    disable_help_flag = true
)]
struct Args {
    /// Print help
    #[arg(long)]
    help: bool,

    /// A subcommand
    #[command(subcommand)]
    pub command: Option<Subcmd>,
}

#[derive(Subcommand, Clone, Debug)]
enum Subcmd {
    /// Some info for A
    #[clap(disable_help_flag = true)]
    A {
        /// Print help
        #[arg(long)]
        help: bool,
    },
    
    /// Some info for B
    #[clap(disable_help_flag = true)]
    B {
        /// Print help
        #[arg(long)]
        help: bool,

        /// An arg
        x: u8,
    },
}

For example, this does not work:

> cargo run -- -- b --help
error: the following required arguments were not provided:
  <X>

Usage: subcommands b --help <X>

Alternatives, if applicable

No response

Additional Context

No response

@htngr htngr added the C-enhancement Category: Raise on the bar on expectations label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

1 participant