clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
14.13k stars 1.03k forks source link

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

Open devjgm opened 5 months ago

devjgm commented 5 months ago

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

epage commented 5 months ago

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