Skip to content

Commit

Permalink
refactor(complete): Add a subcommand to examples
Browse files Browse the repository at this point in the history
It's useful when testing to have a subcommand in the examples.
  • Loading branch information
martinvonz committed Sep 29, 2022
1 parent 8b33da3 commit 6bc8d26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 12 additions & 1 deletion clap_complete/examples/completion-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! . ./value_hints_derive.fish
//! ./target/debug/examples/value_hints_derive --<TAB>
//! ```
use clap::{Command, CommandFactory, Parser, ValueHint};
use clap::{Args, Command, CommandFactory, Parser, Subcommand, ValueHint};
use clap_complete::{generate, Generator, Shell};
use std::ffi::OsString;
use std::io;
Expand All @@ -24,6 +24,17 @@ struct Opt {
// If provided, outputs the completion file for given shell
#[arg(long = "generate", value_enum)]
generator: Option<Shell>,
#[clap(subcommand)]
command: Option<Commands>,
}

#[derive(Subcommand, Debug, PartialEq)]
enum Commands {
ValueHint(ValueHintOpt),
}

#[derive(Args, Debug, PartialEq)]
struct ValueHintOpt {
// Showcasing all possible ValueHints:
#[arg(long, value_hint = ValueHint::Unknown)]
unknown: Option<String>,
Expand Down
15 changes: 9 additions & 6 deletions clap_complete/examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ use clap_complete::{generate, Generator, Shell};
use std::io;

fn build_cli() -> Command {
Command::new("completion")
.arg(
Arg::new("generator")
.long("generate")
.value_parser(value_parser!(Shell)),
)
let value_hint_command = Command::new("value-hint")
.arg(
Arg::new("unknown")
.long("unknown")
Expand Down Expand Up @@ -87,7 +82,15 @@ fn build_cli() -> Command {
Arg::new("email")
.long("email")
.value_hint(ValueHint::EmailAddress),
);

Command::new("completion")
.arg(
Arg::new("generator")
.long("generate")
.value_parser(value_parser!(Shell)),
)
.subcommand(value_hint_command)
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
Expand Down

0 comments on commit 6bc8d26

Please sign in to comment.