posixly_correct/posixly_correct.rs
1//! POSIX [recommends](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02)
2//! that no more options are parsed after the first positional argument.
3//! The other arguments are then all treated as positional arguments.
4//!
5//! lexopt can be used like this. After seeing the first positional argument
6//! (`Arg::Value`), call `Parser::raw_args`.
7//!
8//! The most logical thing to then do is often to collect the values
9//! into a `Vec`. This is shown below.
10//!
11//! Note that most modern software doesn't follow POSIX's rule and allows
12//! options anywhere (as long as they come before "--").
13
14fn main() -> Result<(), lexopt::Error> {
15 use lexopt::prelude::*;
16
17 let mut parser = lexopt::Parser::from_env();
18 let mut free = Vec::new();
19 while let Some(arg) = parser.next()? {
20 match arg {
21 Short('n') | Long("number") => {
22 let num: u16 = parser.value()?.parse()?;
23 println!("Got number {}", num);
24 }
25 Long("shout") => {
26 println!("Got --shout");
27 }
28 Value(val) => {
29 free.push(val);
30 free.extend(parser.raw_args()?);
31 }
32 _ => return Err(arg.unexpected()),
33 }
34 }
35 println!("Got free args {:?}", free);
36 Ok(())
37}