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,
};
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 theme: PathBuf,
pub repo_path: RepoPath,
@ -23,20 +33,23 @@ pub fn process_cmdline() -> Result<CliArgs> {
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();
std::process::exit(0);
}
if arg_matches.get_flag("logging") {
let logfile = arg_matches.get_one::<String>("logfile");
if arg_matches.get_flag(LOGGING_FLAG_ID) {
let logfile = arg_matches.get_one::<String>(LOG_FILE_FLAG_ID);
setup_logging(logfile.map(PathBuf::from))?;
}
let workdir =
arg_matches.get_one::<String>("workdir").map(PathBuf::from);
let gitdir = arg_matches
.get_one::<String>("directory")
.map_or_else(|| PathBuf::from("."), PathBuf::from);
let workdir = arg_matches
.get_one::<String>(WORKDIR_FLAG_ID)
.map(PathBuf::from);
let gitdir =
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 {
RepoPath::Workdir { gitdir, workdir: w }
@ -45,8 +58,8 @@ pub fn process_cmdline() -> Result<CliArgs> {
};
let arg_theme = arg_matches
.get_one::<String>("theme")
.map_or_else(|| PathBuf::from("theme.ron"), PathBuf::from);
.get_one::<String>(THEME_FLAG_ID)
.map_or_else(|| PathBuf::from(DEFAULT_THEME), PathBuf::from);
let confpath = get_app_config_path()?;
fs::create_dir_all(&confpath).with_context(|| {
@ -58,7 +71,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
let theme = confpath.join(arg_theme);
let notify_watcher: bool =
*arg_matches.get_one("watcher").unwrap_or(&false);
*arg_matches.get_one(WATCHER_FLAG_ID).unwrap_or(&false);
Ok(CliArgs {
theme,
@ -84,40 +97,40 @@ fn app() -> ClapApp {
",
)
.arg(
Arg::new("theme")
Arg::new(THEME_FLAG_ID)
.help("Set color theme filename loaded from config directory")
.short('t')
.long("theme")
.value_name("THEME_FILE")
.default_value("theme.ron")
.default_value(DEFAULT_THEME)
.num_args(1),
)
.arg(
Arg::new("logging")
Arg::new(LOGGING_FLAG_ID)
.help("Store logging output into a file (in the cache directory by default)")
.short('l')
.long("logging")
.default_value_if("logfile", ArgPredicate::IsPresent, "true")
.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)")
.long("logfile")
.value_name("LOG_FILE"))
.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.")
.long("watcher")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("bugreport")
Arg::new(BUG_REPORT_FLAG_ID)
.help("Generate a bug report")
.long("bugreport")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("directory")
Arg::new(GIT_DIR_FLAG_ID)
.help("Set the git directory")
.short('d')
.long("directory")
@ -125,7 +138,7 @@ fn app() -> ClapApp {
.num_args(1),
)
.arg(
Arg::new("workdir")
Arg::new(WORKDIR_FLAG_ID)
.help("Set the working directory")
.short('w')
.long("workdir")