mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
feat: Usage displays short, long, and normal scs
Displayed in the form of pacman {query, --query, -Q} [OPTIONS]
This commit is contained in:
parent
ec35ab8813
commit
1127ca6e13
2 changed files with 184 additions and 8 deletions
|
@ -532,14 +532,12 @@ where
|
|||
if !(self.is_set(AS::ArgsNegateSubcommands) && self.is_set(AS::ValidArgFound)
|
||||
|| self.is_set(AS::AllowExternalSubcommands)
|
||||
|| self.is_set(AS::InferSubcommands))
|
||||
&& subcmd_name.is_none()
|
||||
{
|
||||
let cands =
|
||||
suggestions::did_you_mean(&*arg_os.to_string_lossy(), sc_names!(self.app));
|
||||
if !cands.is_empty() {
|
||||
let cands: Vec<_> =
|
||||
cands.iter().map(|cand| format!("'{}'", cand)).collect();
|
||||
debug!("@NickHackman In arg parsing HELP - regression?");
|
||||
return Err(ClapError::invalid_subcommand(
|
||||
arg_os.to_string_lossy().into_owned(),
|
||||
cands.join(" or "),
|
||||
|
@ -707,7 +705,6 @@ where
|
|||
&& arg_os.starts_with("-"))
|
||||
&& !self.is_set(AS::InferSubcommands)
|
||||
{
|
||||
debug!("@NickHackman Other place - regresion?");
|
||||
return Err(ClapError::unknown_argument(
|
||||
&*arg_os.to_string_lossy(),
|
||||
None,
|
||||
|
@ -1055,8 +1052,22 @@ where
|
|||
|
||||
if let Some(sc) = self.app.subcommands.iter_mut().find(|s| s.name == sc_name) {
|
||||
let mut sc_matcher = ArgMatcher::default();
|
||||
// bin_name should be parent's bin_name + [<reqs>] + the sc's name separated by
|
||||
// a space
|
||||
// Display subcommand name, short and long in usage
|
||||
let mut sc_names = sc.name.clone();
|
||||
let mut flag_subcmd = false;
|
||||
if let Some(l) = sc.long {
|
||||
sc_names.push_str(&format!(", --{}", l));
|
||||
flag_subcmd = true;
|
||||
}
|
||||
if let Some(s) = sc.short {
|
||||
sc_names.push_str(&format!(", -{}", s));
|
||||
flag_subcmd = true;
|
||||
}
|
||||
|
||||
if flag_subcmd {
|
||||
sc_names = format!("{{{}}}", sc_names);
|
||||
}
|
||||
|
||||
sc.usage = Some(format!(
|
||||
"{}{}{}",
|
||||
self.app.bin_name.as_ref().unwrap_or(&String::new()),
|
||||
|
@ -1065,8 +1076,11 @@ where
|
|||
} else {
|
||||
""
|
||||
},
|
||||
&*sc.name
|
||||
sc_names
|
||||
));
|
||||
|
||||
// bin_name should be parent's bin_name + [<reqs>] + the sc's name separated by
|
||||
// a space
|
||||
sc.bin_name = Some(format!(
|
||||
"{}{}{}",
|
||||
self.app.bin_name.as_ref().unwrap_or(&String::new()),
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
mod utils;
|
||||
|
||||
use clap::{App, AppSettings, Arg, ErrorKind};
|
||||
|
||||
#[test]
|
||||
|
@ -309,7 +311,7 @@ fn flag_subcommand_long_infer_fail() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn flag_subcommands_long_infer_pass_close() {
|
||||
fn flag_subcommand_long_infer_pass_close() {
|
||||
let m = App::new("prog")
|
||||
.setting(AppSettings::InferSubcommands)
|
||||
.subcommand(App::new("test").long_flag("test"))
|
||||
|
@ -319,7 +321,7 @@ fn flag_subcommands_long_infer_pass_close() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn flag_subcommands_long_infer_exact_match() {
|
||||
fn flag_subcommand_long_infer_exact_match() {
|
||||
let m = App::new("prog")
|
||||
.setting(AppSettings::InferSubcommands)
|
||||
.subcommand(App::new("test").long_flag("test"))
|
||||
|
@ -328,3 +330,163 @@ fn flag_subcommands_long_infer_exact_match() {
|
|||
.get_matches_from(vec!["prog", "--test"]);
|
||||
assert_eq!(m.subcommand_name(), Some("test"));
|
||||
}
|
||||
|
||||
static FLAG_SUBCOMMAND_HELP: &str = "pacman-query
|
||||
Query the package database.
|
||||
|
||||
USAGE:
|
||||
pacman {query, --query, -Q} [OPTIONS]
|
||||
|
||||
FLAGS:
|
||||
-h, --help Prints help information
|
||||
-V, --version Prints version information
|
||||
|
||||
OPTIONS:
|
||||
-i, --info <info>... view package information
|
||||
-s, --search <search>... search locally-installed packages for matching strings";
|
||||
|
||||
#[test]
|
||||
fn flag_subcommand_long_short_normal_usage_string() {
|
||||
let app = App::new("pacman")
|
||||
.about("package manager utility")
|
||||
.version("5.2.1")
|
||||
.setting(AppSettings::SubcommandRequiredElseHelp)
|
||||
.author("Pacman Development Team")
|
||||
// Query subcommand
|
||||
//
|
||||
// Only a few of its arguments are implemented below.
|
||||
.subcommand(
|
||||
App::new("query")
|
||||
.short_flag('Q')
|
||||
.long_flag("query")
|
||||
.about("Query the package database.")
|
||||
.arg(
|
||||
Arg::new("search")
|
||||
.short('s')
|
||||
.long("search")
|
||||
.about("search locally-installed packages for matching strings")
|
||||
.conflicts_with("info")
|
||||
.multiple_values(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("info")
|
||||
.long("info")
|
||||
.short('i')
|
||||
.conflicts_with("search")
|
||||
.about("view package information")
|
||||
.multiple_values(true),
|
||||
),
|
||||
);
|
||||
assert!(utils::compare_output(
|
||||
app,
|
||||
"pacman -Qh",
|
||||
FLAG_SUBCOMMAND_HELP,
|
||||
false
|
||||
));
|
||||
}
|
||||
|
||||
static FLAG_SUBCOMMAND_NO_SHORT_HELP: &str = "pacman-query
|
||||
Query the package database.
|
||||
|
||||
USAGE:
|
||||
pacman {query, --query} [OPTIONS]
|
||||
|
||||
FLAGS:
|
||||
-h, --help Prints help information
|
||||
-V, --version Prints version information
|
||||
|
||||
OPTIONS:
|
||||
-i, --info <info>... view package information
|
||||
-s, --search <search>... search locally-installed packages for matching strings";
|
||||
|
||||
#[test]
|
||||
fn flag_subcommand_long_normal_usage_string() {
|
||||
let app = App::new("pacman")
|
||||
.about("package manager utility")
|
||||
.version("5.2.1")
|
||||
.setting(AppSettings::SubcommandRequiredElseHelp)
|
||||
.author("Pacman Development Team")
|
||||
// Query subcommand
|
||||
//
|
||||
// Only a few of its arguments are implemented below.
|
||||
.subcommand(
|
||||
App::new("query")
|
||||
.long_flag("query")
|
||||
.about("Query the package database.")
|
||||
.arg(
|
||||
Arg::new("search")
|
||||
.short('s')
|
||||
.long("search")
|
||||
.about("search locally-installed packages for matching strings")
|
||||
.conflicts_with("info")
|
||||
.multiple_values(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("info")
|
||||
.long("info")
|
||||
.short('i')
|
||||
.conflicts_with("search")
|
||||
.about("view package information")
|
||||
.multiple_values(true),
|
||||
),
|
||||
);
|
||||
assert!(utils::compare_output(
|
||||
app,
|
||||
"pacman query --help",
|
||||
FLAG_SUBCOMMAND_NO_SHORT_HELP,
|
||||
false
|
||||
));
|
||||
}
|
||||
|
||||
static FLAG_SUBCOMMAND_NO_LONG_HELP: &str = "pacman-query
|
||||
Query the package database.
|
||||
|
||||
USAGE:
|
||||
pacman {query, -Q} [OPTIONS]
|
||||
|
||||
FLAGS:
|
||||
-h, --help Prints help information
|
||||
-V, --version Prints version information
|
||||
|
||||
OPTIONS:
|
||||
-i, --info <info>... view package information
|
||||
-s, --search <search>... search locally-installed packages for matching strings";
|
||||
|
||||
#[test]
|
||||
fn flag_subcommand_short_normal_usage_string() {
|
||||
let app = App::new("pacman")
|
||||
.about("package manager utility")
|
||||
.version("5.2.1")
|
||||
.setting(AppSettings::SubcommandRequiredElseHelp)
|
||||
.author("Pacman Development Team")
|
||||
// Query subcommand
|
||||
//
|
||||
// Only a few of its arguments are implemented below.
|
||||
.subcommand(
|
||||
App::new("query")
|
||||
.short_flag('Q')
|
||||
.about("Query the package database.")
|
||||
.arg(
|
||||
Arg::new("search")
|
||||
.short('s')
|
||||
.long("search")
|
||||
.about("search locally-installed packages for matching strings")
|
||||
.conflicts_with("info")
|
||||
.multiple_values(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("info")
|
||||
.long("info")
|
||||
.short('i')
|
||||
.conflicts_with("search")
|
||||
.about("view package information")
|
||||
.multiple_values(true),
|
||||
),
|
||||
);
|
||||
assert!(utils::compare_output(
|
||||
app,
|
||||
"pacman query --help",
|
||||
FLAG_SUBCOMMAND_NO_LONG_HELP,
|
||||
false
|
||||
));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue