pub struct Clappers { /* private fields */ }Implementations§
Source§impl Clappers
impl Clappers
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a Clappers parser
§Parameters
None.
§Return value
An empty Clappers parser, which is ready to be configured by
chaining:
set_flags()set_singles()set_multiples()
Once configured, build() is chained last to build the actual
command line arguments parser
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_flags(vec!["h|help", "v|verbose"])
.set_singles(vec!["o|output", "u|username"])
.set_multiples(vec!["i|input", "host"])
.build();
// ...
}Sourcepub fn set_flags(self, arg_specs: Vec<&str>) -> Self
pub fn set_flags(self, arg_specs: Vec<&str>) -> Self
Add flag argument parsing to the Clappers config
Flag arguments are true if they were supplied on the command
line, and false otherwise e.g:
-h
--help
-v
--verboseNote: flag arguments do not take values
§Parameters
arg_specs specifies which flag arguments on the command line
to care about.
Each arg_spec contains “|” separated flag argument alias
names e.g:
clappers.set_flags(vec!["h|help", "v|verbose"]);§Return value
The Clappers parser so that it can be chained
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_flags(vec!["h|help", "v|verbose"])
.set_singles(vec!["o|output", "u|username"])
.set_multiples(vec!["i|input", "host"])
.build();
// ...
}Sourcepub fn set_singles(self, arg_specs: Vec<&str>) -> Self
pub fn set_singles(self, arg_specs: Vec<&str>) -> Self
Add single value argument parsing to the Clappers config
Single value arguments contain a single String value if they
were supplied on the command line, and empty String
otherwise e.g:
-o filename.txt
--output filename.txt
-u Zelensky
--username Zelensky§Parameters
arg_specs specifies which single value arguments on the
command line to care about.
Each arg_spec contains “|” separated single value argument
alias names e.g:
clappers.set_singles(vec!["o|output", "u|username"]);§Return value
The Clappers parser so that it can be chained
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_flags(vec!["h|help", "v|verbose"])
.set_singles(vec!["o|output", "u|username"])
.set_multiples(vec!["i|input", "host"])
.build();
// ...
}Sourcepub fn set_multiples(self, arg_specs: Vec<&str>) -> Self
pub fn set_multiples(self, arg_specs: Vec<&str>) -> Self
Add multiple value argument parsing to the Clappers config
Multiple value arguments contain at least a singly populated
Vec<String> value if they were supplied on the command line,
and empty Vec<String> otherwise e.g:
-i file1.txt
--input file1.txt
--host host1They can also contain multiple values, by repetition on the command line e.g:
-i file1.txt -i file2.txt ... -i fileN.txt
--host host1 --host host2 ... --host hostNThe following format also works, reading from the first value until either the next argument is reached, or until the end of the entire command line arguments e.g:
-i file1.txt file2.txt ... fileN.txt -n next_argument
--host host1 host2 hostN§Parameters
arg_specs specifies which multiple value arguments on the
command line to care about.
Each arg_spec contains “|” separated multiple value argument
alias names e.g:
clappers.set_multiples(vec!["i|input", "host"]);§Return value
The Clappers parser so that it can be chained
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_flags(vec!["h|help", "v|verbose"])
.set_singles(vec!["o|output", "u|username"])
.set_multiples(vec!["i|input", "host"])
.build();
// ...
}Sourcepub fn build(self) -> Self
pub fn build(self) -> Self
Build the command line arguments parser with the current Clappers config
§Parameters
None
§Return value
The Clappers parser containing the parsed command line
arguments values, accessed with:
get_flags()get_singles()get_multiples()get_leftovers()
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_flags(vec!["h|help", "v|verbose"])
.set_singles(vec!["o|output", "u|username"])
.set_multiples(vec!["i|input", "host"])
.build();
if clappers.get_flag("help") {
// Show help text
}
// ...
}Sourcepub fn get_flag(&self, argument: &str) -> bool
pub fn get_flag(&self, argument: &str) -> bool
Check if the flag was supplied on the command line for the specified argument
§Parameters
argument is any alias of the specified argument
§Return value
true if the flag was supplied on the command line, and
false otherwise
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_flags(vec!["h|help"])
.build();
if clappers.get_flag("help") {
// Show help text
}
if clappers.get_flag("h") {
// This will also show the help text
}
// ...
}Sourcepub fn get_single(&self, argument: &str) -> String
pub fn get_single(&self, argument: &str) -> String
Get the single value supplied on the command line for the specified argument
§Parameters
argument is any alias of the specified argument
§Return value
The single String value if they were supplied on the command
line, and empty String otherwise
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_singles(vec!["output"])
.build();
println!("Output filename is {}", clappers.get_single("output"));
// ...
}Sourcepub fn get_multiple(&self, argument: &str) -> Vec<String>
pub fn get_multiple(&self, argument: &str) -> Vec<String>
Get multiple values supplied on the command line for the specified argument
§Parameters
argument is any alias of the specified argument
§Return value
Multiple String values if they were supplied on the command
line, and empty Vec<String> otherwise
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.set_multiples(vec!["input"])
.build();
println!("Input filenames are {:#?}", clappers.get_multiple("input"));
// ...
}Sourcepub fn get_leftovers(&self) -> Vec<String>
pub fn get_leftovers(&self) -> Vec<String>
Get all values supplied on the command line that are not associated with any argument
§Parameters
None
§Return value
All String values supplied on the command line that are not
associated with any argument, and empty Vec<String>
otherwise
§Example
use clappers::Clappers;
fn main() {
let clappers = Clappers::new()
.build();
println!("`ls *` returned the following filenames: {:#?}", clappers.get_leftovers());
// ...
}