Skip to content

Commit

Permalink
refactor: param data struct (#218)
Browse files Browse the repository at this point in the history
* refactor:  param data struct

* refactor hyphens
  • Loading branch information
sigoden authored Aug 3, 2023
1 parent a3ba6d0 commit 130f289
Show file tree
Hide file tree
Showing 6 changed files with 463 additions and 422 deletions.
38 changes: 19 additions & 19 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ impl Command {
if param.is_option() {
root_data.borrow_mut().add_param_fn(
position,
&param.default_fn,
&param.choices_fn,
param.default_fn(),
param.choice_fn(),
);
}
cmd.names_checker.check_flag_option(&param, position)?;
Expand All @@ -166,8 +166,8 @@ impl Command {
let cmd = Self::get_cmd(&mut root_cmd, param.tag_name(), position)?;
root_data.borrow_mut().add_param_fn(
position,
&param.default_fn,
&param.choices_fn,
param.default_fn(),
param.choice_fn(),
);
cmd.add_positional_param(param, position)?;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ impl Command {
let required_options: Vec<String> = self
.flag_option_params
.iter()
.filter(|v| v.required)
.filter(|v| v.required())
.map(|v| v.render_name_values())
.collect();
if self.flag_option_params.len() != required_options.len() {
Expand Down Expand Up @@ -339,10 +339,10 @@ impl Command {
}
let mut list = vec![];
let mut any_describe = false;
let mut double_dash = true;
let mut single_hyphen = false;
for param in self.flag_option_params.iter() {
if param.dashes == "-" {
double_dash = false;
if param.single_hyphen {
single_hyphen = true;
}
let value = param.render_body();
let describe = param.render_describe();
Expand All @@ -351,8 +351,8 @@ impl Command {
}
list.push((value, describe));
}
self.add_help_flag(&mut list, double_dash, any_describe);
self.add_version_flag(&mut list, double_dash, any_describe);
self.add_help_flag(&mut list, single_hyphen, any_describe);
self.add_version_flag(&mut list, single_hyphen, any_describe);
output.push("OPTIONS:".to_string());
let value_size = list.iter().map(|v| v.0.len()).max().unwrap_or_default() + 2;
for (value, describe) in list {
Expand Down Expand Up @@ -503,7 +503,7 @@ impl Command {
&& self
.positional_params
.first()
.map(|v| v.terminated)
.map(|v| v.terminated())
.unwrap_or_default()
}

Expand Down Expand Up @@ -548,18 +548,18 @@ impl Command {
fn add_help_flag(
&self,
list: &mut Vec<(String, String)>,
double_dash: bool,
single_hyphen: bool,
any_describe: bool,
) {
if self.find_flag_option("help").is_some() {
return;
}
let dashes = if double_dash { "--" } else { " -" };
let hyphens = if single_hyphen { " -" } else { "--" };
list.push((
if self.match_help_short_name() {
format!("-h, {}help", dashes)
format!("-h, {}help", hyphens)
} else {
format!(" {}help", dashes)
format!(" {}help", hyphens)
},
if any_describe {
"Print help".into()
Expand All @@ -572,7 +572,7 @@ impl Command {
fn add_version_flag(
&self,
list: &mut Vec<(String, String)>,
double_dash: bool,
single_hyphen: bool,
any_describe: bool,
) {
if self.version.is_none() {
Expand All @@ -581,12 +581,12 @@ impl Command {
if self.find_flag_option("version").is_some() {
return;
}
let dashes = if double_dash { "--" } else { " -" };
let hyphens = if single_hyphen { " -" } else { "--" };
list.push((
if self.match_version_short_name() {
format!("-V, {}version", dashes)
format!("-V, {}version", hyphens)
} else {
format!(" {}version", dashes)
format!(" {}version", hyphens)
},
if any_describe {
"Print version".into()
Expand Down
16 changes: 8 additions & 8 deletions src/command/root_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ pub(crate) struct RootData {
pub(crate) cmd_fns: HashMap<String, Position>,
pub(crate) cmd_pos: usize,
pub(crate) default_fns: Vec<(String, Position)>,
pub(crate) choices_fns: Vec<(String, Position)>,
pub(crate) choice_fns: Vec<(String, Position)>,
}

impl RootData {
pub(crate) fn add_param_fn(
&mut self,
position: usize,
default_fn: &Option<String>,
choices_fn: &Option<(String, bool)>,
default_fn: Option<&String>,
choice_fn: Option<(&String, &bool)>,
) {
if let Some(default_fn) = default_fn.as_ref() {
self.default_fns.push((default_fn.to_string(), position));
if let Some(f) = default_fn {
self.default_fns.push((f.to_string(), position));
}
if let Some((choices_fn, _)) = choices_fn.as_ref() {
self.choices_fns.push((choices_fn.to_string(), position));
if let Some((f, _)) = choice_fn {
self.choice_fns.push((f.to_string(), position));
}
}

Expand All @@ -34,7 +34,7 @@ impl RootData {
bail!("{}(line {}) is missing", name, pos,)
}
}
for (name, pos) in self.choices_fns.iter() {
for (name, pos) in self.choice_fns.iter() {
if !self.fns.contains_key(name) {
bail!("{}(line {}) is missing", name, pos,)
}
Expand Down
Loading

0 comments on commit 130f289

Please sign in to comment.