pub struct Cli {
pub cmd: Option<Cmd>,
pub global_raws: HashMap<String, Raw>,
pub direct_args: Vec<Raw>,
}
Expand description
Something like Cmd
.
But unfortunately, you will use it frequently.
commander_rust
will generate a instance of Application
according your code. It happens in compile-time.
commander_rust
will generate a instance of Cli
according user’s input. It happens in run-time.
What’ the difference?
Content in Application
will be replaced by something concrete through user’s input.
For example, If your code is like this:
#[option(-r, --recursive [dir...], "recursively")]
#[command(rmdir <dir> [otherDirs...], "remove files and directories")]
fn rmdir(dir: i32, other_dirs: Option<Vec<bool>>, cli: Cli) {
let r: bool = cli.get("recursive").into();
}
Let’s see. The last argument of function is Cli
type(you can miss it).
So when we want to do something if --recursive
is offered by user, how can we?
You just need to code like let r: ? = cli.get("recursive").into()
,
then you can get contents of recursive
options if user has inputted it.
That’s why Cli
will be used frequently.
Fields§
§cmd: Option<Cmd>
§global_raws: HashMap<String, Raw>
§direct_args: Vec<Raw>
Implementations§
Source§impl Cli
impl Cli
Sourcepub fn get(&self, idx: &str) -> Raw
pub fn get(&self, idx: &str) -> Raw
Get the content of Options
.
Options
has two types, one is private, the other is global. Of course they are same.
Private means they belong to the command.
Global means they belong to the global.
Private is more weight than global.
Sourcepub fn get_or<T>(&self, idx: &str, d: T) -> T
pub fn get_or<T>(&self, idx: &str, d: T) -> T
Getting contents of Options
. if idx
dont exist, return default
.
Sourcepub fn get_or_else<T, F>(&self, idx: &str, f: F) -> T
pub fn get_or_else<T, F>(&self, idx: &str, f: F) -> T
Get contents of Options
. if idx
dont exist, call f.
f should return a value of type T.