shift
Unobtrusive library for processing command line arguments.
Unobtrusive library for processing command line arguments.
pub fn main() {
// Assume this is the result of std::env::args()
let cmdline = ["git", "--help", "commit", "--message", "hello"];
let mut args = shift::parse(
cmdline.into_iter().map(|s| s.to_string()).collect()
).unwrap();
// Detect help flags
if args.shift_flag("help") || args.shift_flag("h") {
// Show help
}
// Detect positional arguments with known values (aka "commands")
if args.shift_operand_with_value("commit") {
// Do the commit
}
// Get the value of an option
let Some(value) = args.shift_option("m").or_else(|| args.shift_option("message")) else {
panic!();
};
assert_eq!(value, "hello");
}