1use std::path::PathBuf;
4
5use anyhow::Result;
6use clap::{Parser, Subcommand};
7
8use crate::commands;
9
10#[derive(Debug, Parser)]
12#[command(
13 version,
14 about,
15 long_about = None
16)]
17pub struct Cli {
18 #[command(subcommand)]
20 pub command: Option<Commands>,
21 #[arg(short = 'C', long = "repo", global = true, value_name = "PATH")]
23 pub repo: Option<PathBuf>,
24 #[arg(short, long, global = true)]
26 pub verbose: bool,
27 #[arg(long, global = true, value_enum, default_value = "auto")]
29 pub theme: ThemeArg,
30 #[arg(long, global = true, value_enum, default_value = "auto")]
32 pub lang: LangArg,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
37pub enum LangArg {
38 Auto,
40 Zh,
42 En,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
48pub enum ThemeArg {
49 Auto,
51 Dark,
53 Light,
55}
56
57#[derive(Debug, Subcommand)]
59pub enum Commands {
60 Merge(commands::run::MergeArgs),
62 Rebase(commands::run::RebaseArgs),
64 Pull(commands::run::PullArgs),
66 CherryPick(commands::run::CherryPickArgs),
68 Revert(commands::run::RevertArgs),
70 File(commands::file::FileArgs),
72 Abort,
74}
75
76impl Cli {
77 pub fn run(self) -> Result<()> {
79 crate::i18n::init(match self.lang {
80 LangArg::Zh => crate::i18n::Lang::Zh,
81 LangArg::En => crate::i18n::Lang::En,
82 LangArg::Auto => crate::i18n::detect(),
83 });
84 let verbose = self.verbose;
85 let dir = self.repo.unwrap_or_else(|| PathBuf::from("."));
86 let light = match self.theme {
87 ThemeArg::Light => true,
88 ThemeArg::Dark => false,
89 ThemeArg::Auto => crate::ui::detect_light(),
90 };
91 match self.command {
92 None => commands::menu::run(verbose, &dir, light),
93 Some(Commands::Merge(args)) => commands::run::merge(args, verbose, &dir, light),
94 Some(Commands::Rebase(args)) => commands::run::rebase(args, verbose, &dir, light),
95 Some(Commands::Pull(args)) => commands::run::pull(args, verbose, &dir, light),
96 Some(Commands::CherryPick(args)) => {
97 commands::run::cherry_pick(args, verbose, &dir, light)
98 }
99 Some(Commands::Revert(args)) => commands::run::revert(args, verbose, &dir, light),
100 Some(Commands::File(args)) => commands::file::run(args, light),
101 Some(Commands::Abort) => commands::abort::run(verbose, &dir),
102 }
103 }
104}