[−][src]Crate pargs
pargs - command line argument parser
the design goal of pargs is to simply return parsed arguments to a caller in a defined format for ease of lookup.
pargs works with three common types of arguments: commands, flags and options.
Using pargs
using pargs is very simple:
define all three types of arguments that your program needs
and pass them as individual Vec<String> to pargs::parse().
parse() will return a Matches struct of the parsed arguments
keyed by category so that your application can easily
interpret them.
Definitions
command_argsare defined as single arguments that do not have an assigned valuecommand_argsargs should be entered without a dashflag_argsare intended to be boolean valuesflag_argsshould not be assigned a value - if they exist, they are interpreted astrueoption_argsshould be assigned a valueoption_argsshould be denoted with a-characteroption_argscan be assigned a value via=or space between arg and value
Example
The following example shows a simple program that defines all three types of arguments
(commands, flag and option). Pargs is passed a Vec<String> from env::args()
at which point it parses the arguments and returns them to the program in a simple data structure.
use std::env; fn main() { let actual_args: Vec<String> = env::args().collect(); let command_args = vec![String::from("cool_command")]; let flag_args = vec![String::from("-h")]; let option_args = vec![String::from("-j"), String::from("-i")]; let parsed_args = pargs::parse(actual_args, command_args, flag_args, option_args); match parsed_args { Ok(parsed_args) => println!("{:?}", parsed_args), Err(parsed_args) => println!("{}", parsed_args), } }
If we run this program with cargo run cool_command -h -j=test123 -i=test456,
the output will be {"flag_args": ["-h"], "command_args": ["cool_command"], "option_args": ["-j", "test123", "-i", "test456"]}.
From here, we can lookup the values and utilize them in our program.
Structs
| Matches | maintains args that are successfully parsed and returns them to the calling program |
Functions
| parse | parses arguments in relation to expected optional and required arguments |