Skip to content

Commit a5fa914

Browse files
pr: make -n/--number-lines argument optional to match GNU
GNU pr treats the -n/--number-lines argument as optional: a bare -n numbers the lines with a 5-wide, tab-separated line number. uutils required a value, so `pr -t -n` and `pr -t --number-lines` failed with "a value is required". Set num_args(0..=1) so the value is optional; the existing fallback already supplies the default numbering mode.
1 parent 822aa83 commit a5fa914

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/uu/pr/src/pr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ pub fn uu_app() -> Command {
238238
.long(options::NUMBER_LINES)
239239
.help(translate!("pr-help-number-lines"))
240240
.allow_hyphen_values(true)
241+
// GNU pr treats the -n/--number-lines argument as optional and
242+
// defaults to a 5-wide, tab-separated line number; without this a
243+
// bare -n or --number-lines fails asking for a value.
244+
.num_args(0..=1)
241245
.value_name("[char][width]"),
242246
)
243247
.arg(

tests/by-util/test_pr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ fn test_number_lines_empty_value_is_rejected() {
8181
.stderr_contains("pr: '-n' extra characters or invalid number in the argument");
8282
}
8383

84+
#[test]
85+
fn test_number_lines_without_value_numbers_lines() {
86+
// GNU pr treats -n/--number-lines as optional, defaulting to a 5-wide,
87+
// tab-separated line number, instead of requiring an explicit value.
88+
for arg in ["-n", "--number-lines"] {
89+
new_ucmd!()
90+
.args(&["-t", arg])
91+
.pipe_in("a\nb\n")
92+
.succeeds()
93+
.stdout_is(" 1\ta\n 2\tb\n");
94+
}
95+
}
96+
8497
#[test]
8598
fn test_without_any_options() {
8699
let test_file_path = "test_one_page.log";

0 commit comments

Comments
 (0)