numfmt: allow ' ' as field separator

This commit is contained in:
Daniel Hofstetter 2022-11-01 10:04:46 +01:00
parent 614c367e46
commit 2d4810b91b
3 changed files with 8 additions and 3 deletions

View file

@ -160,7 +160,7 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
let fields = args.get_one::<String>(options::FIELD).unwrap().as_str();
// a lone "-" means "all fields", even as part of a list of fields
let fields = if fields.split(',').any(|x| x == "-") {
let fields = if fields.split(&[',', ' ']).any(|x| x == "-") {
vec![Range {
low: 1,
high: std::usize::MAX,

View file

@ -75,7 +75,7 @@ impl Range {
pub fn from_list(list: &str) -> Result<Vec<Self>, String> {
let mut ranges = Vec::new();
for item in list.split(',') {
for item in list.split(&[',', ' ']) {
let range_item = FromStr::from_str(item)
.map_err(|e| format!("range {} was invalid: {}", item.quote(), e))?;
ranges.push(range_item);

View file

@ -373,6 +373,11 @@ fn test_format_selected_fields() {
.args(&["--from=auto", "--field", "1,4,3", "1K 2K 3K 4K 5K 6K"])
.succeeds()
.stdout_only("1000 2K 3000 4000 5K 6K\n");
new_ucmd!()
.args(&["--from=auto", "--field", "1,4 3", "1K 2K 3K 4K 5K 6K"])
.succeeds()
.stdout_only("1000 2K 3000 4000 5K 6K\n");
}
#[test]
@ -401,7 +406,7 @@ fn test_format_selected_field_range() {
#[test]
fn test_format_all_fields() {
let all_fields_patterns = vec!["-", "-,3", "3,-", "1,-,3"];
let all_fields_patterns = vec!["-", "-,3", "3,-", "1,-,3", "- 3"];
for pattern in all_fields_patterns {
new_ucmd!()