usage_cli/cli/
mod.rs

1use crate::usage_spec;
2use clap::{Parser, Subcommand};
3use miette::Result;
4
5mod complete_word;
6mod exec;
7mod generate;
8mod shell;
9
10#[derive(Parser)]
11#[clap(author, version, about)]
12pub struct Cli {
13    #[clap(subcommand)]
14    command: Command,
15
16    /// Outputs a `usage.kdl` spec for this CLI itself
17    #[clap(long)]
18    usage_spec: bool,
19
20    /// Outputs completions for the specified shell for completing the `usage` CLI itself
21    completions: Option<String>,
22}
23
24#[derive(Subcommand)]
25enum Command {
26    CompleteWord(complete_word::CompleteWord),
27    Exec(exec::Exec),
28    Generate(generate::Generate),
29    #[clap(about = "Use bash to execute the script")]
30    Bash(shell::Shell),
31    #[clap(about = "use fish to execute the script")]
32    Fish(shell::Shell),
33    #[clap(about = "use zsh to execute the script")]
34    Zsh(shell::Shell),
35}
36
37impl Cli {
38    pub fn run(argv: &[String]) -> Result<()> {
39        let cli = Self::parse_from(argv);
40        if cli.usage_spec {
41            return usage_spec::generate();
42        }
43        match cli.command {
44            Command::Bash(mut cmd) => cmd.run("bash"),
45            Command::Fish(mut cmd) => cmd.run("fish"),
46            Command::Zsh(mut cmd) => cmd.run("zsh"),
47            Command::Generate(cmd) => cmd.run(),
48            Command::Exec(mut cmd) => cmd.run(),
49            Command::CompleteWord(cmd) => cmd.run(),
50        }
51    }
52}