mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 15:22:30 +00:00
a61b60816c
`App::get_matches` lazily post-processes `App`s and `Arg`s so we don't do it to subcommands that are never run (downside being people have to exercise their full app to get debug_asserts). `clap_generate` was only post-processing the top-level `App` and `Arg`s, ignoring the sub-commands. In #2858, we noticed that `--version` was being left in the completions instead of being removed during the `_build` step. We would also have an incorrect `num_vals` and a host of other problems. This change adds a `App::_build_all` function for `clap_generate` to use to eagerly build everything. By having it there, we make sure everywhere that needs eager building, gets it (like some tests). In `clap_generate::utils`, we add a unit test to ensure the subcommand's `--version` was removed. For some other tests specifying `.version()`, I added `AppSettings::PropagateVersion` to make it behave more consistently. The places I didn't were generally where the version was conditionally set. For `clap_generate/tests/generate_completions.rs`, I had to adjust the `conflicts_with` because the subcommand was inheriting the argument with it defined *but* the subcommand did not have the argument, tripping up a debug assert. Fixes #2860
219 lines
6.3 KiB
Rust
219 lines
6.3 KiB
Rust
use super::*;
|
|
|
|
fn build_app() -> App<'static> {
|
|
build_app_with_name("myapp")
|
|
}
|
|
|
|
fn build_app_with_name(s: &'static str) -> App<'static> {
|
|
App::new(s)
|
|
.version("3.0")
|
|
.setting(AppSettings::PropagateVersion)
|
|
.about("Tests completions")
|
|
.arg(
|
|
Arg::new("file")
|
|
.value_hint(ValueHint::FilePath)
|
|
.about("some input file"),
|
|
)
|
|
.subcommand(
|
|
App::new("test").about("tests things").arg(
|
|
Arg::new("case")
|
|
.long("case")
|
|
.takes_value(true)
|
|
.about("the case to test"),
|
|
),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn elvish() {
|
|
let mut app = build_app();
|
|
common(Elvish, &mut app, "my_app", ELVISH);
|
|
}
|
|
|
|
static ELVISH: &str = r#"
|
|
use builtin;
|
|
use str;
|
|
|
|
set edit:completion:arg-completer[my_app] = [@words]{
|
|
fn spaces [n]{
|
|
builtin:repeat $n ' ' | str:join ''
|
|
}
|
|
fn cand [text desc]{
|
|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
|
|
}
|
|
var command = 'my_app'
|
|
for word $words[1..-1] {
|
|
if (str:has-prefix $word '-') {
|
|
break
|
|
}
|
|
set command = $command';'$word
|
|
}
|
|
var completions = [
|
|
&'my_app'= {
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
cand test 'tests things'
|
|
cand help 'Print this message or the help of the given subcommand(s)'
|
|
}
|
|
&'my_app;test'= {
|
|
cand --case 'the case to test'
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
}
|
|
&'my_app;help'= {
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
}
|
|
]
|
|
$completions[$command]
|
|
}
|
|
"#;
|
|
|
|
#[test]
|
|
fn elvish_with_special_commands() {
|
|
let mut app = build_app_special_commands();
|
|
common(Elvish, &mut app, "my_app", ELVISH_SPECIAL_CMDS);
|
|
}
|
|
|
|
fn build_app_special_commands() -> App<'static> {
|
|
build_app_with_name("my_app")
|
|
.subcommand(
|
|
App::new("some_cmd").about("tests other things").arg(
|
|
Arg::new("config")
|
|
.long("--config")
|
|
.takes_value(true)
|
|
.about("the other case to test"),
|
|
),
|
|
)
|
|
.subcommand(App::new("some-cmd-with-hypens").alias("hyphen"))
|
|
}
|
|
|
|
static ELVISH_SPECIAL_CMDS: &str = r#"
|
|
use builtin;
|
|
use str;
|
|
|
|
set edit:completion:arg-completer[my_app] = [@words]{
|
|
fn spaces [n]{
|
|
builtin:repeat $n ' ' | str:join ''
|
|
}
|
|
fn cand [text desc]{
|
|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
|
|
}
|
|
var command = 'my_app'
|
|
for word $words[1..-1] {
|
|
if (str:has-prefix $word '-') {
|
|
break
|
|
}
|
|
set command = $command';'$word
|
|
}
|
|
var completions = [
|
|
&'my_app'= {
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
cand test 'tests things'
|
|
cand some_cmd 'tests other things'
|
|
cand some-cmd-with-hypens 'some-cmd-with-hypens'
|
|
cand help 'Print this message or the help of the given subcommand(s)'
|
|
}
|
|
&'my_app;test'= {
|
|
cand --case 'the case to test'
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
}
|
|
&'my_app;some_cmd'= {
|
|
cand --config 'the other case to test'
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
}
|
|
&'my_app;some-cmd-with-hypens'= {
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
}
|
|
&'my_app;help'= {
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
}
|
|
]
|
|
$completions[$command]
|
|
}
|
|
"#;
|
|
|
|
#[test]
|
|
fn elvish_with_aliases() {
|
|
let mut app = build_app_with_aliases();
|
|
common(Elvish, &mut app, "cmd", ELVISH_ALIASES);
|
|
}
|
|
|
|
fn build_app_with_aliases() -> App<'static> {
|
|
App::new("cmd")
|
|
.version("3.0")
|
|
.about("testing bash completions")
|
|
.arg(
|
|
Arg::new("flag")
|
|
.short('f')
|
|
.visible_short_alias('F')
|
|
.long("flag")
|
|
.visible_alias("flg")
|
|
.about("cmd flag"),
|
|
)
|
|
.arg(
|
|
Arg::new("option")
|
|
.short('o')
|
|
.visible_short_alias('O')
|
|
.long("option")
|
|
.visible_alias("opt")
|
|
.about("cmd option")
|
|
.takes_value(true),
|
|
)
|
|
.arg(Arg::new("positional"))
|
|
}
|
|
|
|
static ELVISH_ALIASES: &str = r#"
|
|
use builtin;
|
|
use str;
|
|
|
|
set edit:completion:arg-completer[cmd] = [@words]{
|
|
fn spaces [n]{
|
|
builtin:repeat $n ' ' | str:join ''
|
|
}
|
|
fn cand [text desc]{
|
|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
|
|
}
|
|
var command = 'cmd'
|
|
for word $words[1..-1] {
|
|
if (str:has-prefix $word '-') {
|
|
break
|
|
}
|
|
set command = $command';'$word
|
|
}
|
|
var completions = [
|
|
&'cmd'= {
|
|
cand -o 'cmd option'
|
|
cand -O 'cmd option'
|
|
cand --option 'cmd option'
|
|
cand --opt 'cmd option'
|
|
cand -h 'Print help information'
|
|
cand --help 'Print help information'
|
|
cand -V 'Print version information'
|
|
cand --version 'Print version information'
|
|
cand -f 'cmd flag'
|
|
cand -F 'cmd flag'
|
|
cand --flag 'cmd flag'
|
|
cand --flg 'cmd flag'
|
|
}
|
|
]
|
|
$completions[$command]
|
|
}
|
|
"#;
|