Expand description
lapp
provides a straightforward way to parse command-line
arguments, using the usage text as a pattern.
Example
extern crate lapp;
let args = lapp::parse_args("
A test program
-v,--verbose verbose output
-k (default 10)
-s, --save (default 'out.txt')
<out> (default 'stdout')
");
assert_eq!(args.get_bool("verbose"),false);
assert_eq!(args.get_integer("k"),10);
assert_eq!(args.get_string("save"),"out.txt");
assert_eq!(args.get_string("out"),"stdout");
The usage text or specification follows these simple rules:
line begining with one of ‘-short, –long’, ‘–long’ or ‘-short’ (flags)
or begining with false
. This specifier can be a type (like ‘(integer)’) or a default,
like ’(default 10)`. If there’s a default, the type is infered from the value - can always
use single quotes to insist that the flag value is a string. Otherwise this flag is
required and must be present! You can also use a type with default, e.g. “(path default ~/.boo).
The currently supported types are ‘string’,‘integer’,‘bool’,‘float’,‘infile’,‘outfile’ and ‘path’.
There are corresponding access methods like get_string("flag")
and so forth.
Access methods like get_string_result("flag")
will not exit the program on error
and instead return an error.
The flag may be followed by ‘…’ (e.g ‘-I… (
Rest of line (or any other kind of line) is ignored.
lapp scans command-line arguments using GNU-style short and long flags. Short flags may be combined, and may immediately followed by a value, e.g ‘-vk5’. As an extension, you can say ‘–flag=value’ or ‘-f:value’.