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 completions for the specified shell for completing the `usage` CLI itself
17    completions: Option<String>,
18
19    /// Outputs a `usage.kdl` spec for this CLI itself
20    #[clap(long)]
21    usage_spec: bool,
22}
23
24#[derive(Subcommand)]
25enum Command {
26    #[clap(about = "Execute a shell script using bash")]
27    Bash(shell::Shell),
28    CompleteWord(complete_word::CompleteWord),
29    Exec(exec::Exec),
30    #[clap(about = "Execute a shell script using fish")]
31    Fish(shell::Shell),
32    Generate(generate::Generate),
33    #[clap(about = "Execute a shell script using zsh")]
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}