Skip to main content

subcommands/
subcommands.rs

1use clap::{Parser, ValueEnum};
2use clap_tui::Tui;
3
4#[derive(Debug, Parser, PartialEq, Eq)]
5#[command(name = "tool", about = "Subcommand example", version = "0.1.0")]
6enum Command {
7    /// Launch the interactive TUI
8    Tui,
9    Build {
10        #[arg(short, long)]
11        release: bool,
12
13        #[arg(short, long, value_enum, default_value_t = Color::Blue)]
14        color: Color,
15    },
16    Serve {
17        #[arg(long, default_value = "127.0.0.1")]
18        host: String,
19
20        #[arg(long, default_value_t = 8080)]
21        port: u16,
22    },
23}
24
25#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
26enum Color {
27    Red,
28    Green,
29    Blue,
30}
31
32fn dispatch(command: Command) {
33    match command {
34        Command::Tui => {}
35        other => println!("Selected: {other:?}"),
36    }
37}
38
39fn main() -> Result<(), clap_tui::TuiError> {
40    match Command::parse() {
41        Command::Tui => {
42            if let Some(command) = Tui::<Command>::new().hide_entrypoint("tui")?.run()? {
43                dispatch(command);
44            }
45        }
46        command => dispatch(command),
47    }
48
49    Ok(())
50}