diff --git a/2025/01/rust_demo1/.gitignore b/2025/01/rust_demo1/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/2025/01/rust_demo1/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2025/01/rust_demo1/Cargo.lock b/2025/01/rust_demo1/Cargo.lock new file mode 100644 index 0000000..3ff4f47 --- /dev/null +++ b/2025/01/rust_demo1/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "rust_demo1" +version = "0.1.0" +dependencies = [ + "getopts", +] + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" diff --git a/2025/01/rust_demo1/Cargo.toml b/2025/01/rust_demo1/Cargo.toml new file mode 100644 index 0000000..b0a38a2 --- /dev/null +++ b/2025/01/rust_demo1/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "rust_demo1" +version = "0.1.0" +edition = "2021" + +[dependencies] +getopts = "0.2.21" diff --git a/2025/01/rust_demo1/src/main.rs b/2025/01/rust_demo1/src/main.rs new file mode 100644 index 0000000..b5bf49c --- /dev/null +++ b/2025/01/rust_demo1/src/main.rs @@ -0,0 +1,37 @@ +use getopts::Options; + +/** +fn get_help(args: &Vec, opts: &Options) -> String { + return format!("Usage: {} -h? --help?", args[0]); +} +**/ + +fn main() { + println!("Hello, world!"); + + for (key,arg) in std::env::args_os().enumerate() { + println!("[{}] Arg: {:?}", key, arg.to_str().expect("Arg should be serializable to string")); + } + + let args: Vec = std::env::args().collect(); + let mut opts = Options::new(); + opts.optflag("h", "help", "print this help"); + // let usage = get_help(&args, &opts); + // opts.usage(&usage); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => { m } + Err(f) => panic!("Got error: {}", f.to_string()) + }; + + if matches.opt_present("h") { + // println!(get_help(&args, &opts)); + let program = format!("{} [args]", args[0]); + println!("Usage: {}", opts.usage(&program)); + return; + } + + println!("Running..."); + + +}