Crate shift

Source
Expand description

Parsing command line arguments

Call parse to get a Bag from which options, flags and operands may be extracted.

§Supported syntax

GNU-style command line invocations like the following are supported:

exe commit --verbose --level=info --message message
exe -v -l=info -m message

Anything prefixed with a - or -- is treated as an option or flag. The following command lines are equivalent:

exe -one -two -3
exe --one --two --3

After parsing this, you’ll be able to extract three flags: one, two and 3.

The end of options marker -- is respected. Any arguments that come after it are treated as operands (i.e. positional arguments)

While the order the command line arguments appear in is irrelevant, the order in which you extract them is. In particular, when a switch is followed by a value, it is ambiguous whether it should be treated as an option or a flag. The order in which methods are called resolves this ambiguity:

// Treated as a flag followed by a positional argument
let mut bag = shift::parse(vec![
    String::from("program"),
    String::from("--option"),
    String::from("value")
]).unwrap();
assert_eq!(bag.shift_flag("option"), true);
assert_eq!(bag.shift_operand().as_deref(), Some("value"));
assert!(bag.is_empty());

// Treated as an option
let mut bag = shift::parse(vec![
    String::from("program"),
    String::from("--option"),
    String::from("value")
]).unwrap();
assert_eq!(bag.shift_option("option").as_deref(), Some("value"));
assert!(bag.is_empty());

Structs§

Bag
Parsed command line arguments

Enums§

ParseError
A command line parsing error

Functions§

parse
Parses the given command line arguments into a bag