Crates.io | libcli |
lib.rs | libcli |
version | 0.3.9 |
source | src |
created_at | 2020-06-25 18:40:04.182534 |
updated_at | 2020-08-11 13:06:35.687596 |
description | A collection of utilities to help making command line based programs |
homepage | |
repository | |
max_upload_size | |
id | 257981 |
size | 25,987 |
A library for Rust containing utilities to help develop command line based programs
More features are coming
The module config
contains the ability to parse command line arguments
Allows you to supply an OptionSpec list that specifies
Parsing of arguments either from std::env::args or custom list
After creating an OptionSpec list, pass them to either Config::new or Config::new_env
let specs = [
args::OptionSpec::new(
'\0',
"(unnamed)",
"Input files",
true,
args::OptionPolicy::AtLeast(2), // 1st value is program name
),
args::OptionSpec::new(
'o',
"output",
"Searches recursive",
false,
args::OptionPolicy::Exact(1),
),
args::OptionSpec::new(
'v',
"verbose",
"Shows verbose output",
false,
args::OptionPolicy::Exact(0),
),
];
let config = args::Config::new_env(&specs).unwrap_or_else(|err| {
println!("{}", err);
std::process::exit(1);
});
// Check if verbose was specified, either as --verbose or -v
let verbose: bool = match config.option("verbose") {
Some(_) => true,
None => false,
};
// Should always return Some since option was required, new_env should have failed if not included
let files = match config.option("(unnamed)") {
Some(v) => v,
None => panic!("Didn't get input files"),
};
if verbose {
println!("Reading files {:?}...", files);
}
// logic
The OptionPolicy provides a variant call Final which will collect all remaining arguments to the values of the option, regardless if there are more options
This can be useful if you want the user to enter a command as a last argument and not have the argument to that command affect yours, e.g;
myprogram -vo output.txt --exec grep search -r .
If exec
is policy Final
, -r option won't be parsed as another argument but rather become a value of exec
Detecting help option without failing on required option
The FinalIgnore
OptionPolicy does the same as Finalize but will not Err on missing required options, this is useful for overriding options like help
or version
This enables us to provide the help option without failing to parse due to missing required option
args::Config::generate_usage(&specs, list_required, list_unrequired)
generates a usage string which can be printed
// Add this OptionSpec with the others with FinalizeIgnore policy
args::OptionSpec::new(
'h',
"help",
"Display a help screen",
false,
args::OptionPolicy::FinalizeIgnore(),
),
Parse as normal and check if help was present Note: When using FinalizeIgnore, required options may return none, which they don't usually because parse return Err
let config = args::Config::new_env(&specs).unwrap_or_else(|err| {
println!("{}", err);
std::process::exit(1);
});
if let Some(_) = config.option("help") {
println!(
"Myprogram\n{}",
args::Config::generate_usage(&specs, true, true)
);
return;
}
Using this style rather than an auto help feature when parsing is that you can add extra information the argument parses doesn't know or print to something else than console if for example in GUI application
This can also be used for version or similar