refactor(args.rs): make the flags id slices and the default values as a const (#2733)

Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com>
This commit is contained in:
0x61nas 2025-10-28 20:03:48 +03:00 committed by GitHub
parent 6bb216c0d4
commit ec65b372e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,16 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
const BUG_REPORT_FLAG_ID: &str = "bugreport";
const LOG_FILE_FLAG_ID: &str = "logfile";
const LOGGING_FLAG_ID: &str = "logging";
const THEME_FLAG_ID: &str = "theme";
const WORKDIR_FLAG_ID: &str = "workdir";
const GIT_DIR_FLAG_ID: &str = "directory";
const WATCHER_FLAG_ID: &str = "watcher";
const DEFAULT_THEME: &str = "theme.ron";
const DEFAULT_GIT_DIR: &str = ".";
pub struct CliArgs { pub struct CliArgs {
pub theme: PathBuf, pub theme: PathBuf,
pub repo_path: RepoPath, pub repo_path: RepoPath,
@ -23,20 +33,23 @@ pub fn process_cmdline() -> Result<CliArgs> {
let arg_matches = app.get_matches(); let arg_matches = app.get_matches();
if arg_matches.get_flag("bugreport") { if arg_matches.get_flag(BUG_REPORT_FLAG_ID) {
bug_report::generate_bugreport(); bug_report::generate_bugreport();
std::process::exit(0); std::process::exit(0);
} }
if arg_matches.get_flag("logging") { if arg_matches.get_flag(LOGGING_FLAG_ID) {
let logfile = arg_matches.get_one::<String>("logfile"); let logfile = arg_matches.get_one::<String>(LOG_FILE_FLAG_ID);
setup_logging(logfile.map(PathBuf::from))?; setup_logging(logfile.map(PathBuf::from))?;
} }
let workdir = let workdir = arg_matches
arg_matches.get_one::<String>("workdir").map(PathBuf::from); .get_one::<String>(WORKDIR_FLAG_ID)
let gitdir = arg_matches .map(PathBuf::from);
.get_one::<String>("directory") let gitdir =
.map_or_else(|| PathBuf::from("."), PathBuf::from); arg_matches.get_one::<String>(GIT_DIR_FLAG_ID).map_or_else(
|| PathBuf::from(DEFAULT_GIT_DIR),
PathBuf::from,
);
let repo_path = if let Some(w) = workdir { let repo_path = if let Some(w) = workdir {
RepoPath::Workdir { gitdir, workdir: w } RepoPath::Workdir { gitdir, workdir: w }
@ -45,8 +58,8 @@ pub fn process_cmdline() -> Result<CliArgs> {
}; };
let arg_theme = arg_matches let arg_theme = arg_matches
.get_one::<String>("theme") .get_one::<String>(THEME_FLAG_ID)
.map_or_else(|| PathBuf::from("theme.ron"), PathBuf::from); .map_or_else(|| PathBuf::from(DEFAULT_THEME), PathBuf::from);
let confpath = get_app_config_path()?; let confpath = get_app_config_path()?;
fs::create_dir_all(&confpath).with_context(|| { fs::create_dir_all(&confpath).with_context(|| {
@ -58,7 +71,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
let theme = confpath.join(arg_theme); let theme = confpath.join(arg_theme);
let notify_watcher: bool = let notify_watcher: bool =
*arg_matches.get_one("watcher").unwrap_or(&false); *arg_matches.get_one(WATCHER_FLAG_ID).unwrap_or(&false);
Ok(CliArgs { Ok(CliArgs {
theme, theme,
@ -84,40 +97,40 @@ fn app() -> ClapApp {
", ",
) )
.arg( .arg(
Arg::new("theme") Arg::new(THEME_FLAG_ID)
.help("Set color theme filename loaded from config directory") .help("Set color theme filename loaded from config directory")
.short('t') .short('t')
.long("theme") .long("theme")
.value_name("THEME_FILE") .value_name("THEME_FILE")
.default_value("theme.ron") .default_value(DEFAULT_THEME)
.num_args(1), .num_args(1),
) )
.arg( .arg(
Arg::new("logging") Arg::new(LOGGING_FLAG_ID)
.help("Store logging output into a file (in the cache directory by default)") .help("Store logging output into a file (in the cache directory by default)")
.short('l') .short('l')
.long("logging") .long("logging")
.default_value_if("logfile", ArgPredicate::IsPresent, "true") .default_value_if("logfile", ArgPredicate::IsPresent, "true")
.action(clap::ArgAction::SetTrue), .action(clap::ArgAction::SetTrue),
) )
.arg(Arg::new("logfile") .arg(Arg::new(LOG_FILE_FLAG_ID)
.help("Store logging output into the specified file (implies --logging)") .help("Store logging output into the specified file (implies --logging)")
.long("logfile") .long("logfile")
.value_name("LOG_FILE")) .value_name("LOG_FILE"))
.arg( .arg(
Arg::new("watcher") Arg::new(WATCHER_FLAG_ID)
.help("Use notify-based file system watcher instead of tick-based update. This is more performant, but can cause issues on some platforms. See https://github.com/gitui-org/gitui/blob/master/FAQ.md#watcher for details.") .help("Use notify-based file system watcher instead of tick-based update. This is more performant, but can cause issues on some platforms. See https://github.com/gitui-org/gitui/blob/master/FAQ.md#watcher for details.")
.long("watcher") .long("watcher")
.action(clap::ArgAction::SetTrue), .action(clap::ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new("bugreport") Arg::new(BUG_REPORT_FLAG_ID)
.help("Generate a bug report") .help("Generate a bug report")
.long("bugreport") .long("bugreport")
.action(clap::ArgAction::SetTrue), .action(clap::ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new("directory") Arg::new(GIT_DIR_FLAG_ID)
.help("Set the git directory") .help("Set the git directory")
.short('d') .short('d')
.long("directory") .long("directory")
@ -125,7 +138,7 @@ fn app() -> ClapApp {
.num_args(1), .num_args(1),
) )
.arg( .arg(
Arg::new("workdir") Arg::new(WORKDIR_FLAG_ID)
.help("Set the working directory") .help("Set the working directory")
.short('w') .short('w')
.long("workdir") .long("workdir")