Skip to content

optional positional not working as expected? #195

Answered by pacak
0x5a4 asked this question in Q&A
Discussion options

You must be logged in to vote

By default optional would propagate parse error outwards, you can slap catch at the end to return None instead.

This should work the way you describe.

  #[derive(Debug, PartialEq, Eq)]
  struct Out {
      first: Option<String>,
      last: String,
  }

  fn the_parser() -> OptionParser<Out> {
      let first = positional::<String>("first")
          .parse(|v| {
              if v.contains(':') {
                  Ok(v)
              } else {
                  Err("no colon")
              }
          })
          .optional()
          .catch();
      let last = positional::<String>("last");
      construct!(Out { first, last }).to_options()
  }

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by pacak
Comment options

You must be logged in to vote
1 reply
@pacak
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
documentation Improvements or additions to documentation parsing Running the parser to get results
2 participants
Converted from issue

This discussion was converted from issue #194 on April 04, 2023 21:45.