Struct jargon_args::Jargon
source · pub struct Jargon(_);Expand description
Implementations§
source§impl Jargon
impl Jargon
sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Extracts a program’s arguments from the environment.
Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
if j.contains(["-h", "--help"]) { // check for help argument
print!("{}", HELP);
return;
}
if j.contains(["-v", "--version"]) { // check for version argument
println!(
"basename example for Jargon crate {}",
env!("CARGO_PKG_VERSION")
);
return;
}
let args = Args { // fill helper struct
multiple: j.contains(["-a", "--multiple"]), // multiple
suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
zero: j.contains(["-z", "--zero"]), // terminate lines with null
names: j.finish(), // get names
};
if args.names.is_empty() { // check if there are names
println!("Missing NAMES");
return;
}
let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
if args.multiple { // fill the rest if `-a` or `--multiple` was passed
args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
}
if args.zero { // terminate with null if `-z` or `--zero` was passed
v.iter().for_each(|name| print!("{}", name));
} else { // terminate each name with space or new line
v.iter().for_each(|name| println!("{} ", name));
}
}sourcepub fn from_vec<T: ToString>(v: Vec<T>) -> Self
pub fn from_vec<T: ToString>(v: Vec<T>) -> Self
Places provided vector into Jargon. Please have the program’s name or subcommand’s name at
index 0. 0 is always ignored.
sourcepub fn contains<K: Into<Key>>(&mut self, key: K) -> bool
pub fn contains<K: Into<Key>>(&mut self, key: K) -> bool
Checks if provided key is given in arguments. Removes it.
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
if j.contains(["-h", "--help"]) { // check for help argument
print!("{}", HELP);
return;
}
if j.contains(["-v", "--version"]) { // check for version argument
println!(
"basename example for Jargon crate {}",
env!("CARGO_PKG_VERSION")
);
return;
}
let args = Args { // fill helper struct
multiple: j.contains(["-a", "--multiple"]), // multiple
suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
zero: j.contains(["-z", "--zero"]), // terminate lines with null
names: j.finish(), // get names
};
if args.names.is_empty() { // check if there are names
println!("Missing NAMES");
return;
}
let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
if args.multiple { // fill the rest if `-a` or `--multiple` was passed
args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
}
if args.zero { // terminate with null if `-z` or `--zero` was passed
v.iter().for_each(|name| print!("{}", name));
} else { // terminate each name with space or new line
v.iter().for_each(|name| println!("{} ", name));
}
}sourcepub fn on_subcommand<K: Into<Key>, F: FnMut(Vec<String>)>(
&mut self,
key: K,
f: F
)
pub fn on_subcommand<K: Into<Key>, F: FnMut(Vec<String>)>( &mut self, key: K, f: F )
Runs function that does not return a value if specified key exists. Removes the program’s name from provided vector.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
let mut j: Jargon = Jargon::from_env();
j.on_subcommand("go", go);
}
fn go(v: Vec<String>) {
println!("go!");
let mut j: Jargon = Jargon::from_vec(v);
j.on_subcommand("goo", goo)
}
fn goo(v: Vec<String>) {
println!("goo!");
let mut j: Jargon = Jargon::from_vec(v);
j.on_subcommand("gooo", gooo)
}More examples
sourcepub fn opt_on_subcommand<K: Into<Key>, F: FnMut(Vec<String>) -> Option<T>, T>(
&mut self,
key: K,
f: F
) -> Option<T>
pub fn opt_on_subcommand<K: Into<Key>, F: FnMut(Vec<String>) -> Option<T>, T>( &mut self, key: K, f: F ) -> Option<T>
Runs function that returns Option
sourcepub fn res_on_subcommand<K: Into<Key>, F: FnMut(Vec<String>) -> Result<T, Error>, T>(
&mut self,
key: K,
f: F
) -> Result<T, Error>
pub fn res_on_subcommand<K: Into<Key>, F: FnMut(Vec<String>) -> Result<T, Error>, T>( &mut self, key: K, f: F ) -> Result<T, Error>
Runs function that returns Result<T, jargon_args::Error> if specified key exists. Removes the program’s name from provided vector.
sourcepub fn subcommand<K: Into<Key>>(&mut self, key: K) -> Option<Vec<String>>
pub fn subcommand<K: Into<Key>>(&mut self, key: K) -> Option<Vec<String>>
Checks if key exists, removes it, and returns it and all remaining arguments in
Some(Vec
sourcepub fn option_arg<T: FromStr, K: Into<Key>>(&mut self, key: K) -> Option<T>
pub fn option_arg<T: FromStr, K: Into<Key>>(&mut self, key: K) -> Option<T>
Checks for provided key in arguments, removes it, returns Some(String) with the value after it if there is one. None is there is no value.
Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
if j.contains(["-h", "--help"]) { // check for help argument
print!("{}", HELP);
return;
}
if j.contains(["-v", "--version"]) { // check for version argument
println!(
"basename example for Jargon crate {}",
env!("CARGO_PKG_VERSION")
);
return;
}
let args = Args { // fill helper struct
multiple: j.contains(["-a", "--multiple"]), // multiple
suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
zero: j.contains(["-z", "--zero"]), // terminate lines with null
names: j.finish(), // get names
};
if args.names.is_empty() { // check if there are names
println!("Missing NAMES");
return;
}
let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
if args.multiple { // fill the rest if `-a` or `--multiple` was passed
args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
}
if args.zero { // terminate with null if `-z` or `--zero` was passed
v.iter().for_each(|name| print!("{}", name));
} else { // terminate each name with space or new line
v.iter().for_each(|name| println!("{} ", name));
}
}sourcepub fn result_arg<T: FromStr, K: Into<Key>>(
&mut self,
key: K
) -> Result<T, Error>
pub fn result_arg<T: FromStr, K: Into<Key>>( &mut self, key: K ) -> Result<T, Error>
Checks for provided key in arguments, removes it, returns Ok(String) with the value after it if there is one. Err(jargon_args::Error) is there is no value.
sourcepub fn opt_next<T: FromStr>(&mut self) -> Option<T>
pub fn opt_next<T: FromStr>(&mut self) -> Option<T>
Returns the next argument as Some(T) or ‘None’ if there is no more arguments or another argument key.
sourcepub fn finish(self) -> Vec<String>
pub fn finish(self) -> Vec<String>
Returns the next argument as Ok(T) or ‘Err()’ if there is no more arguments or another argument key.
Drops your jargon instance and returns all remaining arguments.
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
if j.contains(["-h", "--help"]) { // check for help argument
print!("{}", HELP);
return;
}
if j.contains(["-v", "--version"]) { // check for version argument
println!(
"basename example for Jargon crate {}",
env!("CARGO_PKG_VERSION")
);
return;
}
let args = Args { // fill helper struct
multiple: j.contains(["-a", "--multiple"]), // multiple
suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
zero: j.contains(["-z", "--zero"]), // terminate lines with null
names: j.finish(), // get names
};
if args.names.is_empty() { // check if there are names
println!("Missing NAMES");
return;
}
let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
if args.multiple { // fill the rest if `-a` or `--multiple` was passed
args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
}
if args.zero { // terminate with null if `-z` or `--zero` was passed
v.iter().for_each(|name| print!("{}", name));
} else { // terminate each name with space or new line
v.iter().for_each(|name| println!("{} ", name));
}
}Trait Implementations§
source§impl Ord for Jargon
impl Ord for Jargon
source§impl PartialEq<Jargon> for Jargon
impl PartialEq<Jargon> for Jargon
source§impl PartialOrd<Jargon> for Jargon
impl PartialOrd<Jargon> for Jargon
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more