mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
Merge pull request #2713 from jfinkels/head-default-options
head: use default() instead of new() for options
This commit is contained in:
commit
0a14fba3db
1 changed files with 13 additions and 28 deletions
|
@ -108,6 +108,12 @@ enum Modes {
|
||||||
Bytes(usize),
|
Bytes(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Modes {
|
||||||
|
fn default() -> Self {
|
||||||
|
Modes::Lines(10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_mode<F>(src: &str, closure: F) -> Result<(Modes, bool), String>
|
fn parse_mode<F>(src: &str, closure: F) -> Result<(Modes, bool), String>
|
||||||
where
|
where
|
||||||
F: FnOnce(usize) -> Modes,
|
F: FnOnce(usize) -> Modes,
|
||||||
|
@ -144,7 +150,7 @@ fn arg_iterate<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Default)]
|
||||||
struct HeadOptions {
|
struct HeadOptions {
|
||||||
pub quiet: bool,
|
pub quiet: bool,
|
||||||
pub verbose: bool,
|
pub verbose: bool,
|
||||||
|
@ -155,22 +161,11 @@ struct HeadOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HeadOptions {
|
impl HeadOptions {
|
||||||
pub fn new() -> HeadOptions {
|
|
||||||
HeadOptions {
|
|
||||||
quiet: false,
|
|
||||||
verbose: false,
|
|
||||||
zeroed: false,
|
|
||||||
all_but_last: false,
|
|
||||||
mode: Modes::Lines(10),
|
|
||||||
files: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///Construct options from matches
|
///Construct options from matches
|
||||||
pub fn get_from(args: impl uucore::Args) -> Result<Self, String> {
|
pub fn get_from(args: impl uucore::Args) -> Result<Self, String> {
|
||||||
let matches = uu_app().get_matches_from(arg_iterate(args)?);
|
let matches = uu_app().get_matches_from(arg_iterate(args)?);
|
||||||
|
|
||||||
let mut options = HeadOptions::new();
|
let mut options: HeadOptions = Default::default();
|
||||||
|
|
||||||
options.quiet = matches.is_present(options::QUIET_NAME);
|
options.quiet = matches.is_present(options::QUIET_NAME);
|
||||||
options.verbose = matches.is_present(options::VERBOSE_NAME);
|
options.verbose = matches.is_present(options::VERBOSE_NAME);
|
||||||
|
@ -197,12 +192,6 @@ impl HeadOptions {
|
||||||
Ok(options)
|
Ok(options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// to make clippy shut up
|
|
||||||
impl Default for HeadOptions {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_n_bytes<R>(input: R, n: usize) -> std::io::Result<()>
|
fn read_n_bytes<R>(input: R, n: usize) -> std::io::Result<()>
|
||||||
where
|
where
|
||||||
|
@ -523,17 +512,13 @@ mod tests {
|
||||||
assert!(options("-c IsThisJustFantasy").is_err());
|
assert!(options("-c IsThisJustFantasy").is_err());
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[allow(clippy::bool_comparison)]
|
|
||||||
fn test_options_correct_defaults() {
|
fn test_options_correct_defaults() {
|
||||||
let opts = HeadOptions::new();
|
let opts: HeadOptions = Default::default();
|
||||||
let opts2: HeadOptions = Default::default();
|
|
||||||
|
|
||||||
assert_eq!(opts, opts2);
|
assert!(!opts.verbose);
|
||||||
|
assert!(!opts.quiet);
|
||||||
assert!(opts.verbose == false);
|
assert!(!opts.zeroed);
|
||||||
assert!(opts.quiet == false);
|
assert!(!opts.all_but_last);
|
||||||
assert!(opts.zeroed == false);
|
|
||||||
assert!(opts.all_but_last == false);
|
|
||||||
assert_eq!(opts.mode, Modes::Lines(10));
|
assert_eq!(opts.mode, Modes::Lines(10));
|
||||||
assert!(opts.files.is_empty());
|
assert!(opts.files.is_empty());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue