Skip to content

Commit a2db784

Browse files
committed
fix(nl): allow repeated flags to match GNU nl behavior (fixes #9132)
Enable args_override_self(true) to allow repeated flags and remove unnecessary ArgAction::Append calls. Helper.rs doesn't need changes as get_one() works correctly with args_override_self. Add comprehensive regression tests with proper flag explanations: - t=nonempty (number only nonempty lines) - rn=right aligned - etc.
1 parent 8f7afa7 commit a2db784

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/uu/nl/src/nl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ pub fn uu_app() -> Command {
245245
.after_help(translate!("nl-after-help"))
246246
.infer_long_args(true)
247247
.disable_help_flag(true)
248+
.args_override_self(true)
248249
.arg(
249250
Arg::new(options::HELP)
250251
.long(options::HELP)

tests/by-util/test_nl.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,100 @@ fn test_file_with_non_utf8_content() {
800800
String::from_utf8_lossy(invalid_utf8)
801801
));
802802
}
803+
804+
// Regression tests for issue #9132: repeated flags should use last value
805+
#[test]
806+
fn test_repeated_body_numbering_flag() {
807+
// -ba -bt should use -bt (t=nonempty)
808+
new_ucmd!()
809+
.args(&["-ba", "-bt"])
810+
.pipe_in("a\n\nb\n\nc")
811+
.succeeds()
812+
.stdout_is(" 1\ta\n \n 2\tb\n \n 3\tc\n");
813+
}
814+
815+
#[test]
816+
fn test_repeated_header_numbering_flag() {
817+
// -ha -ht should use -ht (t=nonempty in header)
818+
new_ucmd!()
819+
.args(&["-ha", "-ht"])
820+
.pipe_in("\\:\\:\\:\na\nb\nc")
821+
.succeeds();
822+
}
823+
824+
#[test]
825+
fn test_repeated_footer_numbering_flag() {
826+
// -fa -ft should use -ft (t=nonempty in footer)
827+
new_ucmd!()
828+
.args(&["-fa", "-ft"])
829+
.pipe_in("\\:\na\nb\nc")
830+
.succeeds();
831+
}
832+
833+
#[test]
834+
fn test_repeated_number_format_flag() {
835+
// -n ln -n rn should use -n rn (rn=right aligned)
836+
new_ucmd!()
837+
.args(&["-n", "ln", "-n", "rn"])
838+
.pipe_in("a\nb\nc")
839+
.succeeds()
840+
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
841+
}
842+
843+
#[test]
844+
fn test_repeated_number_separator_flag() {
845+
// -s ':' -s '|' should use -s '|'
846+
new_ucmd!()
847+
.args(&["-s", ":", "-s", "|"])
848+
.pipe_in("a\nb\nc")
849+
.succeeds()
850+
.stdout_is(" 1|a\n 2|b\n 3|c\n");
851+
}
852+
853+
#[test]
854+
fn test_repeated_number_width_flag() {
855+
// -w 3 -w 8 should use -w 8
856+
new_ucmd!()
857+
.args(&["-w", "3", "-w", "8"])
858+
.pipe_in("a\nb\nc")
859+
.succeeds()
860+
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
861+
}
862+
863+
#[test]
864+
fn test_repeated_line_increment_flag() {
865+
// -i 1 -i 5 should use -i 5
866+
new_ucmd!()
867+
.args(&["-i", "1", "-i", "5"])
868+
.pipe_in("a\nb\nc")
869+
.succeeds()
870+
.stdout_is(" 1\ta\n 6\tb\n 11\tc\n");
871+
}
872+
873+
#[test]
874+
fn test_repeated_starting_line_number_flag() {
875+
// -v 1 -v 10 should use -v 10
876+
new_ucmd!()
877+
.args(&["-v", "1", "-v", "10"])
878+
.pipe_in("a\nb\nc")
879+
.succeeds()
880+
.stdout_is(" 10\ta\n 11\tb\n 12\tc\n");
881+
}
882+
883+
#[test]
884+
fn test_repeated_join_blank_lines_flag() {
885+
// -l 1 -l 2 should use -l 2
886+
new_ucmd!()
887+
.args(&["-l", "1", "-l", "2"])
888+
.pipe_in("a\n\n\nb")
889+
.succeeds();
890+
}
891+
892+
#[test]
893+
fn test_repeated_section_delimiter_flag() {
894+
// -d ':' -d '|' should use -d '|'
895+
new_ucmd!()
896+
.args(&["-d", ":", "-d", "|"])
897+
.pipe_in("a\nb\nc")
898+
.succeeds();
899+
}

0 commit comments

Comments
 (0)