1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#![allow(dead_code)]
use std::ffi::OsString;
#[derive(Debug)]
struct Args {
forwarded_args: Vec<OsString>,
help: bool,
}
fn parse_args() -> Result<Args, pico_args::Error> {
// `from_vec` takes `OsString`, not `String`.
let mut args: Vec<_> = std::env::args_os().collect();
args.remove(0); // remove the executable path.
// Find and process `--`.
let forwarded_args = if let Some(dash_dash) = args.iter().position(|arg| arg == "--") {
// Store all arguments following ...
let later_args = args.drain(dash_dash+1..).collect();
// .. then remove the `--`
args.pop();
later_args
} else {
Vec::new()
};
// Now pass the remaining arguments through to `pico_args`.
let mut args = pico_args::Arguments::from_vec(args);
let res = Args {
forwarded_args,
help: args.contains(["-h", "--help"]),
};
// It's up to the caller what to do with the remaining arguments.
let remaining = args.finish();
if !remaining.is_empty() {
eprintln!("Warning: unused arguments left: {:?}", remaining);
}
Ok(res)
}
fn main() {
match parse_args() {
Ok(args) => println!("{:#?}", args),
Err(err) => eprintln!("{}", err),
}
}
|