Argust
A simple command line parser which will accept the vector of program arguments and return a sorted structure which can be used to direct execution of the program.
The command line is divided into three parts -
- Arguments - args which represent either commands or parameters for the program. e.g.
cp test.rs test2.rshere "test.rs" and "test2.rs" are the arguements. - Short Parameters - These are single character switches which can modify the behaviour of program e.g.
cp test.rs test2.rs -fhere "-f" is a switch. These can also contain values to pass to program. e.g../test -t cpp, Here cpp can be a value being passed to program. - Long Paramters - Behaves same as short parameter. Except these are typed out. Short params are generally shortcuts to long_params e.g.
cp test.rs test2.rs --buffer-size=2048here "--buffer-size=2048" updates the default copy buffer to use the value of 2048 instead.
How to use
Basic Usage
The usage is very straight forward.
let args: = args.skip.collect;
let command_set: ArgContext = parse_args;
// Use the values as required
if let Some = command_set.commands.first else
The ArgContext object contains structured information about arguments which were passed. It's defined as-
Please note that options doesn't necessarily require to be a key value pair. i.e. cp --version will return a hashmap with key 'version' and value None.
Configuring parameter style
It's even possible to override the default parse token, i.e. "-" for switch and "--" for options by using the following object and passing it along with parse_args method.
In the command testApp hello --print=stdin -f. "--" is option, "=" is option_key and "-" is switch.
The default configuration of parser is -
ParseTokens
We need to set the parameterized_switch_list field of ParserConfig in order to specify which short parameters have values associated with them.
Which means this is how parser will behave -
./test -h myval --long=MyLongVal command
This will generate - args = ["command"], short=[{"h", "myval"}], long=[{"long","MyLongVal"}]
The relative order is not important.
This is also valid input as long as parameterized_switch_list is configured - ./test -hMyval
Accepting space seperated long parameters
In order to modify the parser to accept space seperated long values e.g. ./test --long MyLongValue command we need to update the config as follows -
let mut parse_config = new;
parse_config.parse_tokens = ParseTokens ;
Please note: If we set
long_seperatoras " ", then it's important to set theparameterized_option_listofParserConfigto specify which long parameters will have values. Otherwise these values will be recieved as commands. For anylong_seperatorvalue other than " ". This field will be ignored since the value will be generated by spliting the parameter.