summaryrefslogtreecommitdiff
path: root/src/flags.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/flags.rs')
-rw-r--r--src/flags.rs34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/flags.rs b/src/flags.rs
index 91acf3a..9432b8f 100644
--- a/src/flags.rs
+++ b/src/flags.rs
@@ -5,7 +5,7 @@ pub struct Flags {
pub arg_count: usize
}
-pub fn parse(args: &[String]) -> Flags {
+pub fn parse(args: &[String]) -> Option<Flags> {
let mut flags = Flags {
help: false,
version: false,
@@ -17,15 +17,21 @@ pub fn parse(args: &[String]) -> Flags {
flags.arg_count += 1;
if arg.starts_with("--") {
let flag = &arg[2..];
- check_flag(&flag, &mut flags);
+ if !set_flag(&flag, &mut flags) {
+ eprintln!("Invalid argument: {}", arg);
+ return None
+ }
} else {
let flag = &arg[1..];
for char in flag.chars() {
- check_flag(&char.to_string(), &mut flags);
+ if !set_flag(&char.to_string(), &mut flags) {
+ eprintln!("Invalid argument: {}", arg);
+ return None
+ }
}
}
}
- flags
+ Some(flags)
}
fn is_arg(arg: &str) -> bool {
@@ -36,16 +42,18 @@ const HELP_FLAG: &str = "help h";
const VERSION_FLAG: &str = "version v";
const DONT_PERSIST: &str = "d";
-fn check_flag(arg: &str, flags: &mut Flags) {
+fn set_flag(arg: &str, flags: &mut Flags) -> bool {
if has_flag_set(&arg, HELP_FLAG) {
- flags.help = true
- }
- if has_flag_set(&arg, VERSION_FLAG) {
- flags.version = true
- }
- if has_flag_set(&arg, DONT_PERSIST) {
- flags.dont_persist = true
+ flags.help = true;
+ return true
+ } else if has_flag_set(&arg, VERSION_FLAG) {
+ flags.version = true;
+ return true
+ } else if has_flag_set(&arg, DONT_PERSIST) {
+ flags.dont_persist = true;
+ return true
}
+ false
}
fn has_flag_set(arg: &str, check: &str) -> bool {
@@ -55,4 +63,4 @@ fn has_flag_set(arg: &str, check: &str) -> bool {
}
}
return false
-} \ No newline at end of file
+}