pub struct Jargon(/* private fields */);
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
11fn main() {
12 let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
13
14 if j.contains(["-h", "--help"]) { // check for help argument
15 print!("{}", HELP);
16 return;
17 }
18
19 if j.contains(["-v", "--version"]) { // check for version argument
20 println!(
21 "basename example for Jargon crate {}",
22 env!("CARGO_PKG_VERSION")
23 );
24 return;
25 }
26
27 let args = Args { // fill helper struct
28 multiple: j.contains(["-a", "--multiple"]), // multiple
29 suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
30 zero: j.contains(["-z", "--zero"]), // terminate lines with null
31 names: j.finish(), // get names
32 };
33
34 if args.names.is_empty() { // check if there are names
35 println!("Missing NAMES");
36 return;
37 }
38
39 let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
40
41 if args.multiple { // fill the rest if `-a` or `--multiple` was passed
42 args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
43 }
44
45 if args.zero { // terminate with null if `-z` or `--zero` was passed
46 v.iter().for_each(|name| print!("{}", name));
47 } else { // terminate each name with space or new line
48 v.iter().for_each(|name| println!("{} ", name));
49 }
50}
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?
11fn main() {
12 let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
13
14 if j.contains(["-h", "--help"]) { // check for help argument
15 print!("{}", HELP);
16 return;
17 }
18
19 if j.contains(["-v", "--version"]) { // check for version argument
20 println!(
21 "basename example for Jargon crate {}",
22 env!("CARGO_PKG_VERSION")
23 );
24 return;
25 }
26
27 let args = Args { // fill helper struct
28 multiple: j.contains(["-a", "--multiple"]), // multiple
29 suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
30 zero: j.contains(["-z", "--zero"]), // terminate lines with null
31 names: j.finish(), // get names
32 };
33
34 if args.names.is_empty() { // check if there are names
35 println!("Missing NAMES");
36 return;
37 }
38
39 let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
40
41 if args.multiple { // fill the rest if `-a` or `--multiple` was passed
42 args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
43 }
44
45 if args.zero { // terminate with null if `-z` or `--zero` was passed
46 v.iter().for_each(|name| print!("{}", name));
47 } else { // terminate each name with space or new line
48 v.iter().for_each(|name| println!("{} ", name));
49 }
50}
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?
3fn main() {
4 let mut j: Jargon = Jargon::from_env();
5 j.on_subcommand("go", go);
6}
7
8fn go(v: Vec<String>) {
9 println!("go!");
10 let mut j: Jargon = Jargon::from_vec(v);
11 j.on_subcommand("goo", goo)
12}
13
14fn goo(v: Vec<String>) {
15 println!("goo!");
16 let mut j: Jargon = Jargon::from_vec(v);
17 j.on_subcommand("gooo", gooo)
18}
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
11fn main() {
12 let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
13
14 if j.contains(["-h", "--help"]) { // check for help argument
15 print!("{}", HELP);
16 return;
17 }
18
19 if j.contains(["-v", "--version"]) { // check for version argument
20 println!(
21 "basename example for Jargon crate {}",
22 env!("CARGO_PKG_VERSION")
23 );
24 return;
25 }
26
27 let args = Args { // fill helper struct
28 multiple: j.contains(["-a", "--multiple"]), // multiple
29 suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
30 zero: j.contains(["-z", "--zero"]), // terminate lines with null
31 names: j.finish(), // get names
32 };
33
34 if args.names.is_empty() { // check if there are names
35 println!("Missing NAMES");
36 return;
37 }
38
39 let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
40
41 if args.multiple { // fill the rest if `-a` or `--multiple` was passed
42 args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
43 }
44
45 if args.zero { // terminate with null if `-z` or `--zero` was passed
46 v.iter().for_each(|name| print!("{}", name));
47 } else { // terminate each name with space or new line
48 v.iter().for_each(|name| println!("{} ", name));
49 }
50}
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.
Sourcepub fn finish(self) -> Vec<String>
pub fn finish(self) -> Vec<String>
Drops your jargon instance and returns all remaining arguments.
Examples found in repository?
11fn main() {
12 let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`
13
14 if j.contains(["-h", "--help"]) { // check for help argument
15 print!("{}", HELP);
16 return;
17 }
18
19 if j.contains(["-v", "--version"]) { // check for version argument
20 println!(
21 "basename example for Jargon crate {}",
22 env!("CARGO_PKG_VERSION")
23 );
24 return;
25 }
26
27 let args = Args { // fill helper struct
28 multiple: j.contains(["-a", "--multiple"]), // multiple
29 suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
30 zero: j.contains(["-z", "--zero"]), // terminate lines with null
31 names: j.finish(), // get names
32 };
33
34 if args.names.is_empty() { // check if there are names
35 println!("Missing NAMES");
36 return;
37 }
38
39 let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names
40
41 if args.multiple { // fill the rest if `-a` or `--multiple` was passed
42 args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
43 }
44
45 if args.zero { // terminate with null if `-z` or `--zero` was passed
46 v.iter().for_each(|name| print!("{}", name));
47 } else { // terminate each name with space or new line
48 v.iter().for_each(|name| println!("{} ", name));
49 }
50}