Skip to content

Commit

Permalink
Merge pull request #2871 from jfinkels/split-settings-methods
Browse files Browse the repository at this point in the history
split: add a method to convert ArgMatches to Settings
  • Loading branch information
sylvestre authored Jan 30, 2022
2 parents 81b2a24 + 58f2000 commit 57dc11e
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,11 @@ size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = usage();
let long_usage = get_long_usage();

let matches = uu_app()
.override_usage(&usage[..])
.after_help(&long_usage[..])
.get_matches_from(args);

let mut settings = Settings {
prefix: "".to_owned(),
numeric_suffix: false,
suffix_length: 0,
additional_suffix: "".to_owned(),
input: "".to_owned(),
filter: None,
strategy: Strategy::Lines(1000),
verbose: false,
};

settings.suffix_length = matches
.value_of(OPT_SUFFIX_LENGTH)
.unwrap()
.parse()
.unwrap_or_else(|_| panic!("Invalid number for {}", OPT_SUFFIX_LENGTH));

settings.numeric_suffix = matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0;
settings.additional_suffix = matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned();

settings.verbose = matches.occurrences_of("verbose") > 0;
settings.strategy = Strategy::from(&matches)?;
settings.input = matches.value_of(ARG_INPUT).unwrap().to_owned();
settings.prefix = matches.value_of(ARG_PREFIX).unwrap().to_owned();

if matches.occurrences_of(OPT_FILTER) > 0 {
if cfg!(windows) {
// see https://github.com/rust-lang/rust/issues/29494
return Err(USimpleError::new(
-1,
format!("{} is currently not supported in this platform", OPT_FILTER),
));
} else {
settings.filter = Some(matches.value_of(OPT_FILTER).unwrap().to_owned());
}
}

let settings = Settings::from(matches)?;
split(settings)
}

Expand Down Expand Up @@ -251,6 +213,10 @@ impl Strategy {
}
}

/// Parameters that control how a file gets split.
///
/// You can convert an [`ArgMatches`] instance into a [`Settings`]
/// instance by calling [`Settings::from`].
struct Settings {
prefix: String,
numeric_suffix: bool,
Expand All @@ -263,6 +229,36 @@ struct Settings {
verbose: bool,
}

impl Settings {
/// Parse a strategy from the command-line arguments.
fn from(matches: ArgMatches) -> UResult<Self> {
let result = Settings {
suffix_length: matches
.value_of(OPT_SUFFIX_LENGTH)
.unwrap()
.parse()
.unwrap_or_else(|_| panic!("Invalid number for {}", OPT_SUFFIX_LENGTH)),
numeric_suffix: matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0,
additional_suffix: matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(),
verbose: matches.occurrences_of("verbose") > 0,
strategy: Strategy::from(&matches)?,
input: matches.value_of(ARG_INPUT).unwrap().to_owned(),
prefix: matches.value_of(ARG_PREFIX).unwrap().to_owned(),
filter: matches.value_of(OPT_FILTER).map(|s| s.to_owned()),
};
#[cfg(windows)]
if result.filter.is_some() {
// see https://github.com/rust-lang/rust/issues/29494
return Err(USimpleError::new(
-1,
format!("{} is currently not supported in this platform", OPT_FILTER),
));
}

Ok(result)
}
}

trait Splitter {
// Consume as much as possible from `reader` so as to saturate `writer`.
// Equivalent to finishing one of the part files. Returns the number of
Expand Down

0 comments on commit 57dc11e

Please sign in to comment.