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

Hidden subcommands shown when "requires a subcommand but one was not provided" #5471

Open
2 tasks done
devjgm opened this issue Apr 22, 2024 · 1 comment
Open
2 tasks done
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-bug Category: Updating dependencies S-waiting-on-decision Status: Waiting on a go/no-go before implementing

Comments

@devjgm
Copy link

devjgm commented Apr 22, 2024

Please complete the following tasks

Rust Version

rustc 1.74.0 (79e9716c9 2023-11-13)

Clap Version

4.5.4

Minimal reproducible code

use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[clap(version, about)]
struct Cli {
    #[clap(subcommand)]
    command: Command,

    #[clap(long)]
    message: String,
}

#[derive(Subcommand, Debug)]
enum Command {
    Hello,
    #[clap(hide = true)]
    Secret,  // <------ This subcommand is marked hidden and shouldn't be suggested to the user
}

fn main() {
    let cli = Cli::parse();
    match cli.command {
        Command::Hello => println!("Hello {}", cli.message),
        Command::Secret => println!("Secret {}", cli.message),
    }
}

Steps to reproduce the bug with the above code

$ cargo run -- --message foo
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/clap-repro --message foo`
error: 'clap-repro' requires a subcommand but one was not provided
  [subcommands: hello, secret, help]

Usage: clap-repro --message <MESSAGE> <COMMAND>

For more information, try '--help'.

Actual Behaviour

The problem is that the secret subcommand is marked as hidden, yet it is suggested as one of the possible subcommands in the list [subcommands: hello, secret, help]

Expected Behaviour

Since the secret subcommand is marked as hidden, it should not be suggested to the user in the list of possible subcommands. So
Actual: [subcommands: hello, secret, help]
Expected: [subcommands: hello, help]

Additional Context

The --message flag in the repro case seems to be important. If I remove it and omit the subcommand, it sees that no arguments are provided and simply displays the normal help message, which correctly omits the hidden subcommand.

Debug Output

No response

@devjgm devjgm added the C-bug Category: Updating dependencies label Apr 22, 2024
@epage epage added A-help Area: documentation, including docs.rs, readme, examples, etc... S-waiting-on-decision Status: Waiting on a go/no-go before implementing labels Apr 23, 2024
@epage
Copy link
Member

epage commented Apr 23, 2024

FYI #4853 is a different place hidden subcommands can show up in errors

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-bug Category: Updating dependencies S-waiting-on-decision Status: Waiting on a go/no-go before implementing
Projects
None yet
Development

No branches or pull requests

2 participants