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