Skip to content

Commit 791bbd6

Browse files
committed
refactor(cli): add comments, change order
1 parent 18ca1b4 commit 791bbd6

1 file changed

Lines changed: 50 additions & 44 deletions

File tree

src/cli.rs

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,43 +76,49 @@ pub struct Cli {
7676
}
7777

7878
impl Cli {
79-
pub fn parse() -> Cli {
79+
/// Parse all command-line arguments from the user into a `Cli` struct
80+
pub fn parse() -> Self {
81+
fn from_arg_matches(subcommand: Subcommand, matches: &ArgMatches) -> Cli {
82+
Cli {
83+
check: (matches!(&subcommand, Subcommand::Format | Subcommand::Update)) && matches.get_flag("check"),
84+
cwd: env::current_dir().unwrap(),
85+
dependencies: get_patterns(matches, "dependencies"),
86+
dependency_types: get_patterns(matches, "dependency-types"),
87+
disable_ansi: matches.get_flag("no-ansi"),
88+
dry_run: (matches!(&subcommand, Subcommand::Fix | Subcommand::Format | Subcommand::Update)) && matches.get_flag("dry-run"),
89+
log_levels: get_log_levels(matches),
90+
packages: get_patterns(matches, "packages"),
91+
show_hints: should_show(matches, "hints"),
92+
show_ignored: should_show(matches, "ignored"),
93+
show_instances: should_show(matches, "instances"),
94+
show_status_codes: should_show(matches, "statuses"),
95+
sort: get_order_by(matches),
96+
source_patterns: get_patterns(matches, "source"),
97+
specifier_types: get_patterns(matches, "specifier-types"),
98+
subcommand,
99+
target: get_target(matches),
100+
}
101+
}
102+
80103
match create().get_matches().subcommand() {
81-
Some(("lint", matches)) => Cli::from_arg_matches(Subcommand::Lint, matches),
82-
Some(("fix", matches)) => Cli::from_arg_matches(Subcommand::Fix, matches),
83-
Some(("format", matches)) => Cli::from_arg_matches(Subcommand::Format, matches),
84-
Some(("update", matches)) => Cli::from_arg_matches(Subcommand::Update, matches),
85-
Some(("list", matches)) => Cli::from_arg_matches(Subcommand::List, matches),
86-
Some(("json", matches)) => Cli::from_arg_matches(Subcommand::Json, matches),
104+
Some(("lint", matches)) => from_arg_matches(Subcommand::Lint, matches),
105+
Some(("fix", matches)) => from_arg_matches(Subcommand::Fix, matches),
106+
Some(("format", matches)) => from_arg_matches(Subcommand::Format, matches),
107+
Some(("update", matches)) => from_arg_matches(Subcommand::Update, matches),
108+
Some(("list", matches)) => from_arg_matches(Subcommand::List, matches),
109+
Some(("json", matches)) => from_arg_matches(Subcommand::Json, matches),
87110
_ => {
88111
std::process::exit(1);
89112
}
90113
}
91114
}
92115

93-
/// Create a new `Cli` from CLI arguments provided by the user
94-
fn from_arg_matches(subcommand: Subcommand, matches: &ArgMatches) -> Self {
95-
Self {
96-
check: (matches!(&subcommand, Subcommand::Format | Subcommand::Update)) && matches.get_flag("check"),
97-
cwd: env::current_dir().unwrap(),
98-
dependencies: get_patterns(matches, "dependencies"),
99-
dependency_types: get_patterns(matches, "dependency-types"),
100-
disable_ansi: matches.get_flag("no-ansi"),
101-
dry_run: (matches!(&subcommand, Subcommand::Fix | Subcommand::Format | Subcommand::Update)) && matches.get_flag("dry-run"),
102-
log_levels: get_log_levels(matches),
103-
packages: get_patterns(matches, "packages"),
104-
show_hints: should_show(matches, "hints"),
105-
show_ignored: should_show(matches, "ignored"),
106-
show_instances: should_show(matches, "instances"),
107-
show_status_codes: should_show(matches, "statuses"),
108-
sort: get_order_by(matches),
109-
source_patterns: get_patterns(matches, "source"),
110-
specifier_types: get_patterns(matches, "specifier-types"),
111-
subcommand,
112-
target: get_target(matches),
113-
}
114-
}
115-
116+
/// Create a `GroupSelector` struct based on the provided command line options
117+
/// which relate to filtering of packages and dependencies.
118+
///
119+
/// `GroupSelector` is the same struct as used by `VersionGroup` and
120+
/// `SemverGroup` and this CLI `GroupSelector`, when configured, serves as a
121+
/// single `VersionGroup` which overrides all those set in config.
116122
pub fn get_filters(&self, packages: &Packages, all_dependency_types: &[DependencyType]) -> Option<GroupSelector> {
117123
if self.dependencies.is_empty() && self.dependency_types.is_empty() && self.packages.is_empty() && self.specifier_types.is_empty() {
118124
None
@@ -130,20 +136,6 @@ impl Cli {
130136
}
131137
}
132138

133-
fn get_target(matches: &ArgMatches) -> UpdateTarget {
134-
matches
135-
.try_get_one::<String>("target")
136-
.ok()
137-
.flatten()
138-
.map(|target| match target.as_str() {
139-
"latest" => UpdateTarget::Latest,
140-
"minor" => UpdateTarget::Minor,
141-
"patch" => UpdateTarget::Patch,
142-
_ => unreachable!(),
143-
})
144-
.unwrap_or(UpdateTarget::Latest)
145-
}
146-
147139
fn create() -> Command {
148140
Command::new(crate_name!())
149141
.about(crate_description!())
@@ -576,6 +568,20 @@ fn get_patterns(matches: &ArgMatches, option_name: &str) -> Vec<String> {
576568
.unwrap_or_default()
577569
}
578570

571+
fn get_target(matches: &ArgMatches) -> UpdateTarget {
572+
matches
573+
.try_get_one::<String>("target")
574+
.ok()
575+
.flatten()
576+
.map(|target| match target.as_str() {
577+
"latest" => UpdateTarget::Latest,
578+
"minor" => UpdateTarget::Minor,
579+
"patch" => UpdateTarget::Patch,
580+
_ => unreachable!(),
581+
})
582+
.unwrap_or(UpdateTarget::Latest)
583+
}
584+
579585
fn should_show(matches: &ArgMatches, name: &str) -> bool {
580586
matches
581587
.try_get_many::<String>("show")

0 commit comments

Comments
 (0)