Skip to content

Allow value-taking options to be repeated, like boolean flags - #4102

Merged
keith-hall merged 1 commit into
sharkdp:masterfrom
Huang-404-Q:fix/repeatable-valued-options
Nov 2, 2026
Merged

keith-hall merged 1 commit into
sharkdp:masterfrom
Huang-404-Q:fix/repeatable-valued-options

Conversation

@Huang-404-Q

Copy link
Copy Markdown
Contributor

Problem

A value-taking option given in both the config file and on the command line is rejected, so the config value cannot be overridden:

$ cat ~/.config/bat/config
--binary=as-text

$ bat --binary=no-printing style.css
error: the argument '--binary <behavior>' cannot be used multiple times

Usage: bat [OPTIONS] [FILE]...

For more information, try '--help'.

Exit code 2 and no output at all. The config file is prepended to the command line, so any option that appears in both places hits this.

Boolean flags were fixed the same way in #3912/#3921; the valued options were left out. Affected: --binary, --italic-text, --nonprintable-notation, --fallback-syntax, --squeeze-limit.

Fix

Give each of them a self-override, following the "last one wins" behaviour that --color, --theme and the boolean flags already have:

Arg::new("binary")
    .long("binary")
    .overrides_with("binary")
    ...

Five one-line additions in src/bin/bat/clap_app.rs.

Tests

Two integration tests in tests/integration_tests.rs: one repeats each of the five options on the command line, the other puts --binary=as-text in a config file and overrides it with --binary=no-printing on the command line.

Without the fix:

test repeated_valued_option_is_accepted ... FAILED
test valued_option_in_config_can_be_overridden_on_command_line ... FAILED
error: the argument '--binary <behavior>' cannot be used multiple times
test result: FAILED. 0 passed; 2 failed

With it:

test result: ok. 2 passed; 0 failed

Full suite: cargo test --test integration_tests → 251 passed, 2 ignored, 0 failed; cargo test --lib → 144 passed.

@Huang-404-Q
Huang-404-Q force-pushed the fix/repeatable-valued-options branch from e832851 to e35c032 Compare October 29, 2026 08:12

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified the scope of this fix by enumerating every Arg::new in clap_app.rs on main: the five options changed here (--binary, --italic-text, --nonprintable-notation, --fallback-syntax, --squeeze-limit) are exactly the top-level value-taking arguments that lacked overrides_with, while the other valued options (--theme, --language, --paging, --tabs, --wrap, --terminal-width, --color, --decorations, --pager, --strip-ansi, --sanitize, --diff-context, …) already have it. So this closes the remaining gap rather than picking an arbitrary subset — and the --fallback-language visible alias maps to the same arg id, so the override covers it too.

For anyone wondering why overrides_with(self) is the right idiom here: with ArgAction::Set, clap errors on repeat occurrences, and self-override is clap's documented way to allow them with last-value-wins — consistent with how #3912/#3921 handled the boolean flags (I confirmed those SetTrue args now carry overrides_with on main as well). The config-file angle makes this user-visible: get_args_from_config_file prepends config entries to the CLI argv, so any option in both places hit the "cannot be used multiple times" error, which matches your repro.

Two test-design notes I appreciated: the second test reproduces the actual reported scenario (config value overridden by CLI value) and asserts the behavioral consequence — Binary content from with no NUL bytes in stdout — rather than only exit status. tests/examples/test.binary exists as the fixture, so that's solid.

One small observation: bat cache's --source/--target/--completion also lack overrides_with, but they're subcommand-scoped so they can't collide with config-file entries on the top-level invocation — leaving them out is correct.

@Huang-404-Q
Huang-404-Q force-pushed the fix/repeatable-valued-options branch 2 times, most recently from 6edb1ff to ff7baaa Compare November 2, 2026 05:50
The config file is prepended to the command line, so an option set in
both places is passed twice. Boolean flags were made repeatable in
sharkdp#3912, but options that take a value were missed: --binary,
--italic-text, --nonprintable-notation, --fallback-syntax and
--squeeze-limit were rejected with 'cannot be used multiple times',
which aborts bat with exit code 2 before it reads any input.

Add self-overrides so that the last occurrence wins, matching the
behavior of --color, --theme, --wrap, --tabs and friends.
@Huang-404-Q
Huang-404-Q force-pushed the fix/repeatable-valued-options branch from ff7baaa to b5281b5 Compare November 2, 2026 05:50

@keith-hall keith-hall left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@keith-hall
keith-hall merged commit 1a21897 into sharkdp:master Nov 2, 2026
24 checks passed
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Nov 2, 2026
Resolve the CHANGELOG conflict with sharkdp#4102 and sharkdp#4103: keep their new
entries, keep this PR's sharkdp#4084 entry, and point the UTF-16BE entry back
at sharkdp#4076 (the merge of sharkdp#4102 had filled its placeholder with sharkdp#4102).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants