structopt_utilities/completions/
new.rs

1use super::{App, Shell};
2use pipe_trait::*;
3use std::path::PathBuf;
4use structopt::clap::{self, Arg};
5
6impl App {
7    pub fn new(name: &str, bin: &str) -> Self {
8        let matches = clap::App::new("Generate shell completion script")
9            .name(name)
10            .arg(
11                Arg::with_name("binary")
12                    .long("bin")
13                    .value_name("binary")
14                    .help("Binary name")
15                    .takes_value(true)
16                    .default_value(bin),
17            )
18            .arg(
19                Arg::with_name("output")
20                    .long("output")
21                    .short("o")
22                    .value_name("file")
23                    .help("Write to file if specify, write to stdout if not")
24                    .takes_value(true),
25            )
26            .arg(
27                Arg::with_name("shell")
28                    .value_name("shell")
29                    .help("Type of shell")
30                    .takes_value(true)
31                    .required(true)
32                    .possible_value("bash")
33                    .possible_value("fish")
34                    .possible_value("zsh")
35                    .possible_value("powershell")
36                    .possible_value("elvish"),
37            )
38            .get_matches();
39
40        App {
41            bin: matches.value_of("bin").unwrap_or(bin).to_string(),
42            output: matches.value_of("output").map(PathBuf::from),
43            shell: matches
44                .value_of("shell")
45                .unwrap()
46                .pipe(|value| unsafe { Shell::parse_from_str_unchecked(value) }),
47        }
48    }
49}