Merge pull request #4094 from cakebaker/numfmt_field_range_19

numfmt: allow "-" in field list
This commit is contained in:
Sylvestre Ledru 2022-10-31 18:39:48 +01:00 committed by GitHub
commit 614c367e46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View file

@ -158,12 +158,15 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
Ok(0)
}?;
let fields = match args.get_one::<String>(options::FIELD).unwrap().as_str() {
"-" => vec![Range {
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 == "-") {
vec![Range {
low: 1,
high: std::usize::MAX,
}],
v => Range::from_list(v)?,
}]
} else {
Range::from_list(fields)?
};
let format = match args.get_one::<String>(options::FORMAT) {

View file

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