diff options
| author | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-01-14 20:18:23 +0100 |
|---|---|---|
| committer | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-01-14 20:18:23 +0100 |
| commit | 4e6071aee97a26610aeee423d830a695b8c4d563 (patch) | |
| tree | 0ec3ab88f6ca93e473ee48e7b0c2d5330162e021 /src/main.rs | |
| parent | de04d2f40c4bcfdbb041551ca7bc1f87e81eefbf (diff) | |
| download | sylt-4e6071aee97a26610aeee423d830a695b8c4d563.tar.gz | |
Pass printing as an argument
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index ab5f4a0..d5cb465 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,15 @@ use std::path::{Path, PathBuf}; use tihdy::run_file; +struct Args { + file: Option<PathBuf>, + print: bool, +} + fn main() { - let file = file_from_args().unwrap_or_else(|| Path::new("tests/simple.tdy").to_owned()); - if let Err(errs) = run_file(&file) { + let args = parse_args(); + let file = args.file.unwrap_or_else(|| Path::new("tests/simple.tdy").to_owned()); + if let Err(errs) = run_file(&file, args.print) { for err in errs.iter() { println!("{}", err); } @@ -12,6 +18,21 @@ fn main() { } } -fn file_from_args() -> Option<PathBuf> { - std::env::args().skip(1).map(|s| Path::new(&s).to_owned()).find(|p| p.is_file()) +fn parse_args() -> Args { + let mut args = Args { + file: None, + print: false, + }; + + for s in std::env::args().skip(1) { + let path = Path::new(&s).to_owned(); + if path.is_file() { + args.file = Some(path); + } else if "-p" == s { + args.print = true; + } else { + eprintln!("Invalid argument {}.", s); + } + }; + args } |
