aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorTeddy Wing2020-08-02 10:43:54 +0200
committerTeddy Wing2020-08-02 10:43:54 +0200
commit48e4130e825de244592d7cf0631d3b0552d42ef1 (patch)
treee6d2a5b5fba0bdf4b6a9cb406c7b6f00679575f1 /src/config.rs
parent20e98432afb02262703a2bd4524e6a87f3145086 (diff)
downloadgit-suggestion-48e4130e825de244592d7cf0631d3b0552d42ef1.tar.bz2
Move suggestion arguments check to `Config::get`
Since this check for suggestion arguments is in both binaries, we can move it to the common preflight function. Clean up other areas now that we moved the function.
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs28
1 files changed, 14 insertions, 14 deletions
diff --git a/src/config.rs b/src/config.rs
index 07768f0..e605439 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -28,17 +28,14 @@ pub enum Error {
Git(#[from] git2::Error),
}
-pub struct Config<'a> {
+pub struct Config {
pub github_token: String,
pub o_r: Result<OwnerRepo, owner_repo::Error>,
pub suggestions: Vec<String>,
-
- opts: Options,
- usage_brief: &'a str,
}
-impl<'a> Config<'a> {
- pub fn get(args: &[String], usage_brief: &'a str) -> Result<Self, Error> {
+impl Config {
+ pub fn get(args: &[String], usage_brief: &str) -> Result<Self, Error> {
let mut opts = Options::new();
opts.optopt(
@@ -58,7 +55,13 @@ impl<'a> Config<'a> {
let opt_matches = opts.parse(&args[1..])?;
if opt_matches.opt_present("h") {
- print!("{}", opts.usage(&usage_brief));
+ print_usage(&opts, usage_brief);
+
+ process::exit(exitcode::USAGE);
+ }
+
+ if opt_matches.free.is_empty() {
+ print_usage(&opts, usage_brief);
process::exit(exitcode::USAGE);
}
@@ -73,16 +76,9 @@ impl<'a> Config<'a> {
github_token: Self::github_token(&opt_matches, &git_config)?,
o_r: o_r,
suggestions: opt_matches.free,
-
- opts: opts,
- usage_brief,
})
}
- pub fn print_usage(&self) {
- print!("{}", self.opts.usage(&self.usage_brief))
- }
-
fn github_token(
opt_matches: &getopts::Matches,
git_config: &git2::Config,
@@ -119,6 +115,10 @@ impl<'a> Config<'a> {
}
}
+fn print_usage(opts: &Options, brief: &str) {
+ print!("{}", opts.usage(brief));
+}
+
fn git_config_key(key: &str) -> String {
format!("{}.{}", GIT_CONFIG_PREFIX, key)
}