Skip to content

Commit 1b1e862

Browse files
binhvqsylvestre
authored andcommitted
nl: accept hyphen-leading option values
1 parent aa68999 commit 1b1e862

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/uu/nl/src/nl.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,15 @@ pub fn uu_app() -> Command {
323323
Arg::new(options::BODY_NUMBERING)
324324
.short('b')
325325
.long(options::BODY_NUMBERING)
326+
.allow_hyphen_values(true)
326327
.help(translate!("nl-help-body-numbering"))
327328
.value_name("STYLE"),
328329
)
329330
.arg(
330331
Arg::new(options::SECTION_DELIMITER)
331332
.short('d')
332333
.long(options::SECTION_DELIMITER)
334+
.allow_hyphen_values(true)
333335
.help(translate!("nl-help-section-delimiter"))
334336
.value_parser(clap::value_parser!(OsString))
335337
.value_name("CC"),
@@ -338,13 +340,15 @@ pub fn uu_app() -> Command {
338340
Arg::new(options::FOOTER_NUMBERING)
339341
.short('f')
340342
.long(options::FOOTER_NUMBERING)
343+
.allow_hyphen_values(true)
341344
.help(translate!("nl-help-footer-numbering"))
342345
.value_name("STYLE"),
343346
)
344347
.arg(
345348
Arg::new(options::HEADER_NUMBERING)
346349
.short('h')
347350
.long(options::HEADER_NUMBERING)
351+
.allow_hyphen_values(true)
348352
.help(translate!("nl-help-header-numbering"))
349353
.value_name("STYLE"),
350354
)
@@ -354,13 +358,14 @@ pub fn uu_app() -> Command {
354358
.long(options::LINE_INCREMENT)
355359
.help(translate!("nl-help-line-increment"))
356360
.value_name("NUMBER")
357-
.allow_negative_numbers(true)
361+
.allow_hyphen_values(true)
358362
.value_parser(clap::value_parser!(i64)),
359363
)
360364
.arg(
361365
Arg::new(options::JOIN_BLANK_LINES)
362366
.short('l')
363367
.long(options::JOIN_BLANK_LINES)
368+
.allow_hyphen_values(true)
364369
.help(translate!("nl-help-join-blank-lines"))
365370
.value_name("NUMBER")
366371
.value_parser(parse_join_blank_lines),
@@ -369,6 +374,7 @@ pub fn uu_app() -> Command {
369374
Arg::new(options::NUMBER_FORMAT)
370375
.short('n')
371376
.long(options::NUMBER_FORMAT)
377+
.allow_hyphen_values(true)
372378
.help(translate!("nl-help-number-format"))
373379
.value_name("FORMAT")
374380
.value_parser(["ln", "rn", "rz"]),
@@ -384,6 +390,7 @@ pub fn uu_app() -> Command {
384390
Arg::new(options::NUMBER_SEPARATOR)
385391
.short('s')
386392
.long(options::NUMBER_SEPARATOR)
393+
.allow_hyphen_values(true)
387394
.help(translate!("nl-help-number-separator"))
388395
.value_parser(clap::value_parser!(OsString))
389396
.value_name("STRING"),
@@ -394,13 +401,14 @@ pub fn uu_app() -> Command {
394401
.long(options::STARTING_LINE_NUMBER)
395402
.help(translate!("nl-help-starting-line-number"))
396403
.value_name("NUMBER")
397-
.allow_negative_numbers(true)
404+
.allow_hyphen_values(true)
398405
.value_parser(clap::value_parser!(i64)),
399406
)
400407
.arg(
401408
Arg::new(options::NUMBER_WIDTH)
402409
.short('w')
403410
.long(options::NUMBER_WIDTH)
411+
.allow_hyphen_values(true)
404412
.help(translate!("nl-help-number-width"))
405413
.value_name("NUMBER")
406414
.value_parser(clap::value_parser!(u64).range(1..=(i32::MAX as u64))),

‎tests/by-util/test_nl.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,3 +1143,36 @@ fn test_negative_line_increment_as_a_separate_argument() {
11431143
.stdout_is(" 1\ta\n 0\tb\n -1\tc\n");
11441144
}
11451145
}
1146+
1147+
#[rstest]
1148+
#[case::separator(&["-s", "-z"], "a\n", " 1-za\n")]
1149+
#[case::separator_double_dash(&["-s", "--"], "a\n", " 1--a\n")]
1150+
#[case::long_separator(&["--number-separator", "-x"], "a\n", " 1-xa\n")]
1151+
#[case::footer_delimiter(&["-d", "-z"], "a\n-z\nb\n", " 1\ta\n\n b\n")]
1152+
#[case::body_delimiter(&["-d", "--"], "a\n----\nb\n", " 1\ta\n\n 1\tb\n")]
1153+
fn test_hyphen_leading_string_value(
1154+
#[case] args: &[&str],
1155+
#[case] input: &str,
1156+
#[case] expected: &str,
1157+
) {
1158+
new_ucmd!()
1159+
.args(args)
1160+
.pipe_in(input)
1161+
.succeeds()
1162+
.stdout_is(expected);
1163+
}
1164+
1165+
#[rstest]
1166+
#[case::width(&["-w", "-2"])]
1167+
#[case::join_blank_lines(&["-l", "-1"])]
1168+
#[case::body_numbering(&["-b", "-x"])]
1169+
#[case::header_numbering(&["-h", "-x"])]
1170+
#[case::footer_numbering(&["-f", "-x"])]
1171+
#[case::number_format(&["-n", "-x"])]
1172+
fn test_hyphen_leading_value_is_checked_as_a_value(#[case] args: &[&str]) {
1173+
new_ucmd!()
1174+
.args(args)
1175+
.fails_with_code(1)
1176+
.stderr_does_not_contain("unexpected argument")
1177+
.stderr_contains(format!("'{}'", args[1]));
1178+
}

0 commit comments

Comments
 (0)