Skip to content

Commit

Permalink
fix(Parser): 🔥 overwrited help to be able to do a -h
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinFillon committed Sep 8, 2023
1 parent ee6c2a0 commit 9d88912
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() {

let cli = Opts::parse();

let mut input_paths: Vec<&OsStr> = cli.paths.iter().map(|p| p.as_os_str()).collect();
let mut input_paths: Vec<&OsStr> = cli.paths.iter().map(OsString::as_os_str).collect();
if input_paths.is_empty() {
input_paths.push(OsStr::new("."));
}
Expand Down
22 changes: 15 additions & 7 deletions src/options/dir_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl DirAction {
if tree && can_tree {
// Tree is only appropriate in details mode, so this has to
// examine the View, which should have already been deduced by now
Ok(Self::Recurse(RecurseOptions::deduce(matches, true)?))
Ok(Self::Recurse(RecurseOptions::deduce(matches, true)))
}
else if recurse {
Ok(Self::Recurse(RecurseOptions::deduce(matches, false)?))
Ok(Self::Recurse(RecurseOptions::deduce(matches, false)))
}
else if as_file {
Ok(Self::AsFile)
Expand All @@ -54,20 +54,20 @@ impl RecurseOptions {
/// flag’s value, and whether the `--tree` flag was passed, which was
/// determined earlier. The maximum level should be a number, and this
/// will fail with an `Err` if it isn’t.
pub fn deduce(matches: &Opts, tree: bool) -> Result<Self, OptionsError> {
pub fn deduce(matches: &Opts, tree: bool) -> Self {
let max_depth = matches.level;
match max_depth {
Some(d) => {
Ok(Self {
Self {
tree,
max_depth: Some(d),
})
}
},
None => {
Ok(Self {
Self {
tree,
max_depth: None,
})
}
},
}
}
Expand Down Expand Up @@ -129,6 +129,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};

assert_eq!(DirAction::deduce(&matches, false, false).unwrap(), DirAction::List);
Expand Down Expand Up @@ -186,6 +187,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};
assert_eq!(DirAction::deduce(&matches, false, false).unwrap(), DirAction::Recurse(RecurseOptions {
tree: false,
Expand Down Expand Up @@ -245,6 +247,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};
assert_eq!(DirAction::deduce(&matches, true, false).unwrap(), DirAction::Recurse(RecurseOptions {
tree: true,
Expand Down Expand Up @@ -304,6 +307,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};
assert_eq!(DirAction::deduce(&matches, false, false).unwrap(), DirAction::AsFile);
}
Expand Down Expand Up @@ -360,6 +364,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};

assert!(DirAction::deduce(&matches, false, true).is_err());
Expand Down Expand Up @@ -417,6 +422,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};

assert!(DirAction::deduce(&matches, false, true).is_err());
Expand Down Expand Up @@ -474,6 +480,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};

assert!(DirAction::deduce(&matches, false, true).is_err());
Expand Down Expand Up @@ -531,6 +538,7 @@ mod test {
hyperlink: 0,
octal: 0,
security_context: 0,
help: false,
};

assert_eq!(DirAction::deduce(&matches, true, false).unwrap(), DirAction::Recurse(RecurseOptions {
Expand Down
4 changes: 2 additions & 2 deletions src/options/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub enum NumberSource {
impl fmt::Display for NumberSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Var(s) => write!(f, "variable {}", s),
Self::Arg(s) => write!(f, "argument {}", s),
Self::Var(s) => write!(f, "variable {s}"),
Self::Arg(s) => write!(f, "argument {s}"),
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/options/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ use crate::options::parser::Opts;

impl Options {
pub fn deduce<V: Vars>(matches: &Opts, vars: &V) -> Result<Self, OptionsError> {
let classify = Classify::deduce(matches)?;
let classify = Classify::deduce(matches);
let show_icons = ShowIcons::deduce(matches, vars)?;
let embed_hyperlinks = EmbedHyperlinks::deduce(matches)?;
let embed_hyperlinks = EmbedHyperlinks::deduce(matches);

Ok(Self { classify, show_icons, embed_hyperlinks })
}
}

impl Classify {
fn deduce(matches: &Opts) -> Result<Self, OptionsError> {
if matches.classify > 0 { Ok(Self::AddFileIndicators) }
else { Ok(Self::JustFilenames) }
fn deduce(matches: &Opts) -> Self {
if matches.classify > 0 {
return Self::AddFileIndicators;
}
Self::JustFilenames
}
}

Expand All @@ -45,8 +47,10 @@ impl ShowIcons {
}

impl EmbedHyperlinks {
fn deduce(matches: &Opts) -> Result<Self, OptionsError> {
if matches.hyperlink > 0 { Ok(Self::On) }
else { Ok(Self::Off) }
fn deduce(matches: &Opts) -> Self {
if matches.hyperlink > 0 {
return Self::On;
}
Self::Off
}
}
12 changes: 5 additions & 7 deletions src/options/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl FileFilter {
sort_field: SortField::deduce(matches)?,
dot_filter: DotFilter::deduce(matches)?,
ignore_patterns: IgnorePatterns::deduce(matches)?,
git_ignore: GitIgnore::deduce(matches)?,
git_ignore: GitIgnore::deduce(matches),
})
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ impl DotFilter {
(0, 0) => Ok(Self::JustFiles),

// either a single --all or at least one --almost-all is given
(1, _) | (0, _) => Ok(Self::Dotfiles),
(1 | 0, _) => Ok(Self::Dotfiles),
// more than one --all
(_, _) => if matches.tree > 0 {
Err(OptionsError::TreeAllAll)
Expand Down Expand Up @@ -200,13 +200,11 @@ impl IgnorePatterns {


impl GitIgnore {
pub fn deduce(matches: &Opts) -> Result<Self, OptionsError> {
pub fn deduce(matches: &Opts) -> Self {
if matches.git_ignore > 0 {
Ok(Self::CheckAndIgnore)
}
else {
Ok(Self::Off)
return Self::CheckAndIgnore;
}
Self::Off
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Options {
)));
}

let strictness = match vars.get(&vars::EXA_STRICT) {
let strictness = match vars.get(vars::EXA_STRICT) {
None => false,
Some(s) => ! s.is_empty()
};
Expand Down
5 changes: 4 additions & 1 deletion src/options/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ffi::OsString;

#[derive(Parser)]
#[command(author, version, about, long_about = None)] // Read from `Cargo.toml`
#[clap(disable_help_flag = true)]
pub struct Opts {
pub paths: Vec<OsString>,
#[clap(short, long, action = clap::ArgAction::Count)]
Expand Down Expand Up @@ -57,7 +58,7 @@ pub struct Opts {
pub group: u8,
#[clap(short = 'n', long, action = clap::ArgAction::Count)]
pub numeric: u8,
#[clap(long, action = clap::ArgAction::Count)]
#[clap(short = 'h', long, action = clap::ArgAction::Count)]
pub header: u8,
#[clap(long, action = clap::ArgAction::Count)]
pub icons: u8,
Expand Down Expand Up @@ -101,4 +102,6 @@ pub struct Opts {
pub security_context: u8,
#[clap(short = '@', long, action = clap::ArgAction::Count)]
pub extended: u8,
#[clap(long, action = clap::ArgAction::Help)]
pub help: bool,
}
10 changes: 4 additions & 6 deletions src/options/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::parser::Opts;
impl Options {
pub fn deduce<V: Vars>(matches: &Opts, vars: &V) -> Result<Self, OptionsError> {
let use_colours = UseColours::deduce(matches, vars)?;
let colour_scale = ColourScale::deduce(matches)?;
let colour_scale = ColourScale::deduce(matches);

let definitions = if use_colours == UseColours::Never {
Definitions::default()
Expand Down Expand Up @@ -47,13 +47,11 @@ impl UseColours {


impl ColourScale {
fn deduce(matches: &Opts) -> Result<Self, OptionsError> {
fn deduce(matches: &Opts) -> Self {
if matches.color_scale > 0{
Ok(Self::Gradient)
}
else {
Ok(Self::Fixed)
return Self::Gradient;
}
Self::Fixed
}
}

Expand Down
Loading

0 comments on commit 9d88912

Please sign in to comment.