From 52a792d46f2ab409bf0030cca7a61201c4d3f0ba Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Sun, 19 Oct 2025 19:23:57 +0100 Subject: [PATCH] fix: allow hyphen values to `-r`/`--line-range` via #2944 --- CHANGELOG.md | 2 ++ doc/long-help.txt | 1 + src/bin/bat/clap_app.rs | 2 ++ tests/integration_tests.rs | 13 ++++++++++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index febd31ac..5f388972 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ## Bugfixes +- Fix negative values of N not being parsed in line ranges without `=` flag value separator, see #3442 (@lmmx) + ## Other ## Syntaxes diff --git a/doc/long-help.txt b/doc/long-help.txt index c625ffa3..8cb60812 100644 --- a/doc/long-help.txt +++ b/doc/long-help.txt @@ -194,6 +194,7 @@ Options: '--line-range :40' prints lines 1 to 40 '--line-range 40:' prints lines 40 to the end of the file '--line-range 40' only prints line 40 + '--line-range -10:' prints the last 10 lines '--line-range 30:+10' prints lines 30 to 40 '--line-range 35::5' prints lines 30 to 40 (line 35 with 5 lines of context) '--line-range 30:40:2' prints lines 28 to 42 (range 30-40 with 2 lines of context) diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index 3820c284..8c9f7c1f 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -519,6 +519,7 @@ pub fn build_app(interactive_output: bool) -> Command { .short('r') .action(ArgAction::Append) .value_name("N:M") + .allow_hyphen_values(true) .help("Only print the lines from N to M.") .long_help( "Only print the specified range of lines for each file. \ @@ -527,6 +528,7 @@ pub fn build_app(interactive_output: bool) -> Command { '--line-range :40' prints lines 1 to 40\n \ '--line-range 40:' prints lines 40 to the end of the file\n \ '--line-range 40' only prints line 40\n \ + '--line-range -10:' prints the last 10 lines\n \ '--line-range 30:+10' prints lines 30 to 40\n \ '--line-range 35::5' prints lines 30 to 40 (line 35 with 5 lines of context)\n \ '--line-range 30:40:2' prints lines 28 to 42 (range 30-40 with 2 lines of context)", diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 40f0ed3a..20aaad94 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -207,7 +207,7 @@ fn line_range_from_back_last_two() { } #[test] -fn line_range_from_back_last_two_single_line() { +fn line_range_from_back_last_two_single_line_eq_sep() { bat() .arg("single-line.txt") .arg("--line-range=-2:") @@ -216,6 +216,17 @@ fn line_range_from_back_last_two_single_line() { .stdout("Single Line"); } +#[test] +fn line_range_from_back_last_two_single_line_no_sep() { + bat() + .arg("single-line.txt") + .arg("--line-range") + .arg("-2:") + .assert() + .success() + .stdout("Single Line"); +} + #[test] fn line_range_first_two() { bat()