clap/clap_complete_fig/tests/value_hints.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
4.5 KiB
Rust

use clap::{App, AppSettings, Arg, ValueHint};
use clap_complete_fig::Fig;
use completions::common;
mod completions;
pub fn build_app_with_value_hints() -> App<'static> {
App::new("my_app")
.setting(AppSettings::DisableVersionFlag)
.setting(AppSettings::TrailingVarArg)
.arg(
Arg::new("choice")
.long("choice")
.possible_values(["bash", "fish", "zsh"]),
)
.arg(
Arg::new("unknown")
.long("unknown")
.value_hint(ValueHint::Unknown),
)
.arg(Arg::new("other").long("other").value_hint(ValueHint::Other))
.arg(
Arg::new("path")
.long("path")
.short('p')
.value_hint(ValueHint::AnyPath),
)
.arg(
Arg::new("file")
.long("file")
.short('f')
.value_hint(ValueHint::FilePath),
)
.arg(
Arg::new("dir")
.long("dir")
.short('d')
.value_hint(ValueHint::DirPath),
)
.arg(
Arg::new("exe")
.long("exe")
.short('e')
.value_hint(ValueHint::ExecutablePath),
)
.arg(
Arg::new("cmd_name")
.long("cmd-name")
.value_hint(ValueHint::CommandName),
)
.arg(
Arg::new("cmd")
.long("cmd")
.short('c')
.value_hint(ValueHint::CommandString),
)
.arg(
Arg::new("command_with_args")
.takes_value(true)
.multiple_values(true)
.value_hint(ValueHint::CommandWithArguments),
)
.arg(
Arg::new("user")
.short('u')
.long("user")
.value_hint(ValueHint::Username),
)
.arg(
Arg::new("host")
.short('h')
.long("host")
.value_hint(ValueHint::Hostname),
)
.arg(Arg::new("url").long("url").value_hint(ValueHint::Url))
.arg(
Arg::new("email")
.long("email")
.value_hint(ValueHint::EmailAddress),
)
}
static FIG_VALUE_HINTS: &str = r#"const completion: Fig.Spec = {
name: "my_app",
description: "",
options: [
{
name: "--choice",
args: {
name: "choice",
isOptional: true,
suggestions: [
{
name: "bash",
},
{
name: "fish",
},
{
name: "zsh",
},
]
},
},
{
name: "--unknown",
args: {
name: "unknown",
isOptional: true,
},
},
{
name: "--other",
args: {
name: "other",
isOptional: true,
},
},
{
name: ["-p", "--path"],
args: {
name: "path",
isOptional: true,
template: "filepaths",
},
},
{
name: ["-f", "--file"],
args: {
name: "file",
isOptional: true,
template: "filepaths",
},
},
{
name: ["-d", "--dir"],
args: {
name: "dir",
isOptional: true,
template: "folders",
},
},
{
name: ["-e", "--exe"],
args: {
name: "exe",
isOptional: true,
template: "filepaths",
},
},
{
name: "--cmd-name",
args: {
name: "cmd_name",
isOptional: true,
isCommand: true,
},
},
{
name: ["-c", "--cmd"],
args: {
name: "cmd",
isOptional: true,
isCommand: true,
},
},
{
name: ["-u", "--user"],
args: {
name: "user",
isOptional: true,
},
},
{
name: ["-h", "--host"],
args: {
name: "host",
isOptional: true,
},
},
{
name: "--url",
args: {
name: "url",
isOptional: true,
},
},
{
name: "--email",
args: {
name: "email",
isOptional: true,
},
},
{
name: "--help",
description: "Print help information",
},
],
args: {
name: "command_with_args",
isVariadic: true,
isOptional: true,
isCommand: true,
},
};
export default completion;
"#;
#[test]
fn fig_with_value_hints() {
let mut app = build_app_with_value_hints();
common(Fig, &mut app, "my_app", FIG_VALUE_HINTS);
}