mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
use match guard to minimize
This commit is contained in:
parent
50c8bd4c6b
commit
f334ae3149
1 changed files with 29 additions and 30 deletions
|
@ -551,42 +551,41 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let zero_terminated = matches.get_flag(options::ZERO_TERMINATED);
|
let zero_terminated = matches.get_flag(options::ZERO_TERMINATED);
|
||||||
|
|
||||||
match matches.get_one::<String>(options::DELIMITER).map(|s| s.as_str()) {
|
match matches.get_one::<String>(options::DELIMITER).map(|s| s.as_str()) {
|
||||||
Some(mut delim) => {
|
Some(_) if whitespace_delimited => {
|
||||||
if whitespace_delimited {
|
|
||||||
Err("invalid input: Only one of --delimiter (-d) or -w option can be specified".into())
|
Err("invalid input: Only one of --delimiter (-d) or -w option can be specified".into())
|
||||||
}
|
}
|
||||||
else {
|
Some(mut delim) => {
|
||||||
// GNU's `cut` supports `-d=` to set the delimiter to `=`.
|
// GNU's `cut` supports `-d=` to set the delimiter to `=`.
|
||||||
// Clap parsing is limited in this situation, see:
|
// Clap parsing is limited in this situation, see:
|
||||||
// https://github.com/uutils/coreutils/issues/2424#issuecomment-863825242
|
// https://github.com/uutils/coreutils/issues/2424#issuecomment-863825242
|
||||||
if delimiter_is_equal {
|
if delimiter_is_equal {
|
||||||
delim = "=";
|
delim = "=";
|
||||||
} else if delim == "''" {
|
} else if delim == "''" {
|
||||||
// treat `''` as empty delimiter
|
// treat `''` as empty delimiter
|
||||||
delim = "";
|
delim = "";
|
||||||
}
|
}
|
||||||
if delim.chars().count() > 1 {
|
if delim.chars().count() > 1 {
|
||||||
Err("invalid input: The '--delimiter' ('-d') option expects empty or 1 character long, but was provided a value 2 characters or longer".into())
|
Err("invalid input: The '--delimiter' ('-d') option expects empty or 1 character long, but was provided a value 2 characters or longer".into())
|
||||||
|
} else {
|
||||||
|
let delim = if delim.is_empty() {
|
||||||
|
"\0".to_owned()
|
||||||
} else {
|
} else {
|
||||||
let delim = if delim.is_empty() {
|
delim.to_owned()
|
||||||
"\0".to_owned()
|
};
|
||||||
} else {
|
|
||||||
delim.to_owned()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Mode::Fields(
|
Ok(Mode::Fields(
|
||||||
ranges,
|
ranges,
|
||||||
FieldOptions {
|
FieldOptions {
|
||||||
delimiter: delim,
|
delimiter: delim,
|
||||||
out_delimiter: out_delim,
|
out_delimiter: out_delim,
|
||||||
only_delimited,
|
only_delimited,
|
||||||
whitespace_delimited,
|
whitespace_delimited,
|
||||||
zero_terminated,
|
zero_terminated,
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None => Ok(Mode::Fields(
|
None => Ok(Mode::Fields(
|
||||||
ranges,
|
ranges,
|
||||||
FieldOptions {
|
FieldOptions {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue