-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 option always requires an argument #4367
Comments
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 |
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 |
When overriding other fields, help or version flag, globals, etc, a user might not care about the value, so let's ignore the lookup. Been talking about this for a while but Issue clap-rs#4367 moved this forward because there wasn't a good way to handle this without changing behavior.
When overriding other fields, help or version flag, globals, etc, a user might not care about the value, so let's ignore the lookup. Been talking about this for a while but Issue clap-rs#4367 moved this forward because there wasn't a good way to handle this without changing behavior.
#4371 will allow #[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();
} |
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 |
clap-rs/clap#4367 clap-rs/clap#4371 Signed-off-by: Wenxuan Zhang <wenxuangm@gmail.com>
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
The text was updated successfully, but these errors were encountered: