Crate lapp

source ·
Expand description

lapp provides a straightforward way to parse command-line arguments, using the usage text as a pattern.


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')

The usage text or specification follows these simple rules: line begining with one of ‘-short, –long’, ‘–long’ or ‘-short’ (flags) or begining with (positional arguments). These may be followed by a type/default specifier () - otherwise considered a bool flag with default 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… ()’) and it is then a multiple flag; its value will be a vector. This vector may be empty (flag is not required). If the ‘…’ appears inside the type specifier (e.g. ‘-p (integer…)’) then the flag is expecting several space-separated values (like -p ‘10 20 30’); it is also represented by a vector.

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’.



parse the command-line specification and use it to parse the program’s command line args. As before, quits on any error.

Type Definitions