1
use std::process;
2

            
3
use clap::{Parser, Subcommand};
4

            
5
#[derive(Parser)]
6
#[command(author, version, about, long_about=None, arg_required_else_help = true)]
7
struct Cli {
8
    #[command(subcommand)]
9
    command: Option<Commands>,
10
}
11

            
12
#[derive(Subcommand)]
13
enum Commands {
14
    /// Ascend directory by count or path component
15
    Cd {
16
        /// Number of levels to ascend or directory to ascend to
17
        target: cdup::NumberOrString,
18
    },
19
    /// Give completion hint for given term
20
    Hint {
21
        /// Term to print hint for
22
        term: Option<String>,
23
    },
24
    /// Initialize function for shell
25
    Init {
26
        /// Function name to use
27
        #[arg(short, long, default_value = "up")]
28
        alias: String,
29
        /// Used shell
30
        hint: Option<String>,
31
    },
32
}
33

            
34
fn main() {
35
    let cli = Cli::parse();
36

            
37
    let result = match &cli.command {
38
        Some(Commands::Cd { target }) => cdup::run_cd(target),
39
        Some(Commands::Hint { term }) => cdup::run_hint(term.as_deref()),
40
        Some(Commands::Init { alias, hint }) => cdup::run_init(alias, hint),
41
        None => unreachable!(),
42
    };
43

            
44
    match result {
45
        Ok(output) => println!("{output}"),
46
        Err(e) => {
47
            eprintln!("{e}");
48
            process::exit(1);
49
        }
50
    }
51
}