style: rename short, long -> short_flag, long_flag

These names are more explicit about what short and
long flag subcommands are.
This commit is contained in:
NickHackman 2020-07-10 08:52:13 -04:00
parent 0a266f8c8c
commit f5aabfa482
3 changed files with 24 additions and 22 deletions

View file

@ -72,8 +72,8 @@ pub(crate) enum Propagation {
pub struct App<'b> { pub struct App<'b> {
pub(crate) id: Id, pub(crate) id: Id,
pub(crate) name: String, pub(crate) name: String,
pub(crate) long: Option<&'b str>, pub(crate) long_flag: Option<&'b str>,
pub(crate) short: Option<char>, pub(crate) short_flag: Option<char>,
pub(crate) bin_name: Option<String>, pub(crate) bin_name: Option<String>,
pub(crate) author: Option<&'b str>, pub(crate) author: Option<&'b str>,
pub(crate) version: Option<&'b str>, pub(crate) version: Option<&'b str>,
@ -110,13 +110,13 @@ impl<'b> App<'b> {
/// Get the short flag of the subcommand /// Get the short flag of the subcommand
#[inline] #[inline]
pub fn get_short_flag(&self) -> Option<char> { pub fn get_short_flag(&self) -> Option<char> {
self.short self.short_flag
} }
/// Get the long flag of the subcommand /// Get the long flag of the subcommand
#[inline] #[inline]
pub fn get_long_flag(&self) -> Option<&str> { pub fn get_long_flag(&self) -> Option<&str> {
self.long self.long_flag
} }
/// Get the name of the binary /// Get the name of the binary
@ -144,7 +144,7 @@ impl<'b> App<'b> {
/// Iterate through the *visible* short aliases for this subcommand. /// Iterate through the *visible* short aliases for this subcommand.
#[inline] #[inline]
pub fn get_visible_short_aliases(&self) -> impl Iterator<Item = char> + '_ { pub fn get_visible_short_flag_aliases(&self) -> impl Iterator<Item = char> + '_ {
self.short_flag_aliases self.short_flag_aliases
.iter() .iter()
.filter(|(_, vis)| *vis) .filter(|(_, vis)| *vis)
@ -159,7 +159,7 @@ impl<'b> App<'b> {
/// Iterate through the set of *all* the short aliases for this subcommand, both visible and hidden. /// Iterate through the set of *all* the short aliases for this subcommand, both visible and hidden.
#[inline] #[inline]
pub fn get_all_short_aliases(&self) -> impl Iterator<Item = char> + '_ { pub fn get_all_short_flag_aliases(&self) -> impl Iterator<Item = char> + '_ {
self.short_flag_aliases.iter().map(|a| a.0) self.short_flag_aliases.iter().map(|a| a.0)
} }
@ -449,7 +449,7 @@ impl<'b> App<'b> {
/// assert!(sync_matches.is_present("search")); /// assert!(sync_matches.is_present("search"));
/// ``` /// ```
pub fn short_flag(mut self, short: char) -> Self { pub fn short_flag(mut self, short: char) -> Self {
self.short = Some(short); self.short_flag = Some(short);
self self
} }
@ -492,7 +492,7 @@ impl<'b> App<'b> {
/// assert!(sync_matches.is_present("search")); /// assert!(sync_matches.is_present("search"));
/// ``` /// ```
pub fn long_flag(mut self, long: &'b str) -> Self { pub fn long_flag(mut self, long: &'b str) -> Self {
self.long = Some(long.trim_start_matches(|c| c == '-')); self.long_flag = Some(long.trim_start_matches(|c| c == '-'));
self self
} }
@ -1697,14 +1697,14 @@ enum Flag<'a> {
impl<'a> Flag<'_> { impl<'a> Flag<'_> {
fn short(&self) -> Option<char> { fn short(&self) -> Option<char> {
match self { match self {
Self::App(app) => app.short, Self::App(app) => app.short_flag,
Self::Arg(arg) => arg.short, Self::Arg(arg) => arg.short,
} }
} }
fn long(&self) -> Option<&str> { fn long(&self) -> Option<&str> {
match self { match self {
Self::App(app) => app.long, Self::App(app) => app.long_flag,
Self::Arg(arg) => arg.long, Self::Arg(arg) => arg.long,
} }
} }
@ -1857,7 +1857,7 @@ impl<'b> App<'b> {
for sc in &self.subcommands { for sc in &self.subcommands {
// Conflicts between flag subcommands and long args // Conflicts between flag subcommands and long args
if let Some(l) = sc.long { if let Some(l) = sc.long_flag {
if let Some((first, second)) = self.two_flags_of(|f| f.long() == Some(l)) { if let Some((first, second)) = self.two_flags_of(|f| f.long() == Some(l)) {
panic!( panic!(
"Long option names must be unique for each argument, \ "Long option names must be unique for each argument, \
@ -1868,7 +1868,7 @@ impl<'b> App<'b> {
} }
// Conflicts between flag subcommands and short args // Conflicts between flag subcommands and short args
if let Some(s) = sc.short { if let Some(s) = sc.short_flag {
if let Some((first, second)) = self.two_flags_of(|f| f.short() == Some(s)) { if let Some((first, second)) = self.two_flags_of(|f| f.short() == Some(s)) {
panic!( panic!(
"Short option names must be unique for each argument, \ "Short option names must be unique for each argument, \
@ -2082,7 +2082,7 @@ impl<'b> App<'b> {
|| self || self
.subcommands .subcommands
.iter() .iter()
.any(|sc| sc.short == Some('h') || sc.long == Some("help"))) .any(|sc| sc.short_flag == Some('h') || sc.long_flag == Some("help")))
{ {
debug!("App::_create_help_and_version: Building --help"); debug!("App::_create_help_and_version: Building --help");
let mut help = Arg::new("help") let mut help = Arg::new("help")
@ -2103,7 +2103,7 @@ impl<'b> App<'b> {
|| self || self
.subcommands .subcommands
.iter() .iter()
.any(|sc| sc.short == Some('V') || sc.long == Some("version"))) .any(|sc| sc.short_flag == Some('V') || sc.long_flag == Some("version")))
{ {
debug!("App::_create_help_and_version: Building --version"); debug!("App::_create_help_and_version: Building --version");
let mut version = Arg::new("version") let mut version = Arg::new("version")
@ -2401,7 +2401,9 @@ impl<'b> App<'b> {
/// Find a flag subcommand name by short flag or an alias /// Find a flag subcommand name by short flag or an alias
pub(crate) fn find_short_subcmd(&self, c: char) -> Option<&str> { pub(crate) fn find_short_subcmd(&self, c: char) -> Option<&str> {
self.get_subcommands() self.get_subcommands()
.find(|sc| sc.short == Some(c) || sc.get_all_short_aliases().any(|alias| alias == c)) .find(|sc| {
sc.short_flag == Some(c) || sc.get_all_short_flag_aliases().any(|alias| alias == c)
})
.map(|sc| sc.get_name()) .map(|sc| sc.get_name())
} }
@ -2409,7 +2411,7 @@ impl<'b> App<'b> {
pub(crate) fn find_long_subcmd(&self, long: &ArgStr<'_>) -> Option<&str> { pub(crate) fn find_long_subcmd(&self, long: &ArgStr<'_>) -> Option<&str> {
self.get_subcommands() self.get_subcommands()
.find(|sc| { .find(|sc| {
if let Some(sc_long) = sc.long { if let Some(sc_long) = sc.long_flag {
*long == sc_long || sc.aliases_to(long) *long == sc_long || sc.aliases_to(long)
} else { } else {
false false

View file

@ -585,7 +585,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
); );
let mut short_als = a let mut short_als = a
.get_visible_short_aliases() .get_visible_short_flag_aliases()
.map(|a| format!("-{}", a)) .map(|a| format!("-{}", a))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -781,8 +781,8 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
{ {
let btm = ord_m.entry(sc.disp_ord).or_insert(BTreeMap::new()); let btm = ord_m.entry(sc.disp_ord).or_insert(BTreeMap::new());
let mut sc_str = String::new(); let mut sc_str = String::new();
sc_str.push_str(&sc.short.map_or(String::new(), |c| format!("-{}, ", c))); sc_str.push_str(&sc.short_flag.map_or(String::new(), |c| format!("-{}, ", c)));
sc_str.push_str(&sc.long.map_or(String::new(), |c| format!("--{}, ", c))); sc_str.push_str(&sc.long_flag.map_or(String::new(), |c| format!("--{}, ", c)));
sc_str.push_str(&sc.name); sc_str.push_str(&sc.name);
self.longest = cmp::max(self.longest, str_width(&sc_str)); self.longest = cmp::max(self.longest, str_width(&sc_str));
btm.insert(sc_str, sc.clone()); btm.insert(sc_str, sc.clone());

View file

@ -888,7 +888,7 @@ where
.app .app
.get_subcommands() .get_subcommands()
.fold(Vec::new(), |mut options, sc| { .fold(Vec::new(), |mut options, sc| {
if let Some(long) = sc.long { if let Some(long) = sc.long_flag {
if arg_os.is_prefix_of(long) { if arg_os.is_prefix_of(long) {
options.push(long); options.push(long);
} }
@ -1072,11 +1072,11 @@ where
// Display subcommand name, short and long in usage // Display subcommand name, short and long in usage
let mut sc_names = sc.name.clone(); let mut sc_names = sc.name.clone();
let mut flag_subcmd = false; let mut flag_subcmd = false;
if let Some(l) = sc.long { if let Some(l) = sc.long_flag {
sc_names.push_str(&format!(", --{}", l)); sc_names.push_str(&format!(", --{}", l));
flag_subcmd = true; flag_subcmd = true;
} }
if let Some(s) = sc.short { if let Some(s) = sc.short_flag {
sc_names.push_str(&format!(", -{}", s)); sc_names.push_str(&format!(", -{}", s));
flag_subcmd = true; flag_subcmd = true;
} }