Open wfxr opened 1 year ago
A workaround:
#!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "=4.0.12", features = ["derive"] }
//! ```
use clap::{ArgAction, Parser};
#[derive(Parser)]
#[command(about, version)]
#[command(disable_help_flag = true)]
pub struct App {
#[arg(short = 'h', long, default_value = "localhost")]
pub host: String,
#[arg(long, action = ArgAction::Help, default_value = "false")]
pub help: bool,
}
fn main() {
App::parse();
}
We probably should set the default value_parser, value, and missing value for this like other flags but I suspect that would be a breaking change.
I have also been considering adding support for ()
as a type to mean" don't bother reading it" which would be more appropriate.
Except that workaround doesn't work because we take any value to mean to show help./
Bleh. The joy of not realizing a test doesn't cover all cases
#[test]
fn custom_help_flag() {
#[derive(Debug, Clone, Parser)]
#[command(disable_help_flag = true)]
struct CliOptions {
#[arg(short = 'h', long = "verbose-help", action = ArgAction::Help, value_parser = clap::value_parser!(bool))]
help: (),
}
let result = CliOptions::try_parse_from(["cmd", "--verbose-help"]);
let err = result.unwrap_err();
assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp);
CliOptions::try_parse_from(["cmd"]).unwrap();
}
@epage Great! #4371 solves the problem perfectly. Thanks you!
Going to re-open as I feel like we should add a default value parser and value for Help and Version actions so they can be used with less ceremony
Please complete the following tasks
Rust Version
rustc 1.66.0-nightly (a6b7274a4 2022-10-10)
Clap Version
4.0.12
Minimal reproducible code
Steps to reproduce the bug with the above code
Actual Behaviour
The following message was printed:
Expected Behaviour
No error printed.
Additional Context
No response
Debug Output