pub struct ArgParser {
pub args: Vec<String>,
/* private fields */
}
Expand description
Our homebrewed Arg Parser
Fields§
§args: Vec<String>
Implementations§
Source§impl ArgParser
impl ArgParser
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new ArgParser object
The capacity specifies the initial capacity of the number parameters. Always good to set it at the number of flags and opts total. (Used for initial hashmap allocation size)
Sourcepub fn add_flag(self, flags: &[&str]) -> Self
pub fn add_flag(self, flags: &[&str]) -> Self
Builder method for adding both short and long flags
Flags are just parameters that have no assigned values. They are used for when certain features or options have been enabled for the application
§Examples
> ls -l --human-readable
^ ^ ^
| | |
| | `-- A long flag to enable human readable numbers.
| `-- A short flag to enable the long format.
`-- The command to list files.
Sourcepub fn add_opt(self, short: &str, long: &str) -> Self
pub fn add_opt(self, short: &str, long: &str) -> Self
Builder method for adding both short and long opts
Opts are parameters that hold assigned values. They are used for when certain features or options have been enabled for the application
§Examples
> ls -T 4 --color=always
^ ^ ^
| | |
| | `-- A long opt to enable the use of color with value `always`.
| `-- A short opt to set tab size to the value `4`.
`-- The command to list files.
Sourcepub fn add_opt_default(self, short: &str, long: &str, default: &str) -> Self
pub fn add_opt_default(self, short: &str, long: &str, default: &str) -> Self
Add an opt with a default value. Works the same way as add_opt.
Sourcepub fn add_setting(self, setting: &str) -> Self
pub fn add_setting(self, setting: &str) -> Self
Builder method for adding settings
Settings are parameters that hold assigned values. They are used in some applications such as dd
§Examples
> dd if=/path/file
^ ^
| |
| |
| `-- The setting set to /path/file
`-- The command to list files.
Sourcepub fn add_setting_default(self, setting: &str, default: &str) -> Self
pub fn add_setting_default(self, setting: &str, default: &str) -> Self
Add a setting with a default value. Works the same way as add_setting
Sourcepub fn parse<A: Iterator<Item = String>>(&mut self, args: A)
pub fn parse<A: Iterator<Item = String>>(&mut self, args: A)
Start parsing user inputted args for which flags and opts are used at
runtime. The rest of the args that are not associated to opts get added
to ArgParser.args
.
Sourcepub fn count<P: Hash + Eq + ?Sized>(&self, name: &P) -> usize
pub fn count<P: Hash + Eq + ?Sized>(&self, name: &P) -> usize
Get the number of times a flag or opt has been found after parsing.
Sourcepub fn found<P: Hash + Eq + ?Sized>(&self, name: &P) -> bool
pub fn found<P: Hash + Eq + ?Sized>(&self, name: &P) -> bool
Check if a flag or opt has been found after initialization. While the short form of the opt or flag will be recognized, the long form should be used to enhance code readability.
§Examples
if parser.found("my-long-flag") {
//Do things...
}
Sourcepub fn flag<F: Hash + Eq + ?Sized>(&mut self, flag: &F) -> RefMut<'_, bool>
pub fn flag<F: Hash + Eq + ?Sized>(&mut self, flag: &F) -> RefMut<'_, bool>
Modify the state of a flag. Use true
if the flag is to be enabled. Use false
to
disable its use.
Sourcepub fn opt<O: Hash + Eq + ?Sized>(&mut self, opt: &O) -> RefMut<'_, String>
pub fn opt<O: Hash + Eq + ?Sized>(&mut self, opt: &O) -> RefMut<'_, String>
Modify the state value of an opt. Use Some(String)
to set if the opt is to be enabled and
has been assigned a value from String
. Use None
to disable the opt’s use.
Sourcepub fn get_opt<O: Hash + Eq + ?Sized>(&self, opt: &O) -> Option<String>
pub fn get_opt<O: Hash + Eq + ?Sized>(&self, opt: &O) -> Option<String>
Get the value of an Opt. If it has been set or defaulted, it will return a Some(String)
value otherwise it will return None.
Sourcepub fn get_setting<O: Hash + Eq + ?Sized>(&self, setting: &O) -> Option<String>
pub fn get_setting<O: Hash + Eq + ?Sized>(&self, setting: &O) -> Option<String>
Get the value of an Setting. If it has been set or defaulted, it will return a Some(String)
value otherwise it will return None.