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/main.rs | |
| parent | cda07006720dc53367d0a552aa2c77796825f82d (diff) | |
Basic implementation of CLI arguments
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 4f299d4..82e568e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,11 +6,16 @@ use std::io; use std::path::PathBuf; use std::process; +pub mod args; + + use rand::{thread_rng, seq::{SliceRandom, IteratorRandom}}; use clap::Parser; +use crate::args::Args; #[derive(Debug)] enum Error { + BadArguments, NoSentences, MultipleSentences, BadTemplate(String), @@ -27,6 +32,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::NoSentences => write!(f, "no sentences.txt file found"), Error::MultipleSentences => @@ -64,7 +71,7 @@ type ProgResult<T> = Result<T, Error>; #[derive(Parser, Debug)] #[command(name = "spout")] #[command(about = "Generates nonsense", long_about = None)] -struct Args { +struct OldArgs { /// Path to data directory directory: String, @@ -326,7 +333,8 @@ fn crash(e: Error) -> ! { process::exit(1); } -fn sub_main(args: Args) -> ProgResult<()> { +fn sub_main() -> ProgResult<()> { + let args = Args::parse().ok_or(Error::BadArguments)?; let (mut sentences, mut categories) = read_files(&args.directory, args.unique)?; for _i in 0..args.n_sentences { @@ -339,9 +347,7 @@ fn sub_main(args: Args) -> ProgResult<()> { } fn main() { - let args = Args::parse(); - - if let Err(e) = sub_main(args) { + if let Err(e) = sub_main() { crash(e) } } |
