Struct Ref

Source
pub struct Ref<'parser: 'refer, 'refer, T: 'parser> { /* private fields */ }

Implementations§

Source§

impl<'parser, 'refer, T> Ref<'parser, 'refer, T>

Source

pub fn add_option<'x, A: TypedAction<T>>( &'x mut self, names: &[&'parser str], action: A, help: &'parser str, ) -> &'x mut Ref<'parser, 'refer, T>

Examples found in repository?
examples/subcommands.rs (lines 33-34)
27fn play_command(verbose: bool, args: Vec<String>) {
28    let mut output = "".to_string();
29    {
30        let mut ap = ArgumentParser::new();
31        ap.set_description("Plays a sound");
32        ap.refer(&mut output)
33            .add_option(&["--output"], Store,
34                r#"Output sink to play to"#);
35        match ap.parse(args, &mut stdout(), &mut stderr()) {
36            Ok(()) =>  {}
37            Err(x) => {
38                std::process::exit(x);
39            }
40        }
41    }
42    println!("Verbosity: {}, Output: {}", verbose, output);
43}
44
45fn record_command(verbose: bool, args: Vec<String>) {
46    let mut input = "".to_string();
47    {
48        let mut ap = ArgumentParser::new();
49        ap.set_description("Records a sound");
50        ap.refer(&mut input)
51            .add_option(&["--input"], Store,
52                r#"Output source to record from"#);
53        match ap.parse(args, &mut stdout(), &mut stderr()) {
54            Ok(()) =>  {}
55            Err(x) => {
56                std::process::exit(x);
57            }
58        }
59    }
60    println!("Verbosity: {}, Input: {}", verbose, input);
61}
62
63fn main() {
64    let mut verbose = false;
65    let mut subcommand = Command::play;
66    let mut args = vec!();
67    {
68        let mut ap = ArgumentParser::new();
69        ap.set_description("Plays or records sound");
70        ap.refer(&mut verbose)
71            .add_option(&["-v", "--verbose"], StoreTrue,
72            "Be verbose");
73        ap.refer(&mut subcommand).required()
74            .add_argument("command", Store,
75                r#"Command to run (either "play" or "record")"#);
76        ap.refer(&mut args)
77            .add_argument("arguments", List,
78                r#"Arguments for command"#);
79        ap.stop_on_first_argument(true);
80        ap.parse_args_or_exit();
81    }
82
83    args.insert(0, format!("subcommand {:?}", subcommand));
84    match subcommand {
85        Command::play => play_command(verbose, args),
86        Command::record => record_command(verbose, args),
87    }
88}
More examples
Hide additional examples
examples/greeting.rs (lines 14-15)
5fn main() {
6    let mut verbose = false;
7    let mut name = "World".to_string();
8    {
9        let mut ap = ArgumentParser::new();
10        ap.set_description("Greet somebody.");
11        ap.add_option(&["-V", "--version"],
12            Print(env!("CARGO_PKG_VERSION").to_string()), "Show version");
13        ap.refer(&mut verbose)
14            .add_option(&["-v", "--verbose"], StoreTrue,
15            "Be verbose");
16        ap.refer(&mut name)
17            .add_option(&["--name"], Store,
18            "Name for the greeting");
19        ap.parse_args_or_exit();
20    }
21
22    if verbose {
23        println!("name is {}", name);
24    }
25    println!("Hello {}!", name);
26}
examples/structure.rs (lines 21-22)
12fn main() {
13    let mut options = Options {
14        verbose: false,
15        name: "World".to_string(),
16    };
17    {
18        let mut ap = ArgumentParser::new();
19        ap.set_description("Greet somebody.");
20        ap.refer(&mut options.verbose)
21            .add_option(&["-v", "--verbose"], StoreTrue,
22            "Be verbose");
23        ap.refer(&mut options.name)
24            .add_option(&["--name"], Store,
25            "Name for the greeting");
26        match ap.parse_args() {
27            Ok(()) => {}
28            Err(x) => {
29                exit(x);
30            }
31        }
32    }
33
34    if options.verbose {
35        println!("name is {}", options.name);
36    }
37    println!("Hello {}!", options.name);
38}
Source

pub fn add_argument<'x, A: TypedAction<T>>( &'x mut self, name: &'parser str, action: A, help: &'parser str, ) -> &'x mut Ref<'parser, 'refer, T>

Examples found in repository?
examples/subcommands.rs (lines 74-75)
63fn main() {
64    let mut verbose = false;
65    let mut subcommand = Command::play;
66    let mut args = vec!();
67    {
68        let mut ap = ArgumentParser::new();
69        ap.set_description("Plays or records sound");
70        ap.refer(&mut verbose)
71            .add_option(&["-v", "--verbose"], StoreTrue,
72            "Be verbose");
73        ap.refer(&mut subcommand).required()
74            .add_argument("command", Store,
75                r#"Command to run (either "play" or "record")"#);
76        ap.refer(&mut args)
77            .add_argument("arguments", List,
78                r#"Arguments for command"#);
79        ap.stop_on_first_argument(true);
80        ap.parse_args_or_exit();
81    }
82
83    args.insert(0, format!("subcommand {:?}", subcommand));
84    match subcommand {
85        Command::play => play_command(verbose, args),
86        Command::record => record_command(verbose, args),
87    }
88}
Source

pub fn metavar<'x>(&'x mut self, name: &str) -> &'x mut Ref<'parser, 'refer, T>

Source

pub fn required<'x>(&'x mut self) -> &'x mut Ref<'parser, 'refer, T>

Examples found in repository?
examples/subcommands.rs (line 73)
63fn main() {
64    let mut verbose = false;
65    let mut subcommand = Command::play;
66    let mut args = vec!();
67    {
68        let mut ap = ArgumentParser::new();
69        ap.set_description("Plays or records sound");
70        ap.refer(&mut verbose)
71            .add_option(&["-v", "--verbose"], StoreTrue,
72            "Be verbose");
73        ap.refer(&mut subcommand).required()
74            .add_argument("command", Store,
75                r#"Command to run (either "play" or "record")"#);
76        ap.refer(&mut args)
77            .add_argument("arguments", List,
78                r#"Arguments for command"#);
79        ap.stop_on_first_argument(true);
80        ap.parse_args_or_exit();
81    }
82
83    args.insert(0, format!("subcommand {:?}", subcommand));
84    match subcommand {
85        Command::play => play_command(verbose, args),
86        Command::record => record_command(verbose, args),
87    }
88}
Source§

impl<'parser, 'refer, T: 'static + FromStr> Ref<'parser, 'refer, T>

Source

pub fn envvar<'x>( &'x mut self, varname: &'parser str, ) -> &'x mut Ref<'parser, 'refer, T>

Auto Trait Implementations§

§

impl<'parser, 'refer, T> Freeze for Ref<'parser, 'refer, T>

§

impl<'parser, 'refer, T> !RefUnwindSafe for Ref<'parser, 'refer, T>

§

impl<'parser, 'refer, T> !Send for Ref<'parser, 'refer, T>

§

impl<'parser, 'refer, T> !Sync for Ref<'parser, 'refer, T>

§

impl<'parser, 'refer, T> Unpin for Ref<'parser, 'refer, T>

§

impl<'parser, 'refer, T> !UnwindSafe for Ref<'parser, 'refer, T>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.