diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2024-03-30 23:09:34 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2024-03-30 23:09:34 +1300 |
| commit | 437b8b7a17dc09cd7281d1a09d681874fb166da8 (patch) | |
| tree | 51a86482d6af12f3c0503a7c9cb46eae57ca5061 /src/main.rs | |
| parent | 4ef3b4a2997e11a7969ffb02e3cda6f3b9a3b651 (diff) | |
More robust argument parsing
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index 8c74322..a96f1a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,11 @@ use std::process; pub mod args; use rand::{thread_rng, seq::{SliceRandom, IteratorRandom}}; -use crate::args::Args; +use crate::args::{Args, ArgError, HELP_STR}; #[derive(Debug)] enum Error { - BadArguments, + BadArguments(ArgError), NoSentences, MultipleSentences, BadTemplate(String), @@ -30,8 +30,8 @@ enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::BadArguments => - write!(f, "error while parsing arguments"), + Error::BadArguments(e) => + write!(f, "Error while parsing arguments: {}\n{}", e, HELP_STR), Error::NoSentences => write!(f, "no sentences.txt file found"), Error::MultipleSentences => @@ -64,6 +64,12 @@ impl From<io::Error> for Error { } } +impl From<ArgError> for Error { + fn from(error: ArgError) -> Self { + Error::BadArguments(error) + } +} + type ProgResult<T> = Result<T, Error>; #[derive(Debug, Clone)] @@ -316,7 +322,7 @@ fn crash(e: Error) -> ! { } fn sub_main() -> ProgResult<()> { - let args = Args::parse().ok_or(Error::BadArguments)?; + let args = Args::parse()?; let (mut sentences, mut categories) = read_files(&args.directory, args.unique)?; for _i in 0..args.n_sentences { |
