Skip to content

Commit 3b6a251

Browse files
committed
refactor(args.rs): make the flags id slices and the default values as a const
1 parent 1d22485 commit 3b6a251

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/args.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ use std::{
1212
path::PathBuf,
1313
};
1414

15+
const BUG_REPORT_FLAG_ID: &str = "bugreport";
16+
const LOG_FILE_FLAG_ID: &str = "logfile";
17+
const LOGGING_FLAG_ID: &str = "logging";
18+
const THEME_FLAG_ID: &str = "theme";
19+
const WORKDIR_FLAG_ID: &str = "workdir";
20+
const GIT_DIR_FLAG_ID: &str = "directory";
21+
const WATCHER_FLAG_ID: &str = "watcher";
22+
const DEFAULT_THEME: &str = "theme.ron";
23+
const DEFAULT_GIT_DIR: &str = ".";
24+
1525
pub struct CliArgs {
1626
pub theme: PathBuf,
1727
pub repo_path: RepoPath,
@@ -23,20 +33,23 @@ pub fn process_cmdline() -> Result<CliArgs> {
2333

2434
let arg_matches = app.get_matches();
2535

26-
if arg_matches.get_flag("bugreport") {
36+
if arg_matches.get_flag(BUG_REPORT_FLAG_ID) {
2737
bug_report::generate_bugreport();
2838
std::process::exit(0);
2939
}
30-
if arg_matches.get_flag("logging") {
31-
let logfile = arg_matches.get_one::<String>("logfile");
40+
if arg_matches.get_flag(LOGGING_FLAG_ID) {
41+
let logfile = arg_matches.get_one::<String>(LOG_FILE_FLAG_ID);
3242
setup_logging(logfile.map(PathBuf::from))?;
3343
}
3444

35-
let workdir =
36-
arg_matches.get_one::<String>("workdir").map(PathBuf::from);
37-
let gitdir = arg_matches
38-
.get_one::<String>("directory")
39-
.map_or_else(|| PathBuf::from("."), PathBuf::from);
45+
let workdir = arg_matches
46+
.get_one::<String>(WORKDIR_FLAG_ID)
47+
.map(PathBuf::from);
48+
let gitdir =
49+
arg_matches.get_one::<String>(GIT_DIR_FLAG_ID).map_or_else(
50+
|| PathBuf::from(DEFAULT_GIT_DIR),
51+
PathBuf::from,
52+
);
4053

4154
let repo_path = if let Some(w) = workdir {
4255
RepoPath::Workdir { gitdir, workdir: w }
@@ -45,8 +58,8 @@ pub fn process_cmdline() -> Result<CliArgs> {
4558
};
4659

4760
let arg_theme = arg_matches
48-
.get_one::<String>("theme")
49-
.map_or_else(|| PathBuf::from("theme.ron"), PathBuf::from);
61+
.get_one::<String>(THEME_FLAG_ID)
62+
.map_or_else(|| PathBuf::from(DEFAULT_THEME), PathBuf::from);
5063

5164
let confpath = get_app_config_path()?;
5265
fs::create_dir_all(&confpath).with_context(|| {
@@ -58,7 +71,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
5871
let theme = confpath.join(arg_theme);
5972

6073
let notify_watcher: bool =
61-
*arg_matches.get_one("watcher").unwrap_or(&false);
74+
*arg_matches.get_one(WATCHER_FLAG_ID).unwrap_or(&false);
6275

6376
Ok(CliArgs {
6477
theme,
@@ -84,48 +97,48 @@ fn app() -> ClapApp {
8497
",
8598
)
8699
.arg(
87-
Arg::new("theme")
100+
Arg::new(THEME_FLAG_ID)
88101
.help("Set color theme filename loaded from config directory")
89102
.short('t')
90103
.long("theme")
91104
.value_name("THEME_FILE")
92-
.default_value("theme.ron")
105+
.default_value(DEFAULT_THEME)
93106
.num_args(1),
94107
)
95108
.arg(
96-
Arg::new("logging")
109+
Arg::new(LOGGING_FLAG_ID)
97110
.help("Store logging output into a file (in the cache directory by default)")
98111
.short('l')
99112
.long("logging")
100113
.default_value_if("logfile", ArgPredicate::IsPresent, "true")
101114
.action(clap::ArgAction::SetTrue),
102115
)
103-
.arg(Arg::new("logfile")
116+
.arg(Arg::new(LOG_FILE_FLAG_ID)
104117
.help("Store logging output into the specified file (implies --logging)")
105118
.long("logfile")
106119
.value_name("LOG_FILE"))
107120
.arg(
108-
Arg::new("watcher")
121+
Arg::new(WATCHER_FLAG_ID)
109122
.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.")
110123
.long("watcher")
111124
.action(clap::ArgAction::SetTrue),
112125
)
113126
.arg(
114-
Arg::new("bugreport")
127+
Arg::new(BUG_REPORT_FLAG_ID)
115128
.help("Generate a bug report")
116129
.long("bugreport")
117130
.action(clap::ArgAction::SetTrue),
118131
)
119132
.arg(
120-
Arg::new("directory")
133+
Arg::new(GIT_DIR_FLAG_ID)
121134
.help("Set the git directory")
122135
.short('d')
123136
.long("directory")
124137
.env("GIT_DIR")
125138
.num_args(1),
126139
)
127140
.arg(
128-
Arg::new("workdir")
141+
Arg::new(WORKDIR_FLAG_ID)
129142
.help("Set the working directory")
130143
.short('w')
131144
.long("workdir")

0 commit comments

Comments
 (0)