mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #3111 from jfinkels/split-suffix-contains-separator
split: error when --additional-suffix contains /
This commit is contained in:
commit
748e6e742a
2 changed files with 25 additions and 2 deletions
|
@ -264,6 +264,9 @@ enum SettingsError {
|
||||||
/// Invalid suffix length parameter.
|
/// Invalid suffix length parameter.
|
||||||
SuffixLength(String),
|
SuffixLength(String),
|
||||||
|
|
||||||
|
/// Suffix contains a directory separator, which is not allowed.
|
||||||
|
SuffixContainsSeparator(String),
|
||||||
|
|
||||||
/// The `--filter` option is not supported on Windows.
|
/// The `--filter` option is not supported on Windows.
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
NotSupported,
|
NotSupported,
|
||||||
|
@ -272,7 +275,10 @@ enum SettingsError {
|
||||||
impl SettingsError {
|
impl SettingsError {
|
||||||
/// Whether the error demands a usage message.
|
/// Whether the error demands a usage message.
|
||||||
fn requires_usage(&self) -> bool {
|
fn requires_usage(&self) -> bool {
|
||||||
matches!(self, Self::Strategy(StrategyError::MultipleWays))
|
matches!(
|
||||||
|
self,
|
||||||
|
Self::Strategy(StrategyError::MultipleWays) | Self::SuffixContainsSeparator(_)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,6 +287,11 @@ impl fmt::Display for SettingsError {
|
||||||
match self {
|
match self {
|
||||||
Self::Strategy(e) => e.fmt(f),
|
Self::Strategy(e) => e.fmt(f),
|
||||||
Self::SuffixLength(s) => write!(f, "invalid suffix length: {}", s.quote()),
|
Self::SuffixLength(s) => write!(f, "invalid suffix length: {}", s.quote()),
|
||||||
|
Self::SuffixContainsSeparator(s) => write!(
|
||||||
|
f,
|
||||||
|
"invalid suffix {}, contains directory separator",
|
||||||
|
s.quote()
|
||||||
|
),
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
Self::NotSupported => write!(
|
Self::NotSupported => write!(
|
||||||
f,
|
f,
|
||||||
|
@ -294,13 +305,17 @@ impl fmt::Display for SettingsError {
|
||||||
impl Settings {
|
impl Settings {
|
||||||
/// Parse a strategy from the command-line arguments.
|
/// Parse a strategy from the command-line arguments.
|
||||||
fn from(matches: &ArgMatches) -> Result<Self, SettingsError> {
|
fn from(matches: &ArgMatches) -> Result<Self, SettingsError> {
|
||||||
|
let additional_suffix = matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_string();
|
||||||
|
if additional_suffix.contains('/') {
|
||||||
|
return Err(SettingsError::SuffixContainsSeparator(additional_suffix));
|
||||||
|
}
|
||||||
let suffix_length_str = matches.value_of(OPT_SUFFIX_LENGTH).unwrap();
|
let suffix_length_str = matches.value_of(OPT_SUFFIX_LENGTH).unwrap();
|
||||||
let result = Self {
|
let result = Self {
|
||||||
suffix_length: suffix_length_str
|
suffix_length: suffix_length_str
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|_| SettingsError::SuffixLength(suffix_length_str.to_string()))?,
|
.map_err(|_| SettingsError::SuffixLength(suffix_length_str.to_string()))?,
|
||||||
numeric_suffix: matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0,
|
numeric_suffix: matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0,
|
||||||
additional_suffix: matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(),
|
additional_suffix,
|
||||||
verbose: matches.occurrences_of("verbose") > 0,
|
verbose: matches.occurrences_of("verbose") > 0,
|
||||||
strategy: Strategy::from(matches).map_err(SettingsError::Strategy)?,
|
strategy: Strategy::from(matches).map_err(SettingsError::Strategy)?,
|
||||||
input: matches.value_of(ARG_INPUT).unwrap().to_owned(),
|
input: matches.value_of(ARG_INPUT).unwrap().to_owned(),
|
||||||
|
|
|
@ -228,6 +228,14 @@ fn test_split_additional_suffix() {
|
||||||
assert_eq!(glob.collate(), at.read_bytes(name));
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_additional_suffix_no_slash() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--additional-suffix", "a/b"])
|
||||||
|
.fails()
|
||||||
|
.usage_error("invalid suffix 'a/b', contains directory separator");
|
||||||
|
}
|
||||||
|
|
||||||
// note: the test_filter* tests below are unix-only
|
// note: the test_filter* tests below are unix-only
|
||||||
// windows support has been waived for now because of the difficulty of getting
|
// windows support has been waived for now because of the difficulty of getting
|
||||||
// the `cmd` call right
|
// the `cmd` call right
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue