Basic args parsing
will migrate from getopts in favor of manual parsing
This commit is contained in:
parent
bd13458eab
commit
8fe329c620
4 changed files with 70 additions and 0 deletions
1
q-pidon/.gitignore
vendored
Normal file
1
q-pidon/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target/
|
25
q-pidon/Cargo.lock
generated
Normal file
25
q-pidon/Cargo.lock
generated
Normal file
|
@ -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 = "q-pidon"
|
||||||
|
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"
|
7
q-pidon/Cargo.toml
Normal file
7
q-pidon/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "q-pidon"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
getopts = "0.2"
|
37
q-pidon/src/main.rs
Normal file
37
q-pidon/src/main.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
extern crate getopts;
|
||||||
|
use getopts::Options;
|
||||||
|
use std::env;
|
||||||
|
use std::process::ExitCode;
|
||||||
|
|
||||||
|
fn main() -> ExitCode {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
|
// Define flags
|
||||||
|
let mut opts = Options::new();
|
||||||
|
opts.optflag("h", "help", "print this help menu");
|
||||||
|
opts.optflag("v", "version", "print version");
|
||||||
|
opts.reqopt("t", "type", "type of data/instruction to write", "TYPE");
|
||||||
|
opts.reqopt("v", "value", "value to write", "VALUE");
|
||||||
|
opts.reqopt("o", "output", "output folder", "FOLDER");
|
||||||
|
|
||||||
|
// Print help and exit (see https://github.com/rust-lang/getopts/pull/109)
|
||||||
|
if args.contains(&String::from("-h")) || args.contains(&String::from("--help")) {
|
||||||
|
print!("{}", opts.usage(&format!("Usage: {} FILE [options]", &args[0])));
|
||||||
|
return ExitCode::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.contains(&String::from("-v")) || args.contains(&String::from("--version")) {
|
||||||
|
print!("{} {}", &args[0], env!("CARGO_PKG_VERSION"));
|
||||||
|
return ExitCode::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse arguments
|
||||||
|
let matches = match opts.parse(&args[1..]) {
|
||||||
|
Ok(m) => { m }
|
||||||
|
Err(f) => { panic!("{}",f.to_string()) }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue