2020-12-27 19:47:30 +00:00
|
|
|
use super::*;
|
|
|
|
|
2020-12-27 22:50:20 +00:00
|
|
|
fn build_app() -> App<'static> {
|
|
|
|
build_app_with_name("myapp")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_app_with_name(s: &'static str) -> App<'static> {
|
|
|
|
App::new(s)
|
2021-10-08 17:05:40 +00:00
|
|
|
.version("3.0")
|
2021-10-12 18:59:56 +00:00
|
|
|
.setting(AppSettings::PropagateVersion)
|
2020-12-27 22:50:20 +00:00
|
|
|
.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"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:47:30 +00:00
|
|
|
#[test]
|
|
|
|
fn elvish() {
|
|
|
|
let mut app = build_app();
|
2021-08-18 18:25:43 +00:00
|
|
|
common(Elvish, &mut app, "my_app", ELVISH);
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ELVISH: &str = r#"
|
2021-04-08 11:40:17 +00:00
|
|
|
use builtin;
|
2021-04-08 11:21:06 +00:00
|
|
|
use str;
|
2021-04-08 11:40:17 +00:00
|
|
|
|
2021-09-24 12:22:17 +00:00
|
|
|
set edit:completion:arg-completer[my_app] = [@words]{
|
2020-12-27 19:47:30 +00:00
|
|
|
fn spaces [n]{
|
2021-04-08 11:40:17 +00:00
|
|
|
builtin:repeat $n ' ' | str:join ''
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
fn cand [text desc]{
|
2021-09-24 12:22:17 +00:00
|
|
|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
var command = 'my_app'
|
2021-04-08 11:40:17 +00:00
|
|
|
for word $words[1..-1] {
|
2021-04-08 11:21:06 +00:00
|
|
|
if (str:has-prefix $word '-') {
|
2020-12-27 19:47:30 +00:00
|
|
|
break
|
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
set command = $command';'$word
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
var completions = [
|
2020-12-27 19:47:30 +00:00
|
|
|
&'my_app'= {
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2020-12-27 19:47:30 +00:00
|
|
|
cand test 'tests things'
|
2021-07-30 03:23:25 +00:00
|
|
|
cand help 'Print this message or the help of the given subcommand(s)'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
&'my_app;test'= {
|
|
|
|
cand --case 'the case to test'
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
&'my_app;help'= {
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
$completions[$command]
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn elvish_with_special_commands() {
|
|
|
|
let mut app = build_app_special_commands();
|
2021-08-18 18:25:43 +00:00
|
|
|
common(Elvish, &mut app, "my_app", ELVISH_SPECIAL_CMDS);
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 22:50:20 +00:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:47:30 +00:00
|
|
|
static ELVISH_SPECIAL_CMDS: &str = r#"
|
2021-04-08 11:40:17 +00:00
|
|
|
use builtin;
|
2021-04-08 11:21:06 +00:00
|
|
|
use str;
|
2021-04-08 11:40:17 +00:00
|
|
|
|
2021-09-24 12:22:17 +00:00
|
|
|
set edit:completion:arg-completer[my_app] = [@words]{
|
2020-12-27 19:47:30 +00:00
|
|
|
fn spaces [n]{
|
2021-04-08 11:40:17 +00:00
|
|
|
builtin:repeat $n ' ' | str:join ''
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
fn cand [text desc]{
|
2021-09-24 12:22:17 +00:00
|
|
|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
var command = 'my_app'
|
2021-04-08 11:40:17 +00:00
|
|
|
for word $words[1..-1] {
|
2021-04-08 11:21:06 +00:00
|
|
|
if (str:has-prefix $word '-') {
|
2020-12-27 19:47:30 +00:00
|
|
|
break
|
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
set command = $command';'$word
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
var completions = [
|
2020-12-27 19:47:30 +00:00
|
|
|
&'my_app'= {
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2020-12-27 19:47:30 +00:00
|
|
|
cand test 'tests things'
|
|
|
|
cand some_cmd 'tests other things'
|
|
|
|
cand some-cmd-with-hypens 'some-cmd-with-hypens'
|
2021-07-30 03:23:25 +00:00
|
|
|
cand help 'Print this message or the help of the given subcommand(s)'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
&'my_app;test'= {
|
|
|
|
cand --case 'the case to test'
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
&'my_app;some_cmd'= {
|
|
|
|
cand --config 'the other case to test'
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
&'my_app;some-cmd-with-hypens'= {
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
&'my_app;help'= {
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
2020-12-27 19:47:30 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
$completions[$command]
|
|
|
|
}
|
|
|
|
"#;
|
2021-02-15 08:35:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn elvish_with_aliases() {
|
|
|
|
let mut app = build_app_with_aliases();
|
2021-08-18 18:25:43 +00:00
|
|
|
common(Elvish, &mut app, "cmd", ELVISH_ALIASES);
|
2021-02-15 08:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_app_with_aliases() -> App<'static> {
|
|
|
|
App::new("cmd")
|
2021-10-08 17:05:40 +00:00
|
|
|
.version("3.0")
|
2021-02-15 08:35:25 +00:00
|
|
|
.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#"
|
2021-04-08 11:40:17 +00:00
|
|
|
use builtin;
|
2021-04-08 11:21:06 +00:00
|
|
|
use str;
|
2021-04-08 11:40:17 +00:00
|
|
|
|
2021-09-24 12:22:17 +00:00
|
|
|
set edit:completion:arg-completer[cmd] = [@words]{
|
2021-02-15 08:35:25 +00:00
|
|
|
fn spaces [n]{
|
2021-04-08 11:40:17 +00:00
|
|
|
builtin:repeat $n ' ' | str:join ''
|
2021-02-15 08:35:25 +00:00
|
|
|
}
|
|
|
|
fn cand [text desc]{
|
2021-09-24 12:22:17 +00:00
|
|
|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
|
2021-02-15 08:35:25 +00:00
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
var command = 'cmd'
|
2021-04-08 11:40:17 +00:00
|
|
|
for word $words[1..-1] {
|
2021-04-08 11:21:06 +00:00
|
|
|
if (str:has-prefix $word '-') {
|
2021-02-15 08:35:25 +00:00
|
|
|
break
|
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
set command = $command';'$word
|
2021-02-15 08:35:25 +00:00
|
|
|
}
|
2021-09-24 12:22:17 +00:00
|
|
|
var completions = [
|
2021-02-15 08:35:25 +00:00
|
|
|
&'cmd'= {
|
|
|
|
cand -o 'cmd option'
|
|
|
|
cand -O 'cmd option'
|
|
|
|
cand --option 'cmd option'
|
|
|
|
cand --opt 'cmd option'
|
2021-07-30 03:23:25 +00:00
|
|
|
cand -h 'Print help information'
|
|
|
|
cand --help 'Print help information'
|
|
|
|
cand -V 'Print version information'
|
|
|
|
cand --version 'Print version information'
|
2021-02-15 08:35:25 +00:00
|
|
|
cand -f 'cmd flag'
|
|
|
|
cand -F 'cmd flag'
|
|
|
|
cand --flag 'cmd flag'
|
|
|
|
cand --flg 'cmd flag'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
$completions[$command]
|
|
|
|
}
|
|
|
|
"#;
|