revparse
Rust Version of the python revparse library
Installation:
#Clone the git repo
#install to /usr/local/bin
Uninstall:
Usage
First you have to create an instance of the Parser struct and provide the name of your Program, that will later be used for those cases:
your_program_name: unrecognized option '-a'
Usage: your_program_name [OPTION]...
Try 'your_program_name --help' for more information.
You can create an instance of Parser by calling the associated new() function with your programs name as an argument and assigning the returned Parser instance to a mutable variable (it has to be mutable!):
let mut parser = new;
To add arguments, you can use the .add_argument() function on parser. The function takes 4 Parameters apart from self.
The First is the long name, that has to start with "--" and is required, not optional.
The Second is an optional short name, of type Option<&str>. If it is set to None, there will be no short name for that argument, if you want a short name, like "-e" you will have to wrap it in Some() like this Some("-e"). Short names have to start with a '-' and only contain one other character.
The Third option is the help message, that will be shown behind the corresponding option, when --help is called.
The Fourth options is about wheter the argument can take values, or arguments like this:
your_program_name --option-that-takes-a-value="This is the value"
your_program_name --option-that-takes-a-value "This is the value"
your_program_name -o"This is the value"
your_program_name -o "This is the value"
If you want this to be possible, you have to provide a name for the value to be shown in the help message wrapped in a Some(). For example to add an argument "--start-process" that takes a value "PROCESS" you have to write the following:
let mut parser = new;
parser.add_argument;
You don't have to provide "PROCESS" in capital letters, since they will be capitalized automatically. This is what "PROCESS" is needed for:
Usage: your_program_name [OPTION]...
Options:
-s, --start-process=PROCESS Start some process, this is the help message
^-1 ^-2.parameter ^-4.p. ^-3.parameter
To get the value of the arguments, you can use the .get() function defined on Parser. But before you can do that, you'll have to call .run():
let mut parser = new;
parser.add_argument;
parser.run;
Then you can call the .get() function on parser and provide the long name of your argument as a function parameter, which will return an enum called ArgState with three possible variants:
True False Value(String)
True will be returned, if the argument doesn't require a value to be inserted into it, as with --start-process="Value" and was called. False will be returned, if the argument wasn't called, no matter wheter a value is needed or not. Value(String) will be returned, if the argument needs a value, and was called with one. You are given ownership of the returned String.
You can best handle ArgState with a match expression like this:
let mut parser = new;
parser.add_argument;
parser.run;
let result: ArgState = parser.get; // if there is a typo in --start-process, the program will panic at runtime
match result
Here's an example Program:
use ;