Skip to content

Commit 55f8a26

Browse files
committed
refactor(cli): Centralize reading of verbose
1 parent 6b8e192 commit 55f8a26

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/bin/cargo/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
112112
return Ok(());
113113
}
114114

115-
let is_verbose = expanded_args.occurrences_of("verbose") > 0;
115+
let is_verbose = expanded_args.verbose() > 0;
116116
if expanded_args.is_present("version") {
117117
let version = get_version_string(is_verbose);
118118
drop_print!(config, "{}", version);
@@ -331,7 +331,7 @@ fn config_configure(
331331
._is_valid_arg("target-dir")
332332
.then(|| subcommand_args.value_of_path("target-dir", config))
333333
.flatten();
334-
let verbose = global_args.verbose + args.occurrences_of("verbose") as u32;
334+
let verbose = global_args.verbose + args.verbose();
335335
// quiet is unusual because it is redefined in some subcommands in order
336336
// to provide custom help text.
337337
let quiet = args.is_present("quiet")
@@ -389,7 +389,7 @@ struct GlobalArgs {
389389
impl GlobalArgs {
390390
fn new(args: &ArgMatches) -> GlobalArgs {
391391
GlobalArgs {
392-
verbose: args.occurrences_of("verbose") as u32,
392+
verbose: args.verbose(),
393393
quiet: args.is_present("quiet"),
394394
color: args.value_of("color").map(|s| s.to_string()),
395395
frozen: args.is_present("frozen"),

src/bin/cargo/commands/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn cli() -> App {
104104

105105
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
106106
if args.is_present("version") {
107-
let verbose = args.occurrences_of("verbose") > 0;
107+
let verbose = args.verbose() > 0;
108108
let version = cli::get_version_string(verbose);
109109
cargo::drop_print!(config, "{}", version);
110110
return Ok(());

src/bin/cargo/commands/version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn cli() -> App {
99
}
1010

1111
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
12-
let verbose = args.occurrences_of("verbose") > 0;
12+
let verbose = args.verbose() > 0;
1313
let version = cli::get_version_string(verbose);
1414
cargo::drop_print!(config, "{}", version);
1515
Ok(())

src/cargo/util/command_prelude.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ pub trait ArgMatchesExt {
360360
self.value_of_u32("jobs")
361361
}
362362

363+
fn verbose(&self) -> u32 {
364+
self._occurrences("verbose")
365+
}
366+
363367
fn keep_going(&self) -> bool {
364368
self._is_present("keep-going")
365369
}
@@ -729,6 +733,8 @@ pub trait ArgMatchesExt {
729733

730734
fn _values_of_os(&self, name: &str) -> Vec<OsString>;
731735

736+
fn _occurrences(&self, name: &str) -> u32;
737+
732738
fn _is_present(&self, name: &str) -> bool;
733739

734740
fn _is_valid_arg(&self, name: &str) -> bool;
@@ -757,6 +763,10 @@ impl<'a> ArgMatchesExt for ArgMatches {
757763
.collect()
758764
}
759765

766+
fn _occurrences(&self, name: &str) -> u32 {
767+
self.occurrences_of(name) as u32
768+
}
769+
760770
fn _is_present(&self, name: &str) -> bool {
761771
self.is_present(name)
762772
}

0 commit comments

Comments
 (0)