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//!
14//! [`short_equals`][lexopt::Parser::set_short_equals] also diverges
15//! from POSIX (but is otherwise unrelated).
16
17fn main() -> Result<(), lexopt::Error> {
18 use lexopt::prelude::*;
19
20 let mut parser = lexopt::Parser::from_env();
21 parser.set_short_equals(false);
22 let mut free = Vec::new();
23 while let Some(arg) = parser.next()? {
24 match arg {
25 Short('n') | Long("number") => {
26 let num: u16 = parser.value()?.parse()?;
27 println!("Got number {}", num);
28 }
29 Long("shout") => {
30 println!("Got --shout");
31 }
32 Value(val) => {
33 free.push(val);
34 free.extend(parser.raw_args()?);
35 }
36 _ => return Err(arg.unexpected()),
37 }
38 }
39 println!("Got free args {:?}", free);
40 Ok(())
41}