revparse
Usage
First you have to create a mutable variable, we'll call it parser
let mut parser = new; // for grep "executable_name" would be "grep"
There are two types of arguments the user can give
One that takes a value, and one that doesn't:
grep --version // takes no value
grep --file=FILE // takes a value
In this example we will add an argument of the first type, that takes no value
let mut parser = new;
parser.add_argument;
How the argument will be shown in the help message:
-V, --version display version information and exit
In this example we will add an argument of the second type, that takes a value
let mut parser = new;
parser.add_argument;
How the argument will be shown in the help message:
-f, --file=FILE take PATTERNS from FILE
Find out if the user gave us an Option of the first type (That takes no value), or not
(Using the 'get_noval' function)
let mut parser = new;
parser.add_argument;
parser.run; // Will parse the arguments the user entered
let version: bool = parser.get_noval;
assert_eq!; // Since this is a doc-test, the flag will not have been given
Get the value the user entered for the second type (That takes a value)
(Using the 'get_val' function)
let mut parser = new;
parser.add_argument;
parser.run;
let file: = parser.get_val; // DIFFERENT FUNCTION THAN ABOVE !!!
// The 'file' variable will be None, if the user didn't enter this flag, and Some(String) if he did
assert_eq!; // Since this is a doc-test, the flag will not have been given
Examples
Since we want to simulate the user giving us flags (often called Options), we will use the run_custom_args() function instead of run()
let mut parser = new;
parser.add_argument;
parser.run_custom_args; // --test will work just the same
let test: = parser.get_val;
assert_eq!;
let mut parser = new;
parser.add_argument;
// This time the user doesn't give us an argument
parser.run_custom_args;
let test: = parser.get_val;
assert_eq!; // Which is why the 'test' variable is None, and not Some(String)
let mut parser = new;
parser.add_argument;
parser.run_custom_args; // again, --test will work the same
let test: bool = parser.get_noval; // you can't use "-t" for this function
assert_eq!;
let mut parser = new;
parser.add_argument;
parser.run_custom_args; // this time the user gave us no arguments
let test: bool = parser.get_noval;
assert_eq!; // which is why the 'test' variable is false
Positional Arguments
revparse supports the use of positional arguments, which are values passed without flags.
Like this:
program some_value
Instead of
program --value=some_value
Usage
let mut parser = new;
parser.add_pos_arg;
Help message
With a positional argument:
Usage: executable_name [OPTION]... ARGUMENT
Options:
-h, --help display this help text and exit
Without a positional argument:
Usage: executable_name [OPTION]...
Options:
-h, --help display this help text and exit
As you can see, the ARGUMENT after [OPTION]...
vanished
Here's how to force the user to enter at least 1 out of 2 positional arguments
let mut parser = new;
parser.add_pos_arg; // This argument will be forced
parser.add_pos_arg; // This argument won't be forced
// Since we want the user to enter the first one, but don't care
// about whether he enters a second positional argument, we will pass 1 to this function
parser.min_pos_args;
// If you were to give 2 to that function, the user would have to enter 2 positional arguments
Getting the value of positional arguments
// Example from above
let mut parser = new;
parser.add_pos_arg;
parser.add_pos_arg;
parser.min_pos_args;
parser.run_custom_args;
// The second positional argument starts with --- to demonstrate, that the user will have to type -- before such an argument
let pos_args: = parser.get_pos_args;
// parser.get_pos_args() returns a Vector with ALL positional arguments given
assert_eq!; // The user entered both positional arguments, so the length is 2
assert_eq!;
assert_eq!;
Help message for positional arguments
Sometimes positional arguments need an explanation, like with grep:
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.
To implement the above help message with this library you can use the 'pos_arg_help' function:
let mut parser = new;
parser.add_pos_arg;
parser.add_pos_arg;
parser.min_pos_args; // Force the user to enter a PATTERN
parser.pos_arg_help;
parser.run;