axum_startup/
lib.rs

1use clap::{Parser, Subcommand};
2mod new;
3
4#[derive(Debug, Parser)]
5#[command(author, version, about, long_about = None)]
6pub struct Startup {
7    /// Debug mode
8    #[clap(short,long,action=clap::ArgAction::Count)]
9    debug: u8,
10
11    #[command(subcommand)]
12    command: Command,
13}
14
15// 定义子命令
16#[derive(Debug, Subcommand)]
17enum Command {
18    /// Start a new project
19    New,
20}
21
22// 为Startup添加run方法,在main函数中调用
23impl Startup {
24    pub fn start() -> Self {
25        Startup::parse()
26    }
27
28    pub fn run(&self) {
29        // 初始化tracing日志
30        let level = if self.debug > 0 {
31            tracing::Level::TRACE
32        } else {
33            tracing::Level::INFO
34        };
35        tracing_subscriber::fmt().with_max_level(level).init();
36        // 运行命令
37        self.command.run();
38    }
39}
40
41// 为command添加run方法,不同的命令调不同的方法
42impl Command {
43    fn run(&self) {
44        match self {
45            Command::New => new::create_new_project(),
46        }
47    }
48}