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
49
50
51
52
53
54
55
56
57
58
pub(crate) mod commands;
pub(crate) mod config;
pub(crate) mod constants;
pub(crate) mod errors;

use anyhow::Result;
use clap::{Parser, Subcommand};
use commands::config::{ConfigCommand, ConfigCommandParser};
use commands::entity::{EntityCommand, EntityCommandParser};
use commands::project::{ProjectCommand, ProjectCommandParser};
use std::fs;
use std::path::PathBuf;

#[derive(Subcommand)]
enum CliCommand {
    /// OpenBOR project (mod) related operations.
    Project(ProjectCommandParser),
    /// OpenBOR entity (model) related operations.
    Entity(EntityCommandParser),
    /// This CLI application's configuration related operations.
    Config(ConfigCommandParser),
    #[clap(hide = true)]
    PrintAllHelp {
        #[arg(short, long)]
        out_path: PathBuf,
    },
}

#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(next_line_help = true)]
struct Cli {
    #[command(subcommand)]
    command: CliCommand,
}

pub fn run() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        CliCommand::Project(project) => match project.command {
            ProjectCommand::Create(project_create) => project_create.run(),
        },
        CliCommand::Entity(entity) => match entity.command {
            EntityCommand::Create(entity_create) => entity_create.run(),
        },
        CliCommand::Config(config) => match config.command {
            ConfigCommand::Create(config_create) => config_create.run(),
        },
        CliCommand::PrintAllHelp { out_path } => {
            let markdown_str = clap_markdown::help_markdown::<Cli>();

            fs::write(out_path, markdown_str)?;

            Ok(())
        }
    }
}