Skip to content

Commit f70ebe8

Browse files
committed
fix!: Require explicit help/version disabling
Before we introduced actions, it required specific setups to engage with claps version and help printing. With actions making that more explicit, we don't get as much benefit from our multiple, obscure, ways of users customizing help Before - Modify existing help or version with `mut_arg` which would automatically be pushed down the command tree like `global(true)` - Create an new help or version and have it treated as if it was the built-in on (I think) - Use the same flags as built-in and have the built-in flags automatically disabled - Users could explicitly disable the built-in functionality and do what they want Now - `mut_arg` no longer works as we define help and version flags at the end - If someone defines a flag that overlaps with the built-ins by id, long, or short, a debug assert will tell them to explicitly disable the built-in - Any customization has to be done by a user providing their own. To propagate through the command tree, they need to set `global(true)`. Benefits - Hopefully, this makes it less confusing on how to override help behavior. Someone creates an arg and we then tell them how to disable the built-in - This greatly simplifies the arg handling by pushing more responsibility onto the developer in what are hopefully just corner cases - This removes about 1Kb from .text Fixes #3405 Fixes #4033
1 parent 0129d99 commit f70ebe8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+348
-733
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3535
- Remove `Arg::use_value_delimiter` in favor of `Arg::value_delimiter`
3636
- Remove `Arg::require_value_delimiter`, either users could use `Arg::value_delimiter` or implement a custom parser with `TypedValueParser`
3737
- `ArgAction::SetTrue` and `ArgAction::SetFalse` now prioritize `Arg::default_missing_value` over their standard behavior
38+
- `mut_arg` can no longer be used to customize help and version arguments, instead disable them (`Command::disable_help_flag`, `Command::disable_version_flag`) and provide your own
39+
- `Arg::new("help")` and `Arg::new("version")` no longer implicitly disable the
40+
built-in flags and be copied to all subcommands, instead disable
41+
the built-in flags (`Command::disable_help_flag`,
42+
`Command::disable_version_flag`) and mark the custom flags as `global(true)`.
3843
- *(help)* Make `DeriveDisplayOrder` the default and removed the setting. To sort help, set `next_display_order(None)` (#2808)
3944
- *(help)* Subcommand display order respects `Command::next_display_order` instead of `DeriveDisplayOrder` and using its own initial display order value (#2808)
4045
- *(env)* Parse `--help` and `--version` like any `ArgAction::SetTrue` flag (#3776)

clap_bench/benches/05_ripgrep.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ where
310310
.help_template(TEMPLATE)
311311
// Handle help/version manually to make their output formatting
312312
// consistent with short/long views.
313+
.disable_help_flag(true)
314+
.disable_version_flag(true)
313315
.arg(arg("help-short").short('h'))
314316
.arg(flag("help"))
315317
.arg(flag("version").short('V'))

clap_complete/tests/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub fn value_hint_command(name: &'static str) -> clap::Command<'static> {
231231
)
232232
.arg(
233233
clap::Arg::new("host")
234-
.short('h')
234+
.short('H')
235235
.long("host")
236236
.value_hint(clap::ValueHint::Hostname),
237237
)

clap_complete/tests/snapshots/aliases.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ _my-app() {
1919

2020
case "${cmd}" in
2121
my__app)
22-
opts="-h -V -F -f -O -o --help --version --flg --flag --opt --option <positional>"
22+
opts="-F -f -O -o -h -V --flg --flag --opt --option --help --version <positional>"
2323
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
2424
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
2525
return 0

clap_complete/tests/snapshots/aliases.elvish

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ set edit:completion:arg-completer[my-app] = {|@words|
2222
cand -O 'cmd option'
2323
cand --option 'cmd option'
2424
cand --opt 'cmd option'
25-
cand -h 'Print help information'
26-
cand --help 'Print help information'
27-
cand -V 'Print version information'
28-
cand --version 'Print version information'
2925
cand -f 'cmd flag'
3026
cand -F 'cmd flag'
3127
cand --flag 'cmd flag'
3228
cand --flg 'cmd flag'
29+
cand -h 'Print help information'
30+
cand --help 'Print help information'
31+
cand -V 'Print version information'
32+
cand --version 'Print version information'
3333
}
3434
]
3535
$completions[$command]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
complete -c my-app -s o -s O -l option -l opt -d 'cmd option' -r
2+
complete -c my-app -s f -s F -l flag -l flg -d 'cmd flag'
23
complete -c my-app -s h -l help -d 'Print help information'
34
complete -c my-app -s V -l version -d 'Print version information'
4-
complete -c my-app -s f -s F -l flag -l flg -d 'cmd flag'

clap_complete/tests/snapshots/aliases.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
2525
[CompletionResult]::new('-O', 'O', [CompletionResultType]::ParameterName, 'cmd option')
2626
[CompletionResult]::new('--option', 'option', [CompletionResultType]::ParameterName, 'cmd option')
2727
[CompletionResult]::new('--opt', 'opt', [CompletionResultType]::ParameterName, 'cmd option')
28-
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
29-
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
30-
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information')
31-
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information')
3228
[CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'cmd flag')
3329
[CompletionResult]::new('-F', 'F', [CompletionResultType]::ParameterName, 'cmd flag')
3430
[CompletionResult]::new('--flag', 'flag', [CompletionResultType]::ParameterName, 'cmd flag')
3531
[CompletionResult]::new('--flg', 'flg', [CompletionResultType]::ParameterName, 'cmd flag')
32+
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
33+
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
34+
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information')
35+
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information')
3636
break
3737
}
3838
})

clap_complete/tests/snapshots/aliases.zsh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ _my-app() {
1919
'*-O+[cmd option]: : ' /
2020
'*--option=[cmd option]: : ' /
2121
'*--opt=[cmd option]: : ' /
22-
'*-h[Print help information]' /
23-
'*--help[Print help information]' /
24-
'*-V[Print version information]' /
25-
'*--version[Print version information]' /
2622
'*-f[cmd flag]' /
2723
'*-F[cmd flag]' /
2824
'*--flag[cmd flag]' /
2925
'*--flg[cmd flag]' /
26+
'*-h[Print help information]' /
27+
'*--help[Print help information]' /
28+
'*-V[Print version information]' /
29+
'*--version[Print version information]' /
3030
'::positional:' /
3131
&& ret=0
3232
}

clap_complete/tests/snapshots/basic.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ _my-app() {
2525

2626
case "${cmd}" in
2727
my__app)
28-
opts="-h -c -v --help test help"
28+
opts="-c -v -h --help test help"
2929
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
3030
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
3131
return 0
@@ -53,7 +53,7 @@ _my-app() {
5353
return 0
5454
;;
5555
my__app__test)
56-
opts="-d -h -c --help"
56+
opts="-d -c -h --help"
5757
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
5858
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
5959
return 0

clap_complete/tests/snapshots/basic.elvish

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ set edit:completion:arg-completer[my-app] = {|@words|
1818
}
1919
var completions = [
2020
&'my-app'= {
21-
cand -h 'Print help information'
22-
cand --help 'Print help information'
2321
cand -c 'c'
2422
cand -v 'v'
23+
cand -h 'Print help information'
24+
cand --help 'Print help information'
2525
cand test 'Subcommand'
2626
cand help 'Print this message or the help of the given subcommand(s)'
2727
}
2828
&'my-app;test'= {
2929
cand -d 'd'
30+
cand -c 'c'
3031
cand -h 'Print help information'
3132
cand --help 'Print help information'
32-
cand -c 'c'
3333
}
3434
&'my-app;help'= {
3535
cand -c 'c'

0 commit comments

Comments
 (0)