1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use clap::{Parser, Subcommand};
mod new;

#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct Startup {
    /// Debug mode
    #[clap(short,long,action=clap::ArgAction::Count)]
    debug: u8,

    #[command(subcommand)]
    command: Command,
}

// 定义子命令
#[derive(Debug, Subcommand)]
enum Command {
    /// Start a new project
    New,
}

// 为Startup添加run方法,在main函数中调用
impl Startup {
    pub fn start() -> Self {
        Startup::parse()
    }

    pub fn run(&self) {
        // 初始化tracing日志
        let level = if self.debug > 0 {
            tracing::Level::TRACE
        } else {
            tracing::Level::INFO
        };
        tracing_subscriber::fmt().with_max_level(level).init();
        // 运行命令
        self.command.run();
    }
}

// 为command添加run方法,不同的命令调不同的方法
impl Command {
    fn run(&self) {
        match self {
            Command::New => new::create_new_project(),
        }
    }
}