Skip to content

Commit 49950ab

Browse files
committed
nl: accept negative -v and -i values given as separate arguments
GNU nl accepts a negative starting line number or increment passed as a separate argument, such as `nl -v -5` or `nl -i -2`. uutils only accepted the attached forms (`-v-5`, `--starting-line-number=-5`); the space- separated form failed with "unexpected argument '-5' found" because clap treated the leading-hyphen token as an unknown flag. Set allow_hyphen_values on the -v and -i arguments so the negative value is consumed as the option value, matching GNU. The value parser was already i64, so negative numbers were always intended. Add tests covering the space-separated form for both options.
1 parent d41c56b commit 49950ab

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/uu/nl/src/nl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ pub fn uu_app() -> Command {
329329
.long(options::LINE_INCREMENT)
330330
.help(translate!("nl-help-line-increment"))
331331
.value_name("NUMBER")
332+
.allow_hyphen_values(true)
332333
.value_parser(clap::value_parser!(i64)),
333334
)
334335
.arg(
@@ -368,6 +369,7 @@ pub fn uu_app() -> Command {
368369
.long(options::STARTING_LINE_NUMBER)
369370
.help(translate!("nl-help-starting-line-number"))
370371
.value_name("NUMBER")
372+
.allow_hyphen_values(true)
371373
.value_parser(clap::value_parser!(i64)),
372374
)
373375
.arg(

tests/by-util/test_nl.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ fn test_negative_starting_line_number() {
254254
}
255255
}
256256

257+
#[test]
258+
fn test_negative_starting_line_number_space_separated() {
259+
// GNU nl accepts a negative value passed as a separate argument, e.g.
260+
// `nl -v -10`. Without `allow_hyphen_values` clap rejected the `-10` token.
261+
new_ucmd!()
262+
.args(&["-v", "-10"])
263+
.pipe_in("test")
264+
.succeeds()
265+
.stdout_is(" -10\ttest\n");
266+
}
267+
257268
#[test]
258269
fn test_invalid_starting_line_number() {
259270
for arg in ["-vinvalid", "--starting-line-number=invalid"] {
@@ -298,6 +309,16 @@ fn test_negative_line_increment() {
298309
}
299310
}
300311

312+
#[test]
313+
fn test_negative_line_increment_space_separated() {
314+
// GNU nl accepts `nl -i -10` with the value as a separate argument.
315+
new_ucmd!()
316+
.args(&["-i", "-10"])
317+
.pipe_in("a\nb\nc")
318+
.succeeds()
319+
.stdout_is(" 1\ta\n -9\tb\n -19\tc\n");
320+
}
321+
301322
#[test]
302323
fn test_invalid_line_increment() {
303324
for arg in ["-iinvalid", "--line-increment=invalid"] {

0 commit comments

Comments
 (0)