shift 0.3.0

A command-line argument parser
Documentation
  • Coverage
  • 100%
    13 out of 13 items documented1 out of 11 items with examples
  • Size
  • Source code size: 16.44 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.95 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • eze-works

shift

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");
}