pub struct TestOptsBuilder { /* private fields */ }Expand description
Intermediate CLI parser state for TestOpts
Implementations§
Source§impl TestOptsBuilder
impl TestOptsBuilder
Sourcepub fn new() -> Self
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}Sourcepub fn parse_next<'a>(
&mut self,
parser: &mut Parser<'a>,
arg: Arg<'a>,
) -> Result<Option<Arg<'a>>, LexError<'a>>
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}Sourcepub fn finish(self) -> Result<TestOpts, LexError<'static>>
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
impl Debug for TestOptsBuilder
Source§impl Default for TestOptsBuilder
impl Default for TestOptsBuilder
Source§fn default() -> TestOptsBuilder
fn default() -> TestOptsBuilder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for TestOptsBuilder
impl RefUnwindSafe for TestOptsBuilder
impl Send for TestOptsBuilder
impl Sync for TestOptsBuilder
impl Unpin for TestOptsBuilder
impl UnwindSafe for TestOptsBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more