1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-05-13 21:26:27 +02:00

Fix interaction with clap.

Previously, `get_matches` would return even if --help or --version was
given, and we could check for them manually. That behavior seems to have
changed. Instead, we must use get_matches_safe to inspect the error to
determine what happened.

We can't use the same process for -V/--version since clap will
unconditionally print its own version info. Instead, we rename (internally)
the version flag so that clap doesn't interfere.
This commit is contained in:
Andrew Gallant 2017-03-12 22:30:54 -04:00
parent 68af3bbdc4
commit 95bc678403
2 changed files with 15 additions and 10 deletions

View File

@ -68,12 +68,12 @@ fn app<F>(next_line_help: bool, doc: F) -> App<'static, 'static>
// consistent with short/long views. // consistent with short/long views.
.arg(arg("help-short").short("h")) .arg(arg("help-short").short("h"))
.arg(flag("help")) .arg(flag("help"))
.arg(flag("version").short("V")) .arg(arg("ripgrep-version").long("version").short("V"))
// First, set up primary positional/flag arguments. // First, set up primary positional/flag arguments.
.arg(arg("pattern") .arg(arg("pattern")
.required_unless_one(&[ .required_unless_one(&[
"file", "files", "help-short", "help", "regexp", "type-list", "file", "files", "help-short", "help", "regexp", "type-list",
"version", "ripgrep-version",
])) ]))
.arg(arg("path").multiple(true)) .arg(arg("path").multiple(true))
.arg(flag("regexp").short("e") .arg(flag("regexp").short("e")
@ -206,7 +206,7 @@ lazy_static! {
doc!(h, "help", doc!(h, "help",
"Show verbose help output.", "Show verbose help output.",
"When given, more details about flags are provided."); "When given, more details about flags are provided.");
doc!(h, "version", doc!(h, "ripgrep-version",
"Prints version information."); "Prints version information.");
doc!(h, "pattern", doc!(h, "pattern",

View File

@ -88,18 +88,23 @@ impl Args {
/// ///
/// Also, initialize a global logger. /// Also, initialize a global logger.
pub fn parse() -> Result<Args> { pub fn parse() -> Result<Args> {
let matches = app::app_short().get_matches(); use clap::ErrorKind::*;
let matches = match app::app_short().get_matches_safe() {
Ok(matches) => matches,
Err(clap::Error { kind: HelpDisplayed, .. }) => {
let _ = ::app::app_long().print_help();
println!("");
process::exit(0);
}
Err(err) => err.exit(),
};
if matches.is_present("help-short") { if matches.is_present("help-short") {
let _ = ::app::app_short().print_help(); let _ = ::app::app_short().print_help();
println!(""); println!("");
process::exit(0); process::exit(0);
} }
if matches.is_present("help") { if matches.is_present("ripgrep-version") {
let _ = ::app::app_long().print_help();
println!("");
process::exit(0);
}
if matches.is_present("version") {
println!("ripgrep {}", crate_version!()); println!("ripgrep {}", crate_version!());
process::exit(0); process::exit(0);
} }