refactor(cli): Centralize reading of verbose

This commit is contained in:
Ed Page 2022-06-09 16:35:08 -05:00
parent 6b8e192226
commit 55f8a260e0
4 changed files with 15 additions and 5 deletions

View file

@ -112,7 +112,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
return Ok(());
}
let is_verbose = expanded_args.occurrences_of("verbose") > 0;
let is_verbose = expanded_args.verbose() > 0;
if expanded_args.is_present("version") {
let version = get_version_string(is_verbose);
drop_print!(config, "{}", version);
@ -331,7 +331,7 @@ fn config_configure(
._is_valid_arg("target-dir")
.then(|| subcommand_args.value_of_path("target-dir", config))
.flatten();
let verbose = global_args.verbose + args.occurrences_of("verbose") as u32;
let verbose = global_args.verbose + args.verbose();
// quiet is unusual because it is redefined in some subcommands in order
// to provide custom help text.
let quiet = args.is_present("quiet")
@ -389,7 +389,7 @@ struct GlobalArgs {
impl GlobalArgs {
fn new(args: &ArgMatches) -> GlobalArgs {
GlobalArgs {
verbose: args.occurrences_of("verbose") as u32,
verbose: args.verbose(),
quiet: args.is_present("quiet"),
color: args.value_of("color").map(|s| s.to_string()),
frozen: args.is_present("frozen"),

View file

@ -104,7 +104,7 @@ pub fn cli() -> App {
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
if args.is_present("version") {
let verbose = args.occurrences_of("verbose") > 0;
let verbose = args.verbose() > 0;
let version = cli::get_version_string(verbose);
cargo::drop_print!(config, "{}", version);
return Ok(());

View file

@ -9,7 +9,7 @@ pub fn cli() -> App {
}
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let verbose = args.occurrences_of("verbose") > 0;
let verbose = args.verbose() > 0;
let version = cli::get_version_string(verbose);
cargo::drop_print!(config, "{}", version);
Ok(())

View file

@ -360,6 +360,10 @@ pub trait ArgMatchesExt {
self.value_of_u32("jobs")
}
fn verbose(&self) -> u32 {
self._occurrences("verbose")
}
fn keep_going(&self) -> bool {
self._is_present("keep-going")
}
@ -729,6 +733,8 @@ pub trait ArgMatchesExt {
fn _values_of_os(&self, name: &str) -> Vec<OsString>;
fn _occurrences(&self, name: &str) -> u32;
fn _is_present(&self, name: &str) -> bool;
fn _is_valid_arg(&self, name: &str) -> bool;
@ -757,6 +763,10 @@ impl<'a> ArgMatchesExt for ArgMatches {
.collect()
}
fn _occurrences(&self, name: &str) -> u32 {
self.occurrences_of(name) as u32
}
fn _is_present(&self, name: &str) -> bool {
self.is_present(name)
}