diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2024-03-28 22:24:04 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2024-03-28 22:24:04 +1300 |
| commit | 19d78ca2f8c5d37ba0c8bac76e30714a65c9f7b2 (patch) | |
| tree | 476e51332d42f6cf6116365c0f64cffd4c9939e9 /src/args.rs | |
| parent | cda07006720dc53367d0a552aa2c77796825f82d (diff) | |
Basic implementation of CLI arguments
Diffstat (limited to 'src/args.rs')
| -rw-r--r-- | src/args.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..6753664 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,51 @@ +use std::env; + +pub struct Args { + pub directory: String, + pub n_sentences: u32, + pub unique: bool, +} + +impl Args { + pub fn parse() -> Option<Args> { + let arg_vec: Vec<String> = env::args().collect(); + let mut i = 1; //skip first arg (execution path) + + let mut n_sentences = 1; + let mut unique = false; + let mut opt_directory: Option<String> = None; + + while i < arg_vec.len() { + match arg_vec[i].as_str() { + "-n" | "--sentences" => { + if i + 1 < arg_vec.len() { + n_sentences = arg_vec[i+1].parse::<u32>().ok()?; + } else { + return None; + } + i += 2; + }, + "--unique" => { + unique = true; + i += 1; + }, + other => { + opt_directory = match opt_directory { + Some(_) => { return None; }, + None => Some(other.to_string()), + }; + i += 1; + }, + } + } + + let directory = opt_directory?; + + Some(Args { + directory, + n_sentences, + unique, + }) + } +} + |
