-
-
Notifications
You must be signed in to change notification settings - Fork 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
Using -? for help in addition to -h #1127
Comments
There's no reason you can't use |
Thanks, but I was unable to add -? as a new option properly. Maybe I miss something else.
when I compile this as
When I type Second question: Assuming the above can be solved, how would I connect the -? option with the print_help function? |
Which shell are you using? In ZSH For example on my machine: extern crate clap;
use clap::App;
fn main() {
let matches = App::new("prog")
.help_short("?")
.get_matches();
} Then:
It's the same if you simply want to add a flag which prints the help too (as in your example). |
Thanks, I just tried .help_short("?"). It works as desired, but unfortunately, the -h option becomes an invalid one. I have no experience with zsh. I use bash as well as the standard os-x and windows 7 terminals. My examples are tar and python. tar understands -? and it doesn't even understand -h. Python supports both. That shows to me that -? or -h is just a preference of the developer. I would like to be able to support both. In case a shell such as zsh gives the -? a special meaning, that is fair enough, but I am sure the ones that I use would not prevent me from using -? directly. Actually, bash has the same special meaning for ? For example if I have a file called |
If you want to use both extern crate clap;
use std::env;
use std::process;
use clap::{Arg, App};
fn main() {
// We build the App instance and save it, so we can
// use it later if needed
let mut app = App::new("prog")
.arg(Arg::with_name("help")
.short("?")
.help("Also prints the help message"));
// We call this method which will do all the
//parsing, but not consume our App instance
let res = app.get_matches_from_safe_borrow(env::args_os());
// This calls all the normal clap error messages
// if one should exist
let matches = res.unwrap_or_else(|e| e.exit());
// Now we check for ?
if matches.is_present("help") {
let _ = app.print_help();
println!(""); // adds a newline
process::exit(0);
}
// Now we can use matches like normal...
}
|
Thanks! This is exactly what I was looking for. |
@epage is there documentation on how to do this with modern clap (derive specifically)? |
As of clap v3, there are two ways of doing this
#[derive(Parser)]
#[clap(mut_arg("help", |arg| arg.short_alias('?')))]
struct Cli {
...
} (You'd just as easily use
#[derive(Parser)]
struct Cli {
#[clap(short, long, short_alias = '?', action = ArgAction::Help, help = "Print help information")]
help: bool,
} |
That's perfect, thank you! |
@epage How do you do this in v4? |
#[derive(Parser)]
#[command(disable_help_flag = true)]
struct Cli {
#[arg(short, long, short_alias = '?', action = ArgAction::Help, help = "Print help information")]
help: bool,
} |
Thanks! Needs |
Also needs to be |
It would be awesome for this information to make it into the docs/tutorial/examples for both the Derive and Builder patterns. My company standardized on '-?' for help and '-v' for version. So I've spent the last few hours trying to figure how to upgrade all my clap 2.34 apps to v4.0. I was pulling my hair out until I stumbled on this. |
Trying to think of the best way to integrate this into the docs
|
Maybe it's not actually that specialized. Whenever someone creates an I suppose args starting with A certainly common example: Args to connect to a database: use clap::{arg, Parser};
#[derive(Debug, Parser)]
#[command(author, version, about)]
pub struct Args {
/// Database host
#[arg(short, long)]
pub host: String, // <-- Will cause trouble
/// Database port
#[arg(short = 'P', long, default_value_t = 3306)]
pub port: usize,
/// Database user
#[arg(short, long)]
pub user: String,
/// Database password. Pass "-" to read from stdin.
#[arg(short, long)]
pub password: String,
/// Database name
#[arg(short, long)]
pub database: String,
} In the end, I opted to go for This used to be easier, especially when using |
For a tutorial, yes it is. This is where we are showing people how to create an argument in the first place; we shouldn't be covering all the various special cases. There are other places this might fit, like the cookbook, but its a problem of finding the right way to include it so it is discoverable without contributing to the docs being overwhelming. We could have a cookbook item just about this but its better when we can (1) have a concrete example we are mimicing (I want to do "X" like
While I am sympathetic to the struggles of figuring out how to do it, I feel like this use case is a good example for why I think the current behavior is important. Besides anything else I get into below, making
I assume you are referring to clap automatically disabling |
I hold to my initial request 😃 The behavior for this changed in recent versions, and for people upgrading that need to maintain backward compatibility for their apps, it can be a little frustrating how to do it with the new versions. The new way is certainly more flexible, so documenting it would also help explain the new power at hand. If the vagueness of the existing docs are a subtle way to guide users to a default, uniform behavior, then keep in mind the huge number of different areas this library may be used in. I use this a lot for networking and MQTT utilities, and in that space, |
Rust Version
rustc 1.22.1 (05e2e1c41 2017-11-22)
Affected Version of clap
2.28.0
Expected Behavior Summary
The -h option has a good built-in behaviour. It is desireable that -? would do exactly the same.
Actual Behavior Summary
-? is not recognised as a built-in option as -h. Calling the hello world example with
hello -?
produces an error message:Steps to Reproduce the issue
Create a new minimal crate using the example code clap's own documentation, build the executable and run it with the -? option.
Sample Code or Link to Sample Code
https://docs.rs/clap/2.29.0/clap/index.html
Debug output
Not needed
The text was updated successfully, but these errors were encountered: