clap/clap_complete/tests/completions/elvish.rs
Ed Page 88a335ff97 fix(complete): Give crates more specific names
`clap_generate` originally intended to be "generate anything".  With
`fig`, we already broke one part out.  With #3174's man support, we are
also looking at keeping it separate:
- More freedom to iterate on the API
- Uniqueness (and potential weight) of its dependencies
- man generation is normally more for distribution while completions are
  a mix of being distributed with the app or the app generating the
  completions (which will be exacerbated if we move most completion
  parsing logic to be in Rust)

So `clap_generate` is having a lot more limited of a role than the
original name conveys.   I worry the generic name will be a hindrance to
people discovering and using it (yes, documentation can help but there
are limits).

I hesitated because we are on the verge of releasing 3.0. However, doing
it even later will be even more disruptive because more people will be
using it (crates.io lists ~70 people using `clap_generate`).

To ease things, we are still releasing `clap_generate` as a wrapper
around `clap_complete`.
2021-12-31 12:03:29 -06:00

215 lines
6.1 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)
.help("some input file"),
)
.subcommand(
App::new("test").about("tests things").arg(
Arg::new("case")
.long("case")
.takes_value(true)
.help("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'= {
}
]
$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)
.help("the other case to test"),
),
)
.subcommand(App::new("some-cmd-with-hyphens").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-hyphens 'some-cmd-with-hyphens'
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-hyphens'= {
cand -h 'Print help information'
cand --help 'Print help information'
cand -V 'Print version information'
cand --version 'Print version information'
}
&'my_app;help'= {
}
]
$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")
.help("cmd flag"),
)
.arg(
Arg::new("option")
.short('o')
.visible_short_alias('O')
.long("option")
.visible_alias("opt")
.help("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]
}
"#;