cmdopts 0.2.1

GNU-like command line options parser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
//
// Copyright (c) 2023,2024 Piotr Stolarz
// GNU-like Command line options parser.
//
// Distributed under the 2-clause BSD License (the License)
// see accompanying file LICENSE for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the License for more information.
//

//! GNU-like command line options parser for long and short formats. Optional
//! argument (aka option's value) may be associated with an option. The parsing
//! routine `parse_opts()` accepts two user callbacks:
//! * `opt_i()` provides parser with a context the parsed option may be used,
//! * `opt_h()` is the actual handler of the option.
//!
//! The library doesn't interpret parsed options, rather passes them to the
//! user's handler to further processing. The idea is similar to `getopt(3)`
//! and `getopt_long(3)` routines.
//!
//! # Options Format
//!
//! ## Short format
//!
//! All options starting with a single hyphen character `-` are short options.
//! For example: `-a -b -c` constitute 3 short options. These options may grouped
//! into a single block of options as `-abc`.
//!
//! If a short option requires an argument, the argument may be provided directly
//! after the option or separated by white-space(s): `-dARG` or `-d ARG`.
//!
//! If short options are grouped into a block, the last one may be provided
//! with an argument. For example: `-abcdARG` or `-abcd ARG` is equivalent to
//! `-a -b -c -d ARG`, where `-a` `-b` `-c` don't have an argument, while `-d`
//! does.
//!
//! ## Long format
//!
//! If an option starts with `--` it's long format option. For example `--help`.
//! Long options may not be formed into a group. An argument may be provided to
//! the long-format option directly after `=` character or followed by
//! white-space(s): `--config=FILE` or `--config FILE`.

use std::env;
use std::error;
use std::fmt;
use std::ops;

use CmdOpt::*;
use InfoCode::*;
use OptValSpec::*;
use ParseError::*;
use ProcessCode::*;

///
/// Command option types.
///
#[derive(Clone, Debug, PartialEq)]
pub enum CmdOpt {
    /// Short (single char) option.
    Short(char),

    /// Long (string) option.
    Long(String),
}

impl fmt::Display for CmdOpt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::Short(o) => write!(f, "-{}", o),
            Self::Long(o) => write!(f, "--{}", o),
        }
    }
}

///
/// Parser error codes returned by the option handler `opt_h()`.
///
#[derive(Clone, Debug, PartialEq)]
pub enum ParseError {
    /// Empty option found.
    EmptyOptFound,

    /// Invalid option with error description.
    InvalidOpt(CmdOpt, String),

    /// Handler may use this code to return arbitrary errors not particularly
    /// associated with some option (for which case `InvalidOpt` should be
    /// used).
    GenericErr(String),
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::EmptyOptFound => {
                write!(f, "Empty option")
            }
            Self::InvalidOpt(opt, desc) => {
                write!(f, "{}: {}", opt.to_string(), desc)?;
                Ok(())
            }
            Self::GenericErr(desc) => {
                write!(f, "{}", desc)?;
                Ok(())
            }
        }
    }
}

impl error::Error for ParseError {}

///
/// Option process codes returned by the option handler `opt_h()`.
///
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ProcessCode {
    /// Continue the parsing process in the current parsing mode.
    Continue,

    /// Break further parsing with success.
    Break,

    /// Switch the parser between *values-parsing-mode* and *options-parsing-mode*
    /// then continue the parsing process. The activated mode is valid until
    /// `ToggleParsingMode` is returned by the handler or end of options
    /// is reached.
    ///
    /// - *options-parsing-mode* - the parser recognizes options along with
    ///   their values and passes them to the handler. This is the default
    ///   parsing mode.
    ///
    /// - *values-parsing-mode* - the parser treats all parsed tokens as values
    ///   (independent tokens) and passes them to the handler with option
    ///   argument set as `None`.
    ToggleParsingMode,
}

///
/// Option info codes returned by the `opt_i()` callback.
///
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum InfoCode {
    /// Value expected for this option
    ValueOpt,

    /// No value expected for this option
    NoValueOpt,

    /// Invalid option
    InvalidOpt,
}

///
/// The enumeration specifies how a value has been provided for an option.
///
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum OptValSpec {
    /// Value provided as separate token for an option (long and short)
    /// or standalone value without associated option.
    Standalone,

    /// Value provided after '=' for long option.
    StandaloneEqu,

    /// Short option value provided as a tail of option(s) in a group
    /// (the group may consist of a single option only).
    Group,
}

///
/// Option's value description.
///
#[derive(Clone, Debug, PartialEq)]
pub struct OptVal {
    /// Value content
    pub val: String,

    /// Value specifier
    pub val_spec: OptValSpec,
}

///
/// Option constraints.
///
/// Mutable reference to an `OptConstr` object is passed to the `opt_i()`
/// hander to provide ability to define additional constraints on a parsed
/// option. The handler should call `OptConstr` public methods for the object
/// to specify required constraints or remain the object untouched if no
/// constraints are required.
///
pub struct OptConstr(u32);

impl Default for OptConstr {
    fn default() -> Self {
        OptConstr(0)
    }
}

impl OptConstr {
    const NOT_IN_GROUP: u32 = 1;

    ///
    /// Option may not occur inside a group. Applies only for short options.
    ///
    #[inline]
    pub fn not_in_group(&mut self) {
        self.0 |= Self::NOT_IN_GROUP;
    }

    #[inline]
    fn is_not_in_group(&self) -> bool {
        (self.0 & Self::NOT_IN_GROUP) != 0
    }
}

macro_rules! process_h_rc {
    ($rc:expr, $mod:expr) => {
        match $rc {
            Break => {
                return Ok(());
            }
            ToggleParsingMode => {
                $mod = !$mod;
            }
            _ => {}
        }
    };
}

fn parse_opts_iter<I, Fi, Fh>(opts: I, mut opt_i: Fi, mut opt_h: Fh) -> Result<(), ParseError>
where
    I: Iterator<Item = String>,
    Fi: FnMut(&CmdOpt, &mut OptConstr) -> InfoCode,
    Fh: FnMut(&Option<CmdOpt>, &Option<OptVal>) -> Result<ProcessCode, ParseError>,
{
    let mut val_mode = false; // false: option-parsing-mode, true: value-parsing-mode
    let mut val_req = false;  // true is value is required for the parsed option

    let mut opt: Option<CmdOpt> = None;

    for tkn in opts.into_iter() {
        if val_mode || val_req || !tkn.starts_with("-") {
            //
            // Option's value or standalone value.
            //
            let val = Some(OptVal {
                val: tkn,
                val_spec: Standalone,
            });
            let h_rc = opt_h(&opt, &val)?;
            process_h_rc!(h_rc, val_mode);

            val_req = false;
            opt = None;
            continue;
        }

        //
        // options-parsing-mode below
        //

        if tkn.starts_with("--") && tkn.len() > 2 {
            //
            // Long option. Note "--" is treated as short "-" option.
            //
            let _opt;
            let mut constr: OptConstr = Default::default();

            if let Some(equ) = tkn.find('=') {
                // value provided after '='
                _opt = Long(tkn[ops::Range { start: 2, end: equ }].to_string());
                let val = if tkn.len() <= equ + 1 {
                    None
                } else {
                    Some(OptVal {
                        val: tkn[ops::RangeFrom { start: equ + 1 }].to_string(),
                        val_spec: StandaloneEqu,
                    })
                };

                match opt_i(&_opt, &mut constr) {
                    InfoCode::InvalidOpt => {
                        return Err(ParseError::InvalidOpt(
                            _opt.clone(),
                            "Invalid option".to_string(),
                        ));
                    }
                    ValueOpt => {
                        let h_rc = opt_h(&Some(_opt), &val)?;
                        process_h_rc!(h_rc, val_mode);
                    }
                    NoValueOpt => {
                        return Err(ParseError::InvalidOpt(
                            _opt.clone(),
                            "Argument not expected".to_string(),
                        ));
                    }
                }
            } else {
                _opt = Long(tkn[2..].to_string());
                match opt_i(&_opt, &mut constr) {
                    InfoCode::InvalidOpt => {
                        return Err(ParseError::InvalidOpt(
                            _opt.clone(),
                            "Invalid option".to_string(),
                        ));
                    }
                    ValueOpt => {
                        // value provided in the next token
                        opt = Some(_opt);
                        val_req = true;
                    }
                    NoValueOpt => {
                        let h_rc = opt_h(&Some(_opt), &None)?;
                        process_h_rc!(h_rc, val_mode);
                    }
                }
            }
        } else {
            //
            // Short option.
            //
            let opts_grp = tkn[1..].to_string();
            let opts_grp_len = opts_grp.len();

            if opts_grp_len <= 0 {
                return Err(EmptyOptFound);
            }

            // parse group of shorts options
            for (i, c) in opts_grp.char_indices() {
                let mut constr: OptConstr = Default::default();
                let _opt = Short(c);

                // In case some option in the group requires an value,
                // the remaining part of the group constitutes the value.
                // In case last option in the group requires an value, the
                // next token will be used as the value.
                // If some of the option in the group suppresses options-parsing-mode,
                // the mode is still valid until the entire group is handled.
                match opt_i(&_opt, &mut constr) {
                    InfoCode::InvalidOpt => {
                        return Err(ParseError::InvalidOpt(
                            _opt.clone(),
                            "Invalid option".to_string(),
                        ));
                    }
                    ValueOpt => {
                        if constr.is_not_in_group() && i > 0 {
                            return Err(ParseError::InvalidOpt(
                                _opt.clone(),
                                "The option may not exist in group".to_string(),
                            ));
                        }

                        if i + 1 >= opts_grp_len {
                            // value provided in the next token
                            opt = Some(_opt);
                            val_req = true;
                        } else {
                            // value provided as part of the group
                            let val = Some(OptVal {
                                val: opts_grp[ops::RangeFrom { start: i + 1 }].to_string(),
                                val_spec: Group,
                            });

                            let h_rc = opt_h(&Some(_opt), &val)?;
                            process_h_rc!(h_rc, val_mode);
                        }
                        break;
                    }
                    NoValueOpt => {
                        if constr.is_not_in_group() && opts_grp.chars().count() > 1 {
                            return Err(ParseError::InvalidOpt(
                                _opt.clone(),
                                "The option may not exist in group".to_string(),
                            ));
                        }

                        let h_rc = opt_h(&Some(_opt), &None)?;
                        process_h_rc!(h_rc, val_mode);
                    }
                }
            }
        }
    }

    if opt.is_some() {
        if val_req {
            return Err(ParseError::InvalidOpt(
                opt.unwrap().clone(),
                "Argument required".to_string(),
            ));
        } else {
            opt_h(&opt, &None)?;
        }
    }
    Ok(())
}

///
/// Parse process command line options.
///
/// For every parsed option user's provided option `opt_i()` is called-back to
/// retrieve information about parsed option followed by call to `opt_h()` to
/// handle that option.
///
#[inline]
pub fn parse_opts<Fi, Fh>(opt_i: Fi, opt_h: Fh) -> Result<(), ParseError>
where
    Fi: FnMut(&CmdOpt, &mut OptConstr) -> InfoCode,
    Fh: FnMut(&Option<CmdOpt>, &Option<OptVal>) -> Result<ProcessCode, ParseError>,
{
    parse_opts_iter(env::args().skip(1), opt_i, opt_h)
}

#[allow(unused_imports)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_opt() {
        let opts = vec!["-"].into_iter().map(|v| v.to_string());

        let rc = parse_opts_iter(
            opts,
            |_, _| {
                return NoValueOpt;
            },
            |opt, val| {
                match (opt, val) {
                    _ => {
                        println!("UNEXPECTED opt:{:?}, val:{:?}", opt, val);
                        assert!(false);
                    }
                };
                Ok(Continue)
            },
        );
        assert_eq!(rc, Err(EmptyOptFound));
    }

    #[test]
    fn test_short_noval() {
        let opts = vec![
            "-abc", // group of 3 short options
            "-d",   // single short option
            "-e",   // invalid option
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |opt, _| {
                if let Short(o) = opt {
                    if "abcd".contains(*o) {
                        NoValueOpt
                    } else {
                        InfoCode::InvalidOpt
                    }
                } else {
                    InfoCode::InvalidOpt
                }
            },
            |opt, val| {
                match (i, opt, val) {
                    (0, Some(Short('a')), None) => {}
                    (1, Some(Short('b')), None) => {}
                    (2, Some(Short('c')), None) => {}
                    (3, Some(Short('d')), None) => {}
                    (4, Some(Short('e')), None) => {}
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                Ok(Continue)
            },
        );

        assert_eq!(i, 4);
        if let Err(ParseError::InvalidOpt(Short(o), _)) = rc {
            assert_eq!(o, 'e');
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_short_val() {
        let opts = vec![
            "-abc",     // single short options value read from the group
            "-d", "-e", // short option with value
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |opt, _| {
                if let Short(o) = opt {
                    if "ad".contains(*o) {
                        ValueOpt
                    } else {
                        InfoCode::InvalidOpt
                    }
                } else {
                    InfoCode::InvalidOpt
                }
            },
            |opt, val| {
                match (i, opt, val) {
                    (0, Some(Short('a')), Some(v)) if v.val == "bc" && v.val_spec == Group => {}
                    (1, Some(Short('d')), Some(v)) if v.val == "-e" && v.val_spec == Standalone => {
                    }
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                Ok(Continue)
            },
        );

        assert_eq!(i, 2);
        assert_eq!(rc, Ok(()));
    }

    #[test]
    fn test_long() {
        let opts = vec![
            "--a", "--b=v", // option with value (case 1)
            "--b", "v",     // option with value (case 2)
            "--c=v",        // value not expected
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |opt, _| {
                if let Long(o) = opt {
                    match o.as_str() {
                        "a" | "c" => InfoCode::NoValueOpt,
                        "b" => InfoCode::ValueOpt,
                        _ => InfoCode::InvalidOpt,
                    }
                } else {
                    InfoCode::InvalidOpt
                }
            },
            |opt, val| {
                match (i, opt, val) {
                    (0, Some(Long(o)), None) if o == "a" => {}
                    (1, Some(Long(o)), Some(v))
                        if o == "b" && v.val == "v" && v.val_spec == StandaloneEqu => {}
                    (2, Some(Long(o)), Some(v))
                        if o == "b" && v.val == "v" && v.val_spec == Standalone => {}
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                Ok(Continue)
            },
        );

        assert_eq!(i, 3);
        if let Err(ParseError::InvalidOpt(Long(o), _)) = rc {
            assert_eq!(o, "c");
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_inv_long_opt() {
        let opts = vec!["--a", "--a=v"];

        for a in opts.into_iter() {
            let opt = vec![a].into_iter().map(|v| v.to_string());

            let rc = parse_opts_iter(
                opt,
                |_, _| InfoCode::InvalidOpt,
                |opt, val| {
                    match (opt, val) {
                        _ => {
                            println!("UNEXPECTED opt:{:?}, val:{:?}", opt, val);
                            assert!(false);
                        }
                    }
                    Ok(Continue)
                },
            );

            if let Err(ParseError::InvalidOpt(Long(o), _)) = rc {
                assert_eq!(o, "a");
            } else {
                assert!(false);
            }
        }
    }

    #[test]
    fn test_last_opt() {
        // ok, value provided
        let opt = vec!["--a", "v"].into_iter().map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opt,
            |_, _| InfoCode::ValueOpt,
            |opt, val| {
                match (i, opt, val) {
                    (0, Some(Long(o)), Some(v))
                        if o == "a" && v.val == "v" && v.val_spec == Standalone => {}
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                }
                i += 1;
                Ok(Continue)
            },
        );

        assert_eq!(i, 1);
        assert_eq!(rc, Ok(()));

        // error, required value not provided
        let opt = vec!["-a"].into_iter().map(|v| v.to_string());

        let rc = parse_opts_iter(
            opt,
            |_, _| InfoCode::ValueOpt,
            |opt, val| {
                match (i, opt, val) {
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                }
                Ok(Continue)
            },
        );

        if let Err(ParseError::InvalidOpt(Short(o), _)) = rc {
            assert_eq!(o, 'a');
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_modes_switch() {
        let opts = vec![
            "-a",   // short option
            "--",   // switch to values-parsing-mode
            "--b",  // standalone value
            "-c",   // standalone value
            "--",   // back to options-parsing-mode
            "-d-e", // group of shorts, mode switched after it
            "-f",   // standalone value
            "FILE", // standalone value
            "--",   // back to options-parsing-mode
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |_, _| InfoCode::NoValueOpt,
            |opt, val| {
                let mut ret = Ok(Continue);
                match (i, opt, val) {
                    (0, Some(Short('a')), None) => {}
                    (1, Some(Short('-')), None) => {
                        ret = Ok(ToggleParsingMode);
                    }
                    (2, None, Some(v)) if v.val == "--b" && v.val_spec == Standalone => {}
                    (3, None, Some(v)) if v.val == "-c" && v.val_spec == Standalone => {}
                    (4, None, Some(v)) if v.val == "--" => {
                        ret = Ok(ToggleParsingMode);
                    }
                    (5, Some(Short('d')), None) => {}
                    (6, Some(Short('-')), None) => {
                        ret = Ok(ToggleParsingMode);
                    }
                    (7, Some(Short('e')), None) => {}
                    (8, None, Some(v)) if v.val == "-f" && v.val_spec == Standalone => {}
                    (9, None, Some(v)) if v.val == "FILE" && v.val_spec == Standalone => {}
                    (10, None, Some(v)) if v.val == "--" && v.val_spec == Standalone => {
                        ret = Ok(ToggleParsingMode);
                    }
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                ret
            },
        );
        assert_eq!(i, 11);
        assert_eq!(rc, Ok(()));
    }

    #[test]
    fn test_break() {
        let opts = vec![
            "-a",     // 1st short option
            "--help", // will cause break
            "-b",     // ignored
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |_, _| InfoCode::NoValueOpt,
            |opt, val| {
                let mut ret = Ok(Continue);
                match (i, opt, val) {
                    (0, Some(Short('a')), None) => {}
                    (1, Some(Long(opt)), None) if opt == "help" => {
                        ret = Ok(Break);
                    }
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                ret
            },
        );
        assert_eq!(i, 2);
        assert_eq!(rc, Ok(()));
    }

    #[test]
    fn test_not_in_group_constr() {
        // option w/o value test
        let opts = vec![
            "-a",  // ok
            "-ab", // error
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |opt, constr| {
                if let Short('a') = opt {
                    constr.not_in_group();
                }
                InfoCode::NoValueOpt
            },
            |opt, val| {
                match (i, opt, val) {
                    (0, Some(Short('a')), None) => {}
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                Ok(Continue)
            },
        );
        assert_eq!(i, 1);
        if let Err(ParseError::InvalidOpt(Short(o), _)) = rc {
            assert_eq!(o, 'a');
        } else {
            assert!(false);
        }

        // option w/value test
        let opts = vec![
            "-av",  // ok
            "-bav", // error
        ]
        .into_iter()
        .map(|v| v.to_string());

        let mut i = 0;
        let rc = parse_opts_iter(
            opts,
            |opt, constr| {
                if let Short('a') = opt {
                    constr.not_in_group();
                    InfoCode::ValueOpt
                } else {
                    InfoCode::NoValueOpt
                }
            },
            |opt, val| {
                match (i, opt, val) {
                    (0, Some(Short('a')), Some(v)) if v.val == "v" && v.val_spec == Group => {}
                    (1, Some(Short('b')), None) => {}
                    _ => {
                        println!("UNEXPECTED i:{}, opt:{:?}, val:{:?}", i, opt, val);
                        assert!(false);
                    }
                };
                i += 1;
                Ok(Continue)
            },
        );
        assert_eq!(i, 2);
        if let Err(ParseError::InvalidOpt(Short(o), _)) = rc {
            assert_eq!(o, 'a');
        } else {
            assert!(false);
        }
    }
} // mod tests