From 64c5abd9b9a55e4deabda457c4048ef1bed5e8e3 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 29 Aug 2026 12:48:47 -0400 Subject: [PATCH] Start parsing command line arguments --- src/cli.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index c76ab55..8356ce3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,33 @@ +macro_rules! options { + ($($name:ident $sflag:literal $lflag:literal $hasarg:literal),* $(,)?) => { + enum Options { + $($name,)* + } + + const SHORT_FLAGS: &[&str] = &[ + $($sflag,)* + ]; + + const LONG_FLAGS: &[&str] = &[ + $($lflag,)* + ]; + + const HAS_ARG: &[bool] = &[ + $($hasarg,)* + ]; + }; +} + +options! { + Width "w" "width" true, + Height "h" "height" true, +} + #[derive(Default)] pub struct Cli { width: Option, height: Option, + input_file: String, } impl Cli { @@ -10,6 +36,22 @@ impl Cli { } pub fn run(&mut self) -> Option<()> { + let mut args = std::env::args(); + /* Skip program name. */ + args.next(); + loop { + if let Some(arg) = args.next() { + if arg.starts_with("--") { + let flag = &arg[2..]; + } else if arg.starts_with("-") { + let flag = &arg[1..2]; + } else { + self.input_file = arg; + } + } else { + break; + } + } Some(()) } }