summaryrefslogtreecommitdiff
path: root/src/flags.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2022-11-09 10:49:25 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2022-11-09 10:49:25 -0500
commit191632ce0ab810b4773f64eaf80ad67e87200c0b (patch)
tree51ff03bc25aba69142f0583e52d850424c619b55 /src/flags.rs
parentfix session pid, add uninstall sh file (diff)
downloadcrab-191632ce0ab810b4773f64eaf80ad67e87200c0b.tar.gz
crab-191632ce0ab810b4773f64eaf80ad67e87200c0b.tar.bz2
crab-191632ce0ab810b4773f64eaf80ad67e87200c0b.zip
help and version flags
Diffstat (limited to '')
-rw-r--r--src/flags.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/flags.rs b/src/flags.rs
new file mode 100644
index 0000000..91acf3a
--- /dev/null
+++ b/src/flags.rs
@@ -0,0 +1,58 @@
+pub struct Flags {
+ pub help: bool,
+ pub version: bool,
+ pub dont_persist: bool,
+ pub arg_count: usize
+}
+
+pub fn parse(args: &[String]) -> Flags {
+ let mut flags = Flags {
+ help: false,
+ version: false,
+ dont_persist: false,
+ arg_count: 0
+ };
+ for arg in args {
+ if !is_arg(&arg) { break; }
+ flags.arg_count += 1;
+ if arg.starts_with("--") {
+ let flag = &arg[2..];
+ check_flag(&flag, &mut flags);
+ } else {
+ let flag = &arg[1..];
+ for char in flag.chars() {
+ check_flag(&char.to_string(), &mut flags);
+ }
+ }
+ }
+ flags
+}
+
+fn is_arg(arg: &str) -> bool {
+ return arg.starts_with("-");
+}
+
+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) {
+ 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
+ }
+}
+
+fn has_flag_set(arg: &str, check: &str) -> bool {
+ for check_arg in check.split(" ") {
+ if check_arg == arg {
+ return true
+ }
+ }
+ return false
+} \ No newline at end of file