TestOptsBuilder

Struct TestOptsBuilder 

Source
pub struct TestOptsBuilder { /* private fields */ }
Expand description

Intermediate CLI parser state for TestOpts

See TestOptsBuilder::parse_next

Implementations§

Source§

impl TestOptsBuilder

Source

pub fn new() -> Self

Examples found in repository?
examples/libtest-cli.rs (line 4)
1fn main() -> lexarg::Result<()> {
2    use lexarg::prelude::*;
3
4    let mut test_opts = libtest_lexarg::TestOptsBuilder::new();
5
6    let raw = std::env::args_os().collect::<Vec<_>>();
7    let mut parser = lexarg::Parser::new(&raw);
8    let bin = parser
9        .next_raw()
10        .expect("first arg, no pending values")
11        .unwrap_or(std::ffi::OsStr::new("test"));
12    let mut prev_arg = Value(bin);
13    while let Some(arg) = parser.next_arg() {
14        match arg {
15            Short("h") | Long("help") => {
16                let bin = bin.to_string_lossy();
17                let options_help = libtest_lexarg::OPTIONS_HELP.trim();
18                let after_help = libtest_lexarg::AFTER_HELP.trim();
19                println!(
20                    "Usage: {bin} [OPTIONS] [FILTER]...
21
22{options_help}
23
24{after_help}"
25                );
26                std::process::exit(0);
27            }
28            // All values are the same, whether escaped or not, so its a no-op
29            Escape(_) => {
30                prev_arg = arg;
31                continue;
32            }
33            Unexpected(_) => {
34                return Err(lexarg::LexError::msg("unexpected value")
35                    .unexpected(arg)
36                    .within(prev_arg)
37                    .into());
38            }
39            _ => {}
40        }
41        prev_arg = arg;
42
43        let arg = test_opts.parse_next(&mut parser, arg)?;
44
45        if let Some(arg) = arg {
46            return Err(lexarg::LexError::msg("unexpected argument")
47                .unexpected(arg)
48                .into());
49        }
50    }
51
52    let opts = test_opts.finish()?;
53    println!("{opts:#?}");
54
55    Ok(())
56}
Source

pub fn parse_next<'a>( &mut self, parser: &mut Parser<'a>, arg: Arg<'a>, ) -> Result<Option<Arg<'a>>, LexError<'a>>

Check if arg is relevant to TestOpts

Examples found in repository?
examples/libtest-cli.rs (line 43)
1fn main() -> lexarg::Result<()> {
2    use lexarg::prelude::*;
3
4    let mut test_opts = libtest_lexarg::TestOptsBuilder::new();
5
6    let raw = std::env::args_os().collect::<Vec<_>>();
7    let mut parser = lexarg::Parser::new(&raw);
8    let bin = parser
9        .next_raw()
10        .expect("first arg, no pending values")
11        .unwrap_or(std::ffi::OsStr::new("test"));
12    let mut prev_arg = Value(bin);
13    while let Some(arg) = parser.next_arg() {
14        match arg {
15            Short("h") | Long("help") => {
16                let bin = bin.to_string_lossy();
17                let options_help = libtest_lexarg::OPTIONS_HELP.trim();
18                let after_help = libtest_lexarg::AFTER_HELP.trim();
19                println!(
20                    "Usage: {bin} [OPTIONS] [FILTER]...
21
22{options_help}
23
24{after_help}"
25                );
26                std::process::exit(0);
27            }
28            // All values are the same, whether escaped or not, so its a no-op
29            Escape(_) => {
30                prev_arg = arg;
31                continue;
32            }
33            Unexpected(_) => {
34                return Err(lexarg::LexError::msg("unexpected value")
35                    .unexpected(arg)
36                    .within(prev_arg)
37                    .into());
38            }
39            _ => {}
40        }
41        prev_arg = arg;
42
43        let arg = test_opts.parse_next(&mut parser, arg)?;
44
45        if let Some(arg) = arg {
46            return Err(lexarg::LexError::msg("unexpected argument")
47                .unexpected(arg)
48                .into());
49        }
50    }
51
52    let opts = test_opts.finish()?;
53    println!("{opts:#?}");
54
55    Ok(())
56}
Source

pub fn finish(self) -> Result<TestOpts, LexError<'static>>

Finish parsing, resolving to TestOpts

Examples found in repository?
examples/libtest-cli.rs (line 52)
1fn main() -> lexarg::Result<()> {
2    use lexarg::prelude::*;
3
4    let mut test_opts = libtest_lexarg::TestOptsBuilder::new();
5
6    let raw = std::env::args_os().collect::<Vec<_>>();
7    let mut parser = lexarg::Parser::new(&raw);
8    let bin = parser
9        .next_raw()
10        .expect("first arg, no pending values")
11        .unwrap_or(std::ffi::OsStr::new("test"));
12    let mut prev_arg = Value(bin);
13    while let Some(arg) = parser.next_arg() {
14        match arg {
15            Short("h") | Long("help") => {
16                let bin = bin.to_string_lossy();
17                let options_help = libtest_lexarg::OPTIONS_HELP.trim();
18                let after_help = libtest_lexarg::AFTER_HELP.trim();
19                println!(
20                    "Usage: {bin} [OPTIONS] [FILTER]...
21
22{options_help}
23
24{after_help}"
25                );
26                std::process::exit(0);
27            }
28            // All values are the same, whether escaped or not, so its a no-op
29            Escape(_) => {
30                prev_arg = arg;
31                continue;
32            }
33            Unexpected(_) => {
34                return Err(lexarg::LexError::msg("unexpected value")
35                    .unexpected(arg)
36                    .within(prev_arg)
37                    .into());
38            }
39            _ => {}
40        }
41        prev_arg = arg;
42
43        let arg = test_opts.parse_next(&mut parser, arg)?;
44
45        if let Some(arg) = arg {
46            return Err(lexarg::LexError::msg("unexpected argument")
47                .unexpected(arg)
48                .into());
49        }
50    }
51
52    let opts = test_opts.finish()?;
53    println!("{opts:#?}");
54
55    Ok(())
56}

Trait Implementations§

Source§

impl Debug for TestOptsBuilder

Source§

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

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

impl Default for TestOptsBuilder

Source§

fn default() -> TestOptsBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.