Struct jargon_args::Jargon

source ·
pub struct Jargon(_);
Expand description

Jargon

This is the main struct in this crate. This is what is used to handle arguments, and get arguments’ values.

Example

use jargon_args::Jargon;
let mut jargon: Jargon = Jargon::from_env();

if jargon.contains(["-h", "--help"]) {
    println!("help text");
    std::process::exit(0);
}

// ...

Implementations§

source§

impl Jargon

source

pub fn from_env() -> Self

Extracts a program’s arguments from the environment.

Examples found in repository?
examples/subcontinued.rs (line 4)
3
4
5
6
fn main() {
    let mut j: Jargon = Jargon::from_env();
    j.on_subcommand("go", go);
}
More examples
Hide additional examples
examples/subcommand.rs (line 4)
3
4
5
6
7
8
fn main() {
    let mut j: Jargon = Jargon::from_env();

    j.on_subcommand("list", list);
    j.on_subcommand("backwards", list_backwards);
}
examples/name.rs (line 4)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut j: Jargon = Jargon::from_env();
    let name_key: Key = "--name".into();

    match j.option_arg::<String, Key>(name_key.clone()) {
        Some(n) => println!("Your name: {}", n),
        None => eprintln!("Missing argument: {}", name_key),
    }
}
examples/basename.rs (line 12)
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));
    }
}
source

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.

Examples found in repository?
examples/subcontinued.rs (line 10)
8
9
10
11
12
13
14
15
16
17
18
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)
}
source

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?
examples/basename.rs (line 14)
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));
    }
}
source

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?
examples/subcontinued.rs (line 5)
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
Hide additional examples
examples/subcommand.rs (line 6)
3
4
5
6
7
8
fn main() {
    let mut j: Jargon = Jargon::from_env();

    j.on_subcommand("list", list);
    j.on_subcommand("backwards", list_backwards);
}
source

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 if specified key exists. Removes the program’s name from provided vector.

source

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.

source

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). None if key isn’t in arguments.

source

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?
examples/name.rs (line 7)
3
4
5
6
7
8
9
10
11
fn main() {
    let mut j: Jargon = Jargon::from_env();
    let name_key: Key = "--name".into();

    match j.option_arg::<String, Key>(name_key.clone()) {
        Some(n) => println!("Your name: {}", n),
        None => eprintln!("Missing argument: {}", name_key),
    }
}
More examples
Hide additional examples
examples/basename.rs (line 29)
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));
    }
}
source

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.

source

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.

source

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?
examples/basename.rs (line 31)
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 Clone for Jargon

source§

fn clone(&self) -> Jargon

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Jargon

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Ord for Jargon

source§

fn cmp(&self, other: &Jargon) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Jargon> for Jargon

source§

fn eq(&self, other: &Jargon) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Jargon> for Jargon

source§

fn partial_cmp(&self, other: &Jargon) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Jargon

source§

impl StructuralEq for Jargon

source§

impl StructuralPartialEq for Jargon

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.