blarg_builder 1.0.4

A type-safe, domain sensitive, argument/option paradigm command line 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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
use std::collections::HashMap;
use std::marker::PhantomData;

use crate::api::{Condition, Parameter, ParameterClass};
use crate::parser::{
    ArgumentCapture, ArgumentParameter, ConfigError, ConsoleInterface, GeneralParser,
    OptionCapture, UserInterface,
};
use crate::parser::{OptionParameter, ParseUnit, Parser, Printer};

/// The base command line parser.
///
/// ### Example
/// ```
/// # use blarg_builder as blarg;
/// use blarg::{CommandLineParser};
///
/// let parser = CommandLineParser::new("program")
///     // Configure with CommandLineParser::add and CommandLineParser::branch.
///     .build();
/// parser.parse_tokens(empty::slice()).unwrap();
/// ```
pub struct CommandLineParser<'a> {
    program: String,
    about: Option<String>,
    option_parameters: Vec<OptionParameter>,
    argument_parameters: Vec<ArgumentParameter>,
    option_captures: Vec<OptionCapture<'a>>,
    argument_captures: Vec<ArgumentCapture<'a>>,
    discriminator: Option<String>,
}

impl<'a> CommandLineParser<'a> {
    /// Create a command line parser.
    ///
    /// ### Example
    /// ```
    /// # use blarg_builder as blarg;
    /// use blarg::CommandLineParser;
    ///
    /// let parser = CommandLineParser::new("program")
    ///     .build();
    ///
    /// parser.parse_tokens(vec![].as_slice()).unwrap();
    /// ```
    pub fn new(program: impl Into<String>) -> Self {
        Self {
            program: program.into(),
            about: None,
            option_parameters: Vec::default(),
            argument_parameters: Vec::default(),
            option_captures: Vec::default(),
            argument_captures: Vec::default(),
            discriminator: None,
        }
    }

    /// Document the about message for this command line parser.
    /// If repeated, only the final help message will apply.
    ///
    /// An about message documents the command line parser in full sentence/paragraph format.
    /// We recommend allowing `blarg` to format this field (ex: it is not recommended to use line breaks `'\n'`).
    ///
    /// ### Example
    /// ```
    /// # use blarg_builder as blarg;
    /// use blarg::CommandLineParser;
    ///
    /// let parser = CommandLineParser::new("program")
    ///     .about("--this will get discarded--")
    ///     .about("My program that does awesome stuff.  Check it out!")
    ///     .build();
    ///
    /// parser.parse_tokens(vec![].as_slice()).unwrap();
    /// ```
    pub fn about(mut self, description: impl Into<String>) -> Self {
        self.about.replace(description.into());
        self
    }

    /// Add an argument/option to the command line parser.
    ///
    /// The order of argument parameters corresponds to their positional order during parsing.
    /// The order of option parameters does not affect the command parser semantics.
    ///
    /// ### Example
    /// ```
    /// # use blarg_builder as blarg;
    /// use blarg::{CommandLineParser, Parameter, Scalar};
    ///
    /// let mut a: u32 = 0;
    /// let mut b: u32 = 0;
    /// let parser = CommandLineParser::new("program")
    ///     .add(Parameter::argument(Scalar::new(&mut a), "a"))
    ///     .add(Parameter::argument(Scalar::new(&mut b), "b"))
    ///     .build();
    ///
    /// parser.parse_tokens(vec!["1", "2"].as_slice()).unwrap();
    ///
    /// assert_eq!(a, 1);
    /// assert_eq!(b, 2);
    /// ```
    pub fn add<T>(mut self, parameter: Parameter<'a, T>) -> Self {
        let inner = parameter.consume();
        match inner.class() {
            ParameterClass::Opt => {
                self.option_parameters.push(OptionParameter::from(&inner));
                self.option_captures.push(OptionCapture::from(inner));
            }
            ParameterClass::Arg => {
                self.argument_parameters
                    .push(ArgumentParameter::from(&inner));
                self.argument_captures.push(ArgumentCapture::from(inner));
            }
        }

        self
    }

    /// Branch into a sub-command parser.
    ///
    /// This changes the command line parser into a sub-command style command line parser.
    /// Any parameters added before the branch apply to the root parser.
    ///
    /// Branching is always done with a special `Scalar` argument: [`Condition`].
    ///
    /// ### Example
    /// ```
    /// # use blarg_builder as blarg;
    /// use blarg::{CommandLineParser, Parameter, Scalar, Condition};
    ///
    /// let mut belongs_to_root: u32 = 0;
    /// let mut sub_command: String = "".to_string();
    /// let mut belongs_to_sub_command: u32 = 0;
    /// let parser = CommandLineParser::new("program")
    ///     .add(Parameter::argument(Scalar::new(&mut belongs_to_root), "belongs_to_root"))
    ///     .branch(Condition::new(Scalar::new(&mut sub_command), "sub_command"))
    ///     .command("the-command".to_string(), |sub| {
    ///         sub.add(Parameter::argument(Scalar::new(&mut belongs_to_sub_command), "belongs_to_sub_command"))
    ///     })
    ///     .build();
    ///
    /// parser.parse_tokens(vec!["1", "the-command", "2"].as_slice()).unwrap();
    ///
    /// assert_eq!(belongs_to_root, 1);
    /// assert_eq!(&sub_command, "the-command");
    /// assert_eq!(belongs_to_sub_command, 2);
    /// ```
    pub fn branch<T: std::str::FromStr + std::fmt::Display + PartialEq>(
        mut self,
        condition: Condition<'a, T>,
    ) -> SubCommandParser<'a, T> {
        let parameter = condition.consume();
        if self.discriminator.replace(parameter.name()).is_some() {
            unreachable!("internal error - cannot setup multiple discriminators");
        }

        SubCommandParser::new(self.add(parameter))
    }

    fn build_with_interface(
        self,
        user_interface: Box<dyn UserInterface>,
    ) -> Result<GeneralParser<'a>, ConfigError> {
        let parser = Parser::new(
            self.option_captures,
            self.argument_captures,
            self.discriminator,
        )?;
        let command = ParseUnit::new(
            parser,
            Printer::terminal(
                self.program.clone(),
                self.about,
                self.option_parameters,
                self.argument_parameters,
            ),
        );
        Ok(GeneralParser::command(command, user_interface))
    }

    /// Build the command line parser as a Result.
    /// This finalizes the configuration and checks for errors (ex: a repeated parameter name).
    pub fn build_parser(self) -> Result<GeneralParser<'a>, ConfigError> {
        self.build_with_interface(Box::new(ConsoleInterface::default()))
    }

    /// Build the command line parser.
    /// This finalizes the configuration and checks for errors (ex: a repeated parameter name).
    /// If an error is encountered, exits with error code `1` (via [`std::process::exit`]).
    pub fn build(self) -> GeneralParser<'a> {
        match self.build_parser() {
            Ok(gp) => gp,
            Err(e) => {
                eprintln!("{e}");
                std::process::exit(1);
            }
        }
    }
}

/// The sub-command parser.
pub struct SubCommandParser<'a, B: std::fmt::Display> {
    root: CommandLineParser<'a>,
    commands: HashMap<String, CommandLineParser<'a>>,
    deferred_error: Option<ConfigError>,
    _phantom: PhantomData<B>,
}

impl<'a, B: std::str::FromStr + std::fmt::Display + PartialEq> SubCommandParser<'a, B> {
    fn new(root: CommandLineParser<'a>) -> Self {
        Self {
            root,
            commands: HashMap::default(),
            deferred_error: None,
            _phantom: PhantomData,
        }
    }

    /// Setup a sub-command.
    ///
    /// Sub-commands may be added arbitrarily, as long as the correspond to the branching type `B`.
    /// If repeated for the same `variant` of `B`, only the final version will be created on the parser.
    /// The order of sub-commands does not affect the command parser semantics.
    ///
    /// ### Example
    /// ```
    /// # use blarg_builder as blarg;
    /// use blarg::{CommandLineParser, Condition, Parameter, Scalar};
    ///
    /// let mut value_a: u32 = 0;
    /// let mut value_b: u32 = 0;
    /// let mut sub_command: String = "".to_string();
    /// let parser = CommandLineParser::new("program")
    ///     .branch(Condition::new(Scalar::new(&mut sub_command), "sub_command"))
    ///     .command("a".to_string(), |sub| sub.add(Parameter::argument(Scalar::new(&mut value_a), "value_a")))
    ///     .command("b".to_string(), |sub| {
    ///         sub.about("Description for the sub-command 'b'.")
    ///             .add(Parameter::argument(Scalar::new(&mut value_b), "value_b"))
    ///     })
    ///     .build();
    ///
    /// parser.parse_tokens(vec!["a", "1"].as_slice()).unwrap();
    ///
    /// assert_eq!(&sub_command, "a");
    /// assert_eq!(value_a, 1);
    /// assert_eq!(value_b, 0);
    /// ```
    pub fn command(
        mut self,
        variant: B,
        setup_fn: impl FnOnce(SubCommand<'a>) -> SubCommand<'a>,
    ) -> Self {
        let command_str = variant.to_string();

        // Check if the variant does not respect the FromStr-inverts-Display invariant.
        match B::from_str(&command_str) {
            // This is where someone is trying to trick us!
            // The from_str inverts to a valid `B`, however it is not this specific variant.
            Ok(value) if value != variant => {
                self.deferred_error.replace(ConfigError(format!(
                    "parameter '{}' contains invalid sub-command '{command_str}': FromStr does not invert Display.",
                    self.root.discriminator.as_ref().expect("internal error - root must have a discriminator"),
                )));
            }
            // The from_str simply does not invert to a valid `B`.
            Err(_) => {
                self.deferred_error.replace(ConfigError(format!(
                    "parameter '{}' contains invalid sub-command '{command_str}': FromStr does not invert Display.",
                    self.root.discriminator.as_ref().expect("internal error - root must have a discriminator"),
                )));
            }
            _ => {
                // Do nothing.
            }
        }

        let inner = CommandLineParser::new(command_str.clone());
        let sub_command = setup_fn(SubCommand { inner });
        self.commands.insert(command_str, sub_command.inner);
        self
    }

    fn build_with_interface(
        self,
        user_interface: Box<dyn UserInterface>,
    ) -> Result<GeneralParser<'a>, ConfigError> {
        if let Some(error) = self.deferred_error {
            return Err(error);
        }

        let mut sub_commands = HashMap::default();

        for (discriminee, cp) in self.commands.into_iter() {
            let sub_parser = Parser::new(cp.option_captures, cp.argument_captures, None)?;
            let sub_command = ParseUnit::new(
                sub_parser,
                Printer::terminal(
                    format!(
                        "{program} {sub_program}",
                        program = self.root.program,
                        sub_program = cp.program
                    ),
                    cp.about,
                    cp.option_parameters,
                    cp.argument_parameters,
                ),
            );
            sub_commands.insert(discriminee, sub_command);
        }

        let parser = Parser::new(
            self.root.option_captures,
            self.root.argument_captures,
            self.root.discriminator,
        )?;
        let command = ParseUnit::new(
            parser,
            Printer::terminal(
                self.root.program.clone(),
                self.root.about,
                self.root.option_parameters,
                self.root.argument_parameters,
            ),
        );
        Ok(GeneralParser::sub_command(
            // self.root.program,
            command,
            sub_commands,
            user_interface,
        ))
    }

    /// Build the sub-command based command line parser as a Result.
    /// This finalizes the configuration and checks for errors (ex: a repeated parameter name).
    pub fn build_parser(self) -> Result<GeneralParser<'a>, ConfigError> {
        self.build_with_interface(Box::new(ConsoleInterface::default()))
    }

    /// Build the sub-command based command line parser.
    /// This finalizes the configuration and checks for errors (ex: a repeated parameter name).
    /// If an error is encountered, exits with error code `1` (via [`std::process::exit`]).
    pub fn build(self) -> GeneralParser<'a> {
        match self.build_parser() {
            Ok(gp) => gp,
            Err(e) => {
                eprintln!("{e}");
                std::process::exit(1);
            }
        }
    }
}

/// A sub-command line parser.
///
/// Used with [`SubCommandParser::command`].
pub struct SubCommand<'a> {
    inner: CommandLineParser<'a>,
}

impl<'a> SubCommand<'a> {
    /// *Available using 'unit_test' crate feature only.*</br></br>
    /// Build a [`SubCommand`] for use in testing.
    ///
    /// ### Example
    /// ```
    /// # use blarg_builder as blarg;
    /// use blarg::{Parameter, Scalar, SubCommand};
    ///
    /// // Function under test.
    /// // We want to make sure the setup_fn is wired up correctly.
    /// pub fn setup_fn<'a>(value: &'a mut u32) -> impl FnOnce(SubCommand<'a>) -> SubCommand<'a> {
    ///     |sub| sub.add(Parameter::argument(Scalar::new(value), "value"))
    /// }
    ///
    /// let mut x: u32 = 1;
    /// let parser = setup_fn(&mut x)(SubCommand::test_dummy()).build_parser().unwrap();
    /// parser.parse_tokens(vec!["2"].as_slice()).unwrap();
    /// assert_eq!(x, 2);
    /// ```
    #[cfg(feature = "unit_test")]
    pub fn test_dummy() -> Self {
        SubCommand {
            inner: CommandLineParser::new("test-dummy"),
        }
    }

    /// *Available using 'unit_test' crate feature only.*</br></br>
    /// Build a [`GeneralParser`] for testing.
    /// See [`SubCommand::test_dummy`] for an example.
    #[cfg(feature = "unit_test")]
    pub fn build_parser(self) -> Result<GeneralParser<'a>, ConfigError> {
        self.inner
            .build_with_interface(Box::new(ConsoleInterface::default()))
    }

    /// Document the about message for this sub-command.
    /// If repeated, only the final help message will apply.
    ///
    /// An about message documents the sub-command in full sentence/paragraph format.
    /// We recommend allowing `blarg` to format this field (ex: it is not recommended to use line breaks `'\n'`).
    ///
    /// See [`SubCommandParser::command`] for usage.
    pub fn about(self, description: impl Into<String>) -> Self {
        SubCommand {
            inner: self.inner.about(description),
        }
    }

    /// Add an argument/option to the sub-command.
    ///
    /// The order of argument parameters corresponds to their positional order during parsing.
    /// The order of option parameters does not affect the sub-command parser semantics.
    ///
    /// See [`SubCommandParser::command`] for usage.
    pub fn add<T>(self, parameter: Parameter<'a, T>) -> Self {
        SubCommand {
            inner: self.inner.add(parameter),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::{Collection, Parameter, Scalar, Switch};
    use crate::model::Nargs;
    use crate::parser::util::channel_interface;
    use crate::prelude::Choices;
    use crate::test::assert_contains;
    use rstest::rstest;

    #[test]
    fn empty_build() {
        // Setup
        let clp = CommandLineParser::new("program");

        // Execute
        let parser = clp.build_parser().unwrap();

        // Verify
        assert_eq!(parser.details(), ("program".to_string(), None));
        parser.parse_tokens(empty::slice()).unwrap();
    }

    #[rstest]
    #[case(vec![], false, vec![])]
    #[case(vec!["1"], false, vec![1])]
    #[case(vec!["01"], false, vec![1])]
    #[case(vec!["1", "3", "2"], false, vec![1, 3, 2])]
    #[case(vec!["--flag"], true, vec![])]
    #[case(vec!["--flag", "1"], true, vec![1])]
    #[case(vec!["--flag", "01"], true, vec![1])]
    #[case(vec!["--flag", "1", "3", "2"], true, vec![1, 3, 2])]
    fn build(
        #[case] tokens: Vec<&str>,
        #[case] expected_flag: bool,
        #[case] expected_items: Vec<u32>,
    ) {
        // Setup
        let mut flag: bool = false;
        let mut items: Vec<u32> = Vec::default();
        let clp = CommandLineParser::new("program")
            .about("abc def")
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .add(Parameter::argument(
                Collection::new(&mut items, Nargs::Any),
                "item",
            ));

        // Execute
        let parser = clp.build_parser().unwrap();

        // Verify
        assert_eq!(
            parser.details(),
            ("program".to_string(), Some("abc def".to_string()))
        );

        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with the various permutations.
        parser.parse_tokens(tokens.as_slice()).unwrap();
        assert_eq!(flag, expected_flag);
        assert_eq!(items, expected_items);
    }

    #[rstest]
    #[case(vec!["0"], false, 0, vec![], vec![])]
    #[case(vec!["0", "1"], false, 0, vec![1], vec![])]
    #[case(vec!["0", "1", "3", "2"], false, 0, vec![1, 3, 2], vec![])]
    #[case(vec!["1"], false, 1, vec![], vec![])]
    #[case(vec!["1", "1"], false, 1, vec![], vec![1])]
    #[case(vec!["1", "1", "3", "2"], false, 1, vec![], vec![1, 3, 2])]
    #[case(vec!["--flag", "0"], true, 0, vec![], vec![])]
    #[case(vec!["--flag", "0", "1"], true, 0, vec![1], vec![])]
    #[case(vec!["--flag", "0", "1", "3", "2"], true, 0, vec![1, 3, 2], vec![])]
    #[case(vec!["--flag", "1"], true, 1, vec![], vec![])]
    #[case(vec!["--flag", "1", "1"], true, 1, vec![], vec![1])]
    #[case(vec!["--flag", "1", "1", "3", "2"], true, 1, vec![], vec![1, 3, 2])]
    fn branch_build(
        #[case] tokens: Vec<&str>,
        #[case] expected_flag: bool,
        #[case] expected_sub: u32,
        #[case] expected_items_0: Vec<u32>,
        #[case] expected_items_1: Vec<u32>,
    ) {
        // Setup
        let mut flag: bool = false;
        let mut sub: u32 = 0;
        let mut items_0: Vec<u32> = Vec::default();
        let mut items_1: Vec<u32> = Vec::default();
        let clp = CommandLineParser::new("program");
        let scp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .branch(Condition::new(Scalar::new(&mut sub), "sub"))
            .command(0, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items_0, Nargs::Any),
                    "item0",
                ))
            })
            .command(1, |sub| {
                sub.about("abc def").add(Parameter::argument(
                    Collection::new(&mut items_1, Nargs::Any),
                    "item1",
                ))
            });

        // Execute
        let parser = scp.build_parser().unwrap();

        // Verify
        assert_eq!(parser.details(), ("program".to_string(), None));
        assert_eq!(parser.sub_details("x"), None);
        assert_eq!(
            parser.sub_details("0"),
            Some(("program 0".to_string(), None))
        );
        assert_eq!(
            parser.sub_details("1"),
            Some(("program 1".to_string(), Some("abc def".to_string())))
        );

        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with the various permutations.
        parser.parse_tokens(tokens.as_slice()).unwrap();
        assert_eq!(flag, expected_flag);
        assert_eq!(sub, expected_sub);
        assert_eq!(items_0, expected_items_0);
        assert_eq!(items_1, expected_items_1);
    }

    #[test]
    fn repeat_command_build() {
        // Setup
        let mut sub: u32 = 0;
        let mut items_0: Vec<u32> = Vec::default();
        let mut items_1: Vec<u32> = Vec::default();
        let clp = CommandLineParser::new("program");
        let scp = clp
            .branch(Condition::new(Scalar::new(&mut sub), "sub"))
            .command(0, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items_0, Nargs::Any),
                    "item0",
                ))
            })
            .command(0, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items_1, Nargs::Any),
                    "item1",
                ))
            });

        // Execute
        let parser = scp.build_parser().unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with the various permutations.
        parser.parse_tokens(&["0", "1", "2", "3"]).unwrap();
        assert_eq!(sub, 0);
        assert_eq!(items_0, Vec::default());
        assert_eq!(items_1, vec![1, 2, 3]);
    }

    #[rstest]
    #[case(vec!["abc", "0"], false, "abc", 0, vec![])]
    #[case(vec!["abc", "0", "1"], false, "abc", 0, vec![1])]
    #[case(vec!["abc", "0", "1", "3", "2"], false, "abc", 0, vec![1, 3, 2])]
    #[case(vec!["--flag", "abc", "0"], true, "abc", 0, vec![])]
    #[case(vec!["--flag", "abc", "0", "1"], true, "abc", 0, vec![1])]
    #[case(vec!["--flag", "abc", "0", "1", "3", "2"], true, "abc", 0, vec![1, 3, 2])]
    #[case(vec!["abc", "--flag", "0"], true, "abc", 0, vec![])]
    #[case(vec!["abc", "--flag", "0", "1"], true, "abc", 0, vec![1])]
    #[case(vec!["abc", "--flag", "0", "1", "3", "2"], true, "abc", 0, vec![1, 3, 2])]
    fn root_arguments_branch_build(
        #[case] tokens: Vec<&str>,
        #[case] expected_flag: bool,
        #[case] expected_root: &str,
        #[case] expected_sub: u32,
        #[case] expected_items: Vec<u32>,
    ) {
        // Setup
        let mut flag: bool = false;
        let mut root: String = String::default();
        let mut sub: u32 = 0;
        let mut items: Vec<u32> = Vec::default();
        let clp = CommandLineParser::new("program");
        let scp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .add(Parameter::argument(Scalar::new(&mut root), "root"))
            .branch(Condition::new(Scalar::new(&mut sub), "sub"))
            .command(0, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items, Nargs::Any),
                    "item0",
                ))
            });

        // Execute
        let parser = scp.build_parser().unwrap();

        // Verify
        assert_eq!(parser.details(), ("program".to_string(), None));

        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with the various permutations.
        parser.parse_tokens(tokens.as_slice()).unwrap();
        assert_eq!(flag, expected_flag);
        assert_eq!(&root, expected_root);
        assert_eq!(sub, expected_sub);
        assert_eq!(items, expected_items);
    }

    #[test]
    fn empty_build_help() {
        // Setup
        let clp = CommandLineParser::new("program");
        let (sender, receiver) = channel_interface();

        // Execute
        let parser = clp.build_with_interface(Box::new(sender)).unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with --help and spot-checking the output.
        let error_code = parser.parse_tokens(&["--help"]).unwrap_err();
        assert_eq!(error_code, 0);

        let message = receiver.consume_message();
        assert_contains!(message, "usage: program [-h]\n");
    }

    #[test]
    fn build_help() {
        // Setup
        let mut flag: bool = false;
        let mut items: Vec<u32> = Vec::default();
        let mut clp = CommandLineParser::new("program");
        clp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .add(Parameter::argument(
                Collection::new(&mut items, Nargs::Any),
                "item",
            ));
        let (sender, receiver) = channel_interface();

        // Execute
        let parser = clp.build_with_interface(Box::new(sender)).unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with --help and spot-checking the output.
        let error_code = parser.parse_tokens(&["--help"]).unwrap_err();
        assert_eq!(error_code, 0);

        let message = receiver.consume_message();
        assert_contains!(message, "usage: program [-h] [-f] [ITEM ...]\n");
        assert_contains!(message, "-f, --flag");
    }

    #[test]
    fn branch_build_help() {
        // Setup
        let mut flag: bool = false;
        let mut sub: u32 = 0;
        let clp = CommandLineParser::new("program");
        let scp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .branch(
                Condition::new(Scalar::new(&mut sub), "sub")
                    .choice(0, "zero")
                    .choice(1, "one"),
            )
            .command(0, |sub| sub)
            .command(1, |sub| sub);
        let (sender, receiver) = channel_interface();

        // Execute
        let parser = scp.build_with_interface(Box::new(sender)).unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with --help and spot-checking the output.
        let error_code = parser.parse_tokens(&["--help"]).unwrap_err();
        assert_eq!(error_code, 0);

        let message = receiver.consume_message();
        assert_contains!(message, "usage: program [-h] [-f] SUB\n");
        assert_contains!(message, "SUB          {0, 1}");
        assert_contains!(message, "0            zero");
        assert_contains!(message, "1            one");
        assert_contains!(message, "-f, --flag");
    }

    #[test]
    fn sub0_command_build_help() {
        // Setup
        let mut flag: bool = false;
        let mut sub: u32 = 0;
        let mut items: Vec<u32> = Vec::default();
        let mut extra: bool = false;
        let clp = CommandLineParser::new("program");
        let scp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .branch(
                Condition::new(Scalar::new(&mut sub), "sub")
                    .choice(0, "zero")
                    .choice(1, "one"),
            )
            .command(0, |sub| sub)
            .command(1, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items, Nargs::Any),
                    "item",
                ))
                .add(Parameter::option(
                    Switch::new(&mut extra, true),
                    "extra",
                    Some('e'),
                ))
            });
        let (sender, receiver) = channel_interface();

        // Execute
        let parser = scp.build_with_interface(Box::new(sender)).unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with --help and spot-checking the output.
        let error_code = parser.parse_tokens(&["0", "--help"]).unwrap_err();
        assert_eq!(error_code, 0);

        let message = receiver.consume_message();
        assert_contains!(message, "usage: program 0 [-h]\n");
    }

    #[test]
    fn sub1_command_build_help() {
        // Setup
        let mut flag: bool = false;
        let mut sub: u32 = 0;
        let mut items: Vec<u32> = Vec::default();
        let mut extra: bool = false;
        let clp = CommandLineParser::new("program");
        let scp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .branch(
                Condition::new(Scalar::new(&mut sub), "sub")
                    .choice(0, "zero")
                    .choice(1, "one"),
            )
            .command(0, |sub| sub)
            .command(1, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items, Nargs::Any),
                    "item",
                ))
                .add(Parameter::option(
                    Switch::new(&mut extra, true),
                    "extra",
                    Some('e'),
                ))
            });
        let (sender, receiver) = channel_interface();

        // Execute
        let parser = scp.build_with_interface(Box::new(sender)).unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with --help and spot-checking the output.
        let error_code = parser.parse_tokens(&["1", "--help"]).unwrap_err();
        assert_eq!(error_code, 0);

        let message = receiver.consume_message();
        assert_contains!(message, "usage: program 1 [-h] [-e] [ITEM ...]\n");
        assert_contains!(message, "-e, --extra");
    }

    #[test]
    fn root_arguments_branch_build_help() {
        // Setup
        let mut flag: bool = false;
        let mut root: String = String::default();
        let mut sub: u32 = 0;
        let mut items: Vec<u32> = Vec::default();
        let clp = CommandLineParser::new("program");
        let scp = clp
            .add(Parameter::option(
                Switch::new(&mut flag, true),
                "flag",
                Some('f'),
            ))
            .add(Parameter::argument(Scalar::new(&mut root), "root"))
            .branch(Condition::new(Scalar::new(&mut sub), "sub"))
            .command(0, |sub| {
                sub.add(Parameter::argument(
                    Collection::new(&mut items, Nargs::Any),
                    "item0",
                ))
            });
        let (sender, receiver) = channel_interface();

        // Execute
        let parser = scp.build_with_interface(Box::new(sender)).unwrap();

        // Verify
        // We testing that build sets up the right parser.
        // So the verification involves invoking the parser with --help and spot-checking the output.
        let error_code = parser.parse_tokens(&["--help"]).unwrap_err();
        assert_eq!(error_code, 0);

        let message = receiver.consume_message();
        assert_contains!(message, "usage: program [-h] [-f] ROOT SUB\n");
    }

    #[test]
    #[cfg(feature = "unit_test")]
    fn test_dummies() {
        // Setup
        pub fn setup_fn<'a>(value: &'a mut u32) -> impl FnOnce(SubCommand<'a>) -> SubCommand<'a> {
            |sub| sub.add(Parameter::argument(Scalar::new(value), "value"))
        }

        let mut x: u32 = 1;
        let parser = setup_fn(&mut x)(SubCommand::test_dummy())
            .build_parser()
            .unwrap();
        let tokens = vec!["2"];

        // Execute
        parser.parse_tokens(tokens.as_slice()).unwrap();

        // Verify
        assert_eq!(x, 2);
    }

    #[derive(PartialEq)]
    enum Nefarious {
        Foo,
        Bar,
    }

    impl std::fmt::Display for Nefarious {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                Nefarious::Foo => write!(f, "foo"),
                Nefarious::Bar => write!(f, "bar"),
            }
        }
    }

    impl std::str::FromStr for Nefarious {
        type Err = String;

        fn from_str(value: &str) -> Result<Self, Self::Err> {
            match value.to_lowercase().as_str() {
                "bar" => Ok(Nefarious::Foo),
                _ => Err(format!("unknown: {}", value)),
            }
        }
    }

    #[test]
    fn not_invertable_command() {
        // Setup
        let mut nefarious = Nefarious::Bar;
        let clp = CommandLineParser::new("program");
        let scp = clp
            .branch(Condition::new(Scalar::new(&mut nefarious), "abc"))
            .command(Nefarious::Foo, |sub| sub);
        let (sender, _receiver) = channel_interface();

        // Execute
        let result = scp.build_with_interface(Box::new(sender)).unwrap_err();

        // Verify
        assert_matches!(result, ConfigError(message) => {
            assert_eq!(message, "parameter 'abc' contains invalid sub-command 'foo': FromStr does not invert Display.".to_string());
        });
    }

    #[test]
    fn nefarious_command() {
        // Setup
        let mut nefarious = Nefarious::Bar;
        let clp = CommandLineParser::new("program");
        let scp = clp
            .branch(Condition::new(Scalar::new(&mut nefarious), "abc"))
            .command(Nefarious::Bar, |sub| sub);
        let (sender, _receiver) = channel_interface();

        // Execute
        let result = scp.build_with_interface(Box::new(sender)).unwrap_err();

        // Verify
        assert_matches!(result, ConfigError(message) => {
            assert_eq!(message, "parameter 'abc' contains invalid sub-command 'bar': FromStr does not invert Display.".to_string());
        });
    }
}