dynamic-cli 0.7.0

A framework for building configurable CLI and REPL applications from YAML/JSON configuration files
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
//! CLI argument parser
//!
//! This module provides the [`CliParser`] which parses Unix-style command-line
//! arguments into a structured HashMap. It handles:
//! - Positional arguments
//! - Short options (`-v`)
//! - Long options (`--verbose`)
//! - Options with values (`-o file.txt`, `--output=file.txt`)
//! - Type conversion and validation
//!
//! # Example
//!
//! ```
//! use dynamic_cli::parser::cli_parser::CliParser;
//! use dynamic_cli::config::schema::{CommandDefinition, ArgumentDefinition, ArgumentType};
//!
//! let definition = CommandDefinition {
//!     name: "process".to_string(),
//!     aliases: vec![],
//!     description: "Process files".to_string(),
//!     required: false,
//!     arguments: vec![
//!         ArgumentDefinition {
//!             name: "input".to_string(),
//!             arg_type: ArgumentType::Path,
//!             required: true,
//!             description: "Input file".to_string(),
//!             validation: vec![],
//!             secure: false,
//!         }
//!     ],
//!     options: vec![],
//!     implementation: "handler".to_string(),
//! };
//!
//! let parser = CliParser::new(&definition);
//! let args = vec!["file.txt".to_string()];
//! let parsed = parser.parse(&args).unwrap();
//!
//! assert_eq!(parsed.get("input"), Some(&"file.txt".to_string()));
//! ```

#[allow(unused_imports)]
use crate::config::schema::{ArgumentDefinition, CommandDefinition, OptionDefinition};
use crate::error::{ParseError, Result};
use crate::parser::type_parser;
use std::collections::HashMap;

/// CLI argument parser
///
/// Parses command-line arguments according to a [`CommandDefinition`].
/// The parser handles both positional arguments and named options
/// with type conversion and validation.
///
/// # Lifetime
///
/// The parser holds a reference to a [`CommandDefinition`] and therefore
/// has a lifetime parameter `'a` that must outlive the parser.
///
/// # Example
///
/// ```
/// use dynamic_cli::parser::cli_parser::CliParser;
/// use dynamic_cli::config::schema::{
///     CommandDefinition, OptionDefinition, ArgumentType
/// };
/// use std::collections::HashMap;
///
/// let definition = CommandDefinition {
///     name: "test".to_string(),
///     aliases: vec![],
///     description: "Test command".to_string(),
///     required: false,
///     arguments: vec![],
///     options: vec![
///         OptionDefinition {
///             name: "verbose".to_string(),
///             short: Some("v".to_string()),
///             long: Some("verbose".to_string()),
///             option_type: ArgumentType::Bool,
///             required: false,
///             default: Some("false".to_string()),
///             description: "Verbose output".to_string(),
///             choices: vec![],
///             repeatable: false,
///             option_parameters: HashMap::new(),
///         }
///     ],
///     implementation: "handler".to_string(),
/// };
///
/// let parser = CliParser::new(&definition);
/// let args = vec!["-v".to_string()];
/// let parsed = parser.parse(&args).unwrap();
///
/// assert_eq!(parsed.get("verbose"), Some(&"true".to_string()));
/// ```
pub struct CliParser<'a> {
    /// The command definition that specifies expected arguments and options
    definition: &'a CommandDefinition,
}

/// A single occurrence of a repeatable option
///
/// Produced when a `repeatable: true` option is encountered on the
/// command line: `--output csv file=results.csv resolution=100` becomes
/// `OptionOccurrence { discriminant: "csv", params: {"file": "results.csv",
/// "resolution": "100"} }`.
///
/// `params` values are stored as strings after type validation against
/// `option_parameters[discriminant]`, consistent with how scalar option
/// and argument values are stored (see [`ParsedValue::Scalar`]).
#[derive(Debug, Clone, PartialEq)]
pub struct OptionOccurrence {
    /// The token immediately following the flag, validated against the
    /// option's `choices`.
    pub discriminant: String,
    /// The `key=value` pairs supplied for this occurrence.
    pub params: HashMap<String, String>,
}

/// The value parsed for a single positional argument or option
///
/// [`CliParser::parse_typed`] returns `HashMap<String, ParsedValue>` so
/// that repeatable options (which may occur zero or more times, each
/// with their own sub-parameters) and plain scalar values can coexist in
/// a single result map. [`CliParser::parse`] remains additive and
/// unaffected — see its docs for how the two relate.
#[derive(Debug, Clone, PartialEq)]
pub enum ParsedValue {
    /// A plain positional argument or non-repeatable option value.
    Scalar(String),
    /// Every occurrence of a repeatable option, in command-line order.
    Repeated(Vec<OptionOccurrence>),
}

impl<'a> CliParser<'a> {
    /// Create a new CLI parser for the given command definition
    ///
    /// # Arguments
    ///
    /// * `definition` - The command definition specifying expected arguments
    ///
    /// # Example
    ///
    /// ```
    /// use dynamic_cli::parser::cli_parser::CliParser;
    /// use dynamic_cli::config::schema::CommandDefinition;
    ///
    /// # let definition = CommandDefinition {
    /// #     name: "test".to_string(),
    /// #     aliases: vec![],
    /// #     description: "".to_string(),
    /// #     required: false,
    /// #     arguments: vec![],
    /// #     options: vec![],
    /// #     implementation: "".to_string(),
    /// # };
    /// let parser = CliParser::new(&definition);
    /// ```
    pub fn new(definition: &'a CommandDefinition) -> Self {
        Self { definition }
    }

    /// Parse command-line arguments into a HashMap of strings
    ///
    /// Thin, non-breaking wrapper around [`Self::parse_typed`] for callers
    /// that only deal in scalar values. Any [`ParsedValue::Repeated`] entry
    /// (i.e. any `repeatable: true` option) is silently dropped from the
    /// result — no command definition predating DD-024 can have one, so
    /// existing callers see no behaviour change. Once the dispatch layer
    /// is migrated to consume [`crate::parser::ParsedArgs`] directly
    /// (#39, in progress — the type exists but `interface/cli.rs` and
    /// `interface/repl.rs` still call this method, not `parse_typed`),
    /// this method can be removed.
    ///
    /// # Arguments
    ///
    /// * `args` - Slice of argument strings (excluding the command name)
    ///
    /// # Returns
    ///
    /// A HashMap mapping argument/option names to their string values.
    /// All values are stored as strings after type validation.
    ///
    /// # Errors
    ///
    /// - [`ParseError::MissingArgument`] if required arguments are missing
    /// - [`ParseError::MissingOption`] if required options are missing
    /// - [`ParseError::UnknownOption`] if an unrecognized option is provided
    /// - [`ParseError::TypeParseError`] if a value cannot be converted to its expected type
    /// - [`ParseError::TooManyArguments`] if more positional arguments than expected
    ///
    /// # Example
    ///
    /// ```
    /// use dynamic_cli::parser::cli_parser::CliParser;
    /// use dynamic_cli::config::schema::{
    ///     CommandDefinition, ArgumentDefinition, ArgumentType
    /// };
    ///
    /// let definition = CommandDefinition {
    ///     name: "greet".to_string(),
    ///     aliases: vec![],
    ///     description: "Greet someone".to_string(),
    ///     required: false,
    ///     arguments: vec![
    ///         ArgumentDefinition {
    ///             name: "name".to_string(),
    ///             arg_type: ArgumentType::String,
    ///             required: true,
    ///             description: "Name".to_string(),
    ///             validation: vec![],
    ///             secure: false,
    ///         }
    ///     ],
    ///     options: vec![],
    ///     implementation: "handler".to_string(),
    /// };
    ///
    /// let parser = CliParser::new(&definition);
    /// let result = parser.parse(&["Alice".to_string()]).unwrap();
    /// assert_eq!(result.get("name"), Some(&"Alice".to_string()));
    /// ```
    pub fn parse(&self, args: &[String]) -> Result<HashMap<String, String>> {
        let typed = self.parse_typed(args)?;

        Ok(typed
            .into_iter()
            .filter_map(|(name, value)| match value {
                ParsedValue::Scalar(s) => Some((name, s)),
                ParsedValue::Repeated(_) => None,
            })
            .collect())
    }

    /// Parse command-line arguments into a HashMap of [`ParsedValue`]
    ///
    /// Like [`Self::parse`], but preserves repeatable options as
    /// [`ParsedValue::Repeated`] instead of dropping them. This is the
    /// method that actually implements DD-024 parsing; `parse()` is a
    /// filtering wrapper around it.
    ///
    /// # Arguments
    ///
    /// * `args` - Slice of argument strings (excluding the command name)
    ///
    /// # Errors
    ///
    /// In addition to the errors documented on [`Self::parse`]:
    /// - [`ParseError::UnknownDiscriminant`] if the token following a
    ///   repeatable option's flag is not in that option's `choices`
    /// - [`ParseError::UnknownOptionParameter`] if a `key=value` pair uses
    ///   a key not declared in `option_parameters[discriminant]`
    /// - [`ParseError::MissingRequiredOptionParameter`] if a required key
    ///   is absent from an occurrence
    /// - [`ParseError::DuplicateOptionOccurrence`] if the same
    ///   discriminant is supplied twice with identical `key=value` pairs
    pub fn parse_typed(&self, args: &[String]) -> Result<HashMap<String, ParsedValue>> {
        let mut result = HashMap::new();
        let mut positional_index = 0;
        let mut i = 0;

        // Parse arguments
        while i < args.len() {
            let arg = &args[i];

            if arg.starts_with("--") {
                // Long option
                self.parse_long_option(arg, args, &mut i, &mut result)?;
            } else if arg.starts_with('-') && arg.len() > 1 {
                // Short option (ensure it's not just a negative number)
                if arg
                    .chars()
                    .nth(1)
                    .map(|c| c.is_ascii_digit())
                    .unwrap_or(false)
                {
                    // This is a negative number, treat as positional
                    self.parse_positional_argument(arg, positional_index, &mut result)?;
                    positional_index += 1;
                } else {
                    self.parse_short_option(arg, args, &mut i, &mut result)?;
                }
            } else {
                // Positional argument
                self.parse_positional_argument(arg, positional_index, &mut result)?;
                positional_index += 1;
            }

            i += 1;
        }

        // Apply defaults for missing optional options
        self.apply_defaults(&mut result)?;

        // Validate all required arguments are present
        self.validate_required_arguments(&result)?;
        self.validate_required_options(&result)?;

        Ok(result)
    }

    /// Parse a long option (--option or --option=value)
    fn parse_long_option(
        &self,
        arg: &str,
        args: &[String],
        index: &mut usize,
        result: &mut HashMap<String, ParsedValue>,
    ) -> Result<()> {
        let arg_without_dashes = &arg[2..];

        // Check for --option=value format
        if let Some(eq_pos) = arg_without_dashes.find('=') {
            let option_name = &arg_without_dashes[..eq_pos];
            let value = &arg_without_dashes[eq_pos + 1..];

            let option = self.find_option_by_long(option_name)?;
            if option.repeatable {
                // A repeatable option's discriminant/params are never
                // attached via `=` — only the space-separated form is
                // supported (see parse_repeatable_occurrence).
                return Err(ParseError::InvalidSyntax {
                    details: format!(
                        "Option --{} is repeatable and does not support --{}=<value>",
                        option.name, option.name
                    ),
                    hint: Some(format!(
                        "Usage: --{} <{}> [key=value ...]",
                        option.name,
                        option.choices.join("|")
                    )),
                }
                .into());
            }
            let parsed_value = type_parser::parse_value(value, option.option_type)?;
            result.insert(option.name.clone(), ParsedValue::Scalar(parsed_value));
        } else {
            // --option format (value might be next arg)
            let option = self.find_option_by_long(arg_without_dashes)?;

            if option.repeatable {
                self.parse_repeatable_occurrence(option, args, index, result)?;
            } else if matches!(
                option.option_type,
                crate::config::schema::ArgumentType::Bool
            ) {
                result.insert(option.name.clone(), ParsedValue::Scalar("true".to_string()));
            } else {
                // Non-boolean: expect value in next argument
                *index += 1;
                if *index >= args.len() {
                    return Err(ParseError::InvalidSyntax {
                        details: format!(
                            "Option --{} requires a value",
                            option.long.as_ref().unwrap()
                        ),
                        hint: Some(format!(
                            "Usage: --{}=<value> or --{} <value>",
                            option.long.as_ref().unwrap(),
                            option.long.as_ref().unwrap()
                        )),
                    }
                    .into());
                }

                let value = &args[*index];
                let parsed_value = type_parser::parse_value(value, option.option_type)?;
                result.insert(option.name.clone(), ParsedValue::Scalar(parsed_value));
            }
        }

        Ok(())
    }

    /// Parse a short option (-o or -o value)
    fn parse_short_option(
        &self,
        arg: &str,
        args: &[String],
        index: &mut usize,
        result: &mut HashMap<String, ParsedValue>,
    ) -> Result<()> {
        let short_flag = &arg[1..2];
        let option = self.find_option_by_short(short_flag)?;

        if option.repeatable {
            if arg.len() > 2 {
                return Err(ParseError::InvalidSyntax {
                    details: format!(
                        "Option -{} is repeatable and does not support an attached value",
                        short_flag
                    ),
                    hint: Some(format!(
                        "Usage: -{} <{}> [key=value ...]",
                        short_flag,
                        option.choices.join("|")
                    )),
                }
                .into());
            }
            self.parse_repeatable_occurrence(option, args, index, result)?;
        } else if matches!(
            option.option_type,
            crate::config::schema::ArgumentType::Bool
        ) {
            result.insert(option.name.clone(), ParsedValue::Scalar("true".to_string()));
        } else {
            // Check if value is attached (e.g., -ovalue)
            if arg.len() > 2 {
                let value = &arg[2..];
                let parsed_value = type_parser::parse_value(value, option.option_type)?;
                result.insert(option.name.clone(), ParsedValue::Scalar(parsed_value));
            } else {
                // Value is next argument
                *index += 1;
                if *index >= args.len() {
                    return Err(ParseError::InvalidSyntax {
                        details: format!("Option -{} requires a value", short_flag),
                        hint: Some(format!(
                            "Usage: -{}<value> or -{} <value>",
                            short_flag, short_flag
                        )),
                    }
                    .into());
                }

                let value = &args[*index];
                let parsed_value = type_parser::parse_value(value, option.option_type)?;
                result.insert(option.name.clone(), ParsedValue::Scalar(parsed_value));
            }
        }

        Ok(())
    }

    /// Parse a positional argument
    fn parse_positional_argument(
        &self,
        value: &str,
        index: usize,
        result: &mut HashMap<String, ParsedValue>,
    ) -> Result<()> {
        if index >= self.definition.arguments.len() {
            return Err(ParseError::too_many_arguments(
                &self.definition.name,
                self.definition.arguments.len(),
                index + 1,
            )
            .into());
        }

        let arg_def = &self.definition.arguments[index];
        let parsed_value = type_parser::parse_value(value, arg_def.arg_type)?;
        result.insert(arg_def.name.clone(), ParsedValue::Scalar(parsed_value));

        Ok(())
    }

    /// Parse one occurrence of a repeatable option
    ///
    /// On entry, `index` points at the option's flag token. Reads the
    /// discriminant token immediately following it, validates it against
    /// `option.choices`, then consumes `key=value` tokens until the next
    /// flag (any token starting with `-`) or the end of input — a
    /// `key=value` pair can never start with `-` itself, so this is
    /// unambiguous, unlike the top-level positional/negative-number case.
    ///
    /// On return, `index` points at the last token consumed (the
    /// discriminant if no parameters followed, or the last `key=value`
    /// token), matching the convention already used by
    /// [`Self::parse_long_option`] / [`Self::parse_short_option`] — the
    /// caller's own `i += 1` advances past it.
    fn parse_repeatable_occurrence(
        &self,
        option: &OptionDefinition,
        args: &[String],
        index: &mut usize,
        result: &mut HashMap<String, ParsedValue>,
    ) -> Result<()> {
        // Read the discriminant token.
        *index += 1;
        if *index >= args.len() {
            return Err(ParseError::InvalidSyntax {
                details: format!("Option --{} requires a discriminant", option.name),
                hint: Some(format!(
                    "Usage: --{} <{}> [key=value ...]",
                    option.name,
                    option.choices.join("|")
                )),
            }
            .into());
        }
        let discriminant = args[*index].clone();
        if !option.choices.contains(&discriminant) {
            return Err(ParseError::UnknownDiscriminant {
                option: option.name.clone(),
                value: discriminant,
                valid_choices: option.choices.clone(),
                suggestion: Some(format!(
                    "Run --help {} to see valid --{} kinds.",
                    self.definition.name, option.name
                )),
            }
            .into());
        }

        // Guaranteed present by validate_options() (#36) once
        // repeatable/choices/option_parameters consistency has been
        // validated at config-load time.
        let empty: Vec<ArgumentDefinition> = Vec::new();
        let param_defs = option
            .option_parameters
            .get(&discriminant)
            .unwrap_or(&empty);

        // Consume key=value tokens until the next flag or end of input.
        let mut params: HashMap<String, String> = HashMap::new();
        while *index + 1 < args.len() {
            let next = &args[*index + 1];
            if next.starts_with('-') {
                break;
            }

            let eq_pos = match next.find('=') {
                Some(pos) => pos,
                None => {
                    return Err(ParseError::InvalidSyntax {
                        details: format!(
                            "Expected key=value for --{} {}, got: '{}'",
                            option.name, discriminant, next
                        ),
                        hint: Some("Sub-parameters must use the key=value form.".to_string()),
                    }
                    .into());
                }
            };
            let key = &next[..eq_pos];
            let value = &next[eq_pos + 1..];

            let arg_def = param_defs.iter().find(|a| a.name == key).ok_or_else(|| {
                ParseError::UnknownOptionParameter {
                    option: option.name.clone(),
                    discriminant: discriminant.clone(),
                    key: key.to_string(),
                    valid_keys: param_defs.iter().map(|a| a.name.clone()).collect(),
                    suggestion: Some(format!(
                        "Run --help {} to see valid keys for --{} {}.",
                        self.definition.name, option.name, discriminant
                    )),
                }
            })?;

            let typed_value = type_parser::parse_value(value, arg_def.arg_type)?;
            params.insert(key.to_string(), typed_value);

            *index += 1;
        }

        // Validate required keys are present.
        for arg_def in param_defs {
            if arg_def.required && !params.contains_key(&arg_def.name) {
                return Err(ParseError::MissingRequiredOptionParameter {
                    option: option.name.clone(),
                    discriminant: discriminant.clone(),
                    key: arg_def.name.clone(),
                    suggestion: Some(format!(
                        "Run --help {} to see required keys for --{} {}.",
                        self.definition.name, option.name, discriminant
                    )),
                }
                .into());
            }
        }

        let occurrence = OptionOccurrence {
            discriminant: discriminant.clone(),
            params,
        };

        match result
            .entry(option.name.clone())
            .or_insert_with(|| ParsedValue::Repeated(Vec::new()))
        {
            ParsedValue::Repeated(occurrences) => {
                if occurrences.contains(&occurrence) {
                    return Err(ParseError::DuplicateOptionOccurrence {
                        option: option.name.clone(),
                        discriminant: occurrence.discriminant.clone(),
                        params: occurrence.params.clone().into_iter().collect(),
                        suggestion: Some(format!(
                            "Remove one of the two identical --{} {} occurrences.",
                            option.name, discriminant
                        )),
                    }
                    .into());
                }
                occurrences.push(occurrence);
            }
            ParsedValue::Scalar(_) => unreachable!(
                "option '{}' marked repeatable cannot already hold a Scalar value",
                option.name
            ),
        }

        Ok(())
    }

    /// Apply default values for options not provided
    fn apply_defaults(&self, result: &mut HashMap<String, ParsedValue>) -> Result<()> {
        for option in &self.definition.options {
            if !result.contains_key(&option.name) {
                if let Some(ref default) = option.default {
                    // Validate the default value
                    let parsed_default = type_parser::parse_value(default, option.option_type)?;
                    result.insert(option.name.clone(), ParsedValue::Scalar(parsed_default));
                }
            }
        }
        Ok(())
    }

    /// Validate that all required arguments are present
    fn validate_required_arguments(&self, result: &HashMap<String, ParsedValue>) -> Result<()> {
        for arg in &self.definition.arguments {
            if arg.required && !result.contains_key(&arg.name) {
                return Err(ParseError::missing_argument(&arg.name, &self.definition.name).into());
            }
        }
        Ok(())
    }

    /// Validate that all required options are present
    fn validate_required_options(&self, result: &HashMap<String, ParsedValue>) -> Result<()> {
        for option in &self.definition.options {
            if option.required && !result.contains_key(&option.name) {
                return Err(ParseError::missing_option(
                    &option
                        .long
                        .clone()
                        .or(option.short.clone())
                        .unwrap_or_default(),
                    &self.definition.name,
                )
                .into());
            }
        }
        Ok(())
    }

    /// Find an option by its long form
    fn find_option_by_long(&self, long: &str) -> Result<&OptionDefinition> {
        self.definition
            .options
            .iter()
            .find(|opt| opt.long.as_deref() == Some(long))
            .ok_or_else(|| {
                let available: Vec<String> = self
                    .definition
                    .options
                    .iter()
                    .filter_map(|o| o.long.clone())
                    .collect();
                ParseError::unknown_option_with_suggestions(
                    &format!("--{}", long),
                    &self.definition.name,
                    &available,
                )
                .into()
            })
    }

    /// Find an option by its short form
    fn find_option_by_short(&self, short: &str) -> Result<&OptionDefinition> {
        self.definition
            .options
            .iter()
            .find(|opt| opt.short.as_deref() == Some(short))
            .ok_or_else(|| {
                let available: Vec<String> = self
                    .definition
                    .options
                    .iter()
                    .filter_map(|o| o.short.clone())
                    .collect();
                ParseError::unknown_option_with_suggestions(
                    &format!("-{}", short),
                    &self.definition.name,
                    &available,
                )
                .into()
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::schema::{ArgumentType, OptionDefinition};

    /// Helper to create a test command definition
    fn create_test_definition() -> CommandDefinition {
        CommandDefinition {
            name: "test".to_string(),
            aliases: vec![],
            description: "Test command".to_string(),
            required: false,
            arguments: vec![
                ArgumentDefinition {
                    name: "input".to_string(),
                    arg_type: ArgumentType::Path,
                    required: true,
                    description: "Input file".to_string(),
                    validation: vec![],
                    secure: false,
                },
                ArgumentDefinition {
                    name: "output".to_string(),
                    arg_type: ArgumentType::Path,
                    required: false,
                    description: "Output file".to_string(),
                    validation: vec![],
                    secure: false,
                },
            ],
            options: vec![
                OptionDefinition {
                    name: "verbose".to_string(),
                    short: Some("v".to_string()),
                    long: Some("verbose".to_string()),
                    option_type: ArgumentType::Bool,
                    required: false,
                    default: Some("false".to_string()),
                    description: "Verbose output".to_string(),
                    choices: vec![],
                    repeatable: false,
                    option_parameters: HashMap::new(),
                },
                OptionDefinition {
                    name: "count".to_string(),
                    short: Some("c".to_string()),
                    long: Some("count".to_string()),
                    option_type: ArgumentType::Integer,
                    required: false,
                    default: Some("10".to_string()),
                    description: "Count".to_string(),
                    choices: vec![],
                    repeatable: false,
                    option_parameters: HashMap::new(),
                },
            ],
            implementation: "handler".to_string(),
        }
    }

    // ========================================================================
    // Positional arguments tests
    // ========================================================================

    #[test]
    fn test_parse_single_positional_argument() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"input.txt".to_string()));
    }

    #[test]
    fn test_parse_multiple_positional_arguments() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "output.txt".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"input.txt".to_string()));
        assert_eq!(result.get("output"), Some(&"output.txt".to_string()));
    }

    #[test]
    fn test_parse_missing_required_argument() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args: Vec<String> = vec![];
        let result = parser.parse(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::MissingArgument {
                argument, ..
            }) => {
                assert_eq!(argument, "input");
            }
            other => panic!("Expected MissingArgument error, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_too_many_positional_arguments() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "input.txt".to_string(),
            "output.txt".to_string(),
            "extra.txt".to_string(),
        ];
        let result = parser.parse(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::TooManyArguments { .. }) => {}
            other => panic!("Expected TooManyArguments error, got {:?}", other),
        }
    }

    // ========================================================================
    // Long options tests
    // ========================================================================

    #[test]
    fn test_parse_long_boolean_option() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "--verbose".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
    }

    #[test]
    fn test_parse_long_option_with_equals() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "--count=42".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("count"), Some(&"42".to_string()));
    }

    #[test]
    fn test_parse_long_option_with_space() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "input.txt".to_string(),
            "--count".to_string(),
            "42".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("count"), Some(&"42".to_string()));
    }

    #[test]
    fn test_parse_unknown_long_option() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "--unknown".to_string()];
        let result = parser.parse(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::UnknownOption { .. }) => {}
            other => panic!("Expected UnknownOption error, got {:?}", other),
        }
    }

    // ========================================================================
    // Short options tests
    // ========================================================================

    #[test]
    fn test_parse_short_boolean_option() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "-v".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
    }

    #[test]
    fn test_parse_short_option_with_space() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "-c".to_string(), "42".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("count"), Some(&"42".to_string()));
    }

    #[test]
    fn test_parse_short_option_attached_value() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string(), "-c42".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("count"), Some(&"42".to_string()));
    }

    #[test]
    fn test_parse_negative_number_as_positional() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        // -123 should be treated as a positional argument, not an option
        let args = vec!["-123".to_string()];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"-123".to_string()));
    }

    // ========================================================================
    // Default values tests
    // ========================================================================

    #[test]
    fn test_apply_default_values() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec!["input.txt".to_string()];
        let result = parser.parse(&args).unwrap();

        // Default values should be applied
        assert_eq!(result.get("verbose"), Some(&"false".to_string()));
        assert_eq!(result.get("count"), Some(&"10".to_string()));
    }

    #[test]
    fn test_override_default_values() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "input.txt".to_string(),
            "-v".to_string(),
            "-c".to_string(),
            "5".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        // Provided values should override defaults
        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
        assert_eq!(result.get("count"), Some(&"5".to_string()));
    }

    // ========================================================================
    // Type conversion tests
    // ========================================================================

    #[test]
    fn test_type_conversion_error() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        // "abc" cannot be parsed as integer
        let args = vec![
            "input.txt".to_string(),
            "--count".to_string(),
            "abc".to_string(),
        ];
        let result = parser.parse(&args);

        assert!(result.is_err());
    }

    // ========================================================================
    // Integration tests
    // ========================================================================

    #[test]
    fn test_parse_complex_command_line() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "input.txt".to_string(),
            "output.txt".to_string(),
            "--verbose".to_string(),
            "--count=100".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"input.txt".to_string()));
        assert_eq!(result.get("output"), Some(&"output.txt".to_string()));
        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
        assert_eq!(result.get("count"), Some(&"100".to_string()));
    }

    #[test]
    fn test_parse_mixed_options_and_arguments() {
        let definition = create_test_definition();
        let parser = CliParser::new(&definition);

        // Options can be interspersed with positional arguments
        let args = vec![
            "--verbose".to_string(),
            "input.txt".to_string(),
            "-c".to_string(),
            "50".to_string(),
            "output.txt".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("input"), Some(&"input.txt".to_string()));
        assert_eq!(result.get("output"), Some(&"output.txt".to_string()));
        assert_eq!(result.get("verbose"), Some(&"true".to_string()));
        assert_eq!(result.get("count"), Some(&"50".to_string()));
    }

    // ========================================================================
    // DD-024: repeatable options with option_parameters (#38)
    // ========================================================================

    /// Helper: a command with a repeatable `--output` option, mirroring
    /// the chrom-rs motivating example (csv with an optional resolution,
    /// plot with just a file).
    fn create_repeatable_test_definition() -> CommandDefinition {
        let mut option_parameters = HashMap::new();
        option_parameters.insert(
            "csv".to_string(),
            vec![
                ArgumentDefinition {
                    name: "file".to_string(),
                    arg_type: ArgumentType::Path,
                    required: true,
                    description: "Destination CSV file".to_string(),
                    validation: vec![],
                    secure: false,
                },
                ArgumentDefinition {
                    name: "resolution".to_string(),
                    arg_type: ArgumentType::Integer,
                    required: false,
                    description: "Time-step resolution".to_string(),
                    validation: vec![],
                    secure: false,
                },
            ],
        );
        option_parameters.insert(
            "plot".to_string(),
            vec![ArgumentDefinition {
                name: "file".to_string(),
                arg_type: ArgumentType::Path,
                required: true,
                description: "Destination image file".to_string(),
                validation: vec![],
                secure: false,
            }],
        );

        CommandDefinition {
            name: "export".to_string(),
            aliases: vec![],
            description: "Export simulation results".to_string(),
            required: false,
            arguments: vec![],
            options: vec![OptionDefinition {
                name: "output".to_string(),
                short: None,
                long: Some("output".to_string()),
                option_type: ArgumentType::String,
                required: false,
                default: None,
                description: "Write results in one or more output kinds".to_string(),
                choices: vec!["csv".to_string(), "plot".to_string()],
                repeatable: true,
                option_parameters,
            }],
            implementation: "export_handler".to_string(),
        }
    }

    #[test]
    fn test_parse_repeatable_option_single_occurrence() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=results.csv".to_string(),
        ];
        let result = parser.parse_typed(&args).unwrap();

        match result.get("output") {
            Some(ParsedValue::Repeated(occurrences)) => {
                assert_eq!(occurrences.len(), 1);
                assert_eq!(occurrences[0].discriminant, "csv");
                assert_eq!(
                    occurrences[0].params.get("file"),
                    Some(&"results.csv".to_string())
                );
            }
            other => panic!("Expected Repeated([csv]), got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_optional_param_can_be_omitted() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=results.csv".to_string(),
        ];
        let result = parser.parse_typed(&args).unwrap();

        match result.get("output") {
            Some(ParsedValue::Repeated(occurrences)) => {
                assert_eq!(occurrences[0].params.get("resolution"), None);
            }
            other => panic!("Expected Repeated([csv]), got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_with_optional_param_provided() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=results.csv".to_string(),
            "resolution=100".to_string(),
        ];
        let result = parser.parse_typed(&args).unwrap();

        match result.get("output") {
            Some(ParsedValue::Repeated(occurrences)) => {
                assert_eq!(
                    occurrences[0].params.get("resolution"),
                    Some(&"100".to_string())
                );
            }
            other => panic!("Expected Repeated([csv]), got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_multiple_discriminants_both_parse() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=results.csv".to_string(),
            "--output".to_string(),
            "plot".to_string(),
            "file=chart.png".to_string(),
        ];
        let result = parser.parse_typed(&args).unwrap();

        match result.get("output") {
            Some(ParsedValue::Repeated(occurrences)) => {
                assert_eq!(occurrences.len(), 2);
                assert_eq!(occurrences[0].discriminant, "csv");
                assert_eq!(occurrences[1].discriminant, "plot");
            }
            other => panic!("Expected Repeated([csv, plot]), got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_same_discriminant_different_params_both_kept() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=a.csv".to_string(),
            "--output".to_string(),
            "csv".to_string(),
            "file=b.csv".to_string(),
            "resolution=50".to_string(),
        ];
        let result = parser.parse_typed(&args).unwrap();

        match result.get("output") {
            Some(ParsedValue::Repeated(occurrences)) => {
                assert_eq!(occurrences.len(), 2);
            }
            other => panic!("Expected Repeated([csv, csv]), got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_duplicate_occurrence_errors() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=a.csv".to_string(),
            "--output".to_string(),
            "csv".to_string(),
            "file=a.csv".to_string(),
        ];
        let result = parser.parse_typed(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::DuplicateOptionOccurrence {
                ..
            }) => {}
            other => panic!("Expected DuplicateOptionOccurrence error, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_missing_required_param_errors() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        // "file" is required for the csv discriminant and is not supplied.
        let args = vec!["--output".to_string(), "csv".to_string()];
        let result = parser.parse_typed(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::MissingRequiredOptionParameter {
                key,
                ..
            }) => {
                assert_eq!(key, "file");
            }
            other => panic!(
                "Expected MissingRequiredOptionParameter error, got {:?}",
                other
            ),
        }
    }

    #[test]
    fn test_parse_repeatable_option_unknown_param_key_errors() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=a.csv".to_string(),
            "compression=gzip".to_string(),
        ];
        let result = parser.parse_typed(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::UnknownOptionParameter {
                key,
                ..
            }) => {
                assert_eq!(key, "compression");
            }
            other => panic!("Expected UnknownOptionParameter error, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_repeatable_option_unknown_discriminant_errors() {
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "xml".to_string(),
            "file=a.xml".to_string(),
        ];
        let result = parser.parse_typed(&args);

        assert!(result.is_err());
        match result.unwrap_err() {
            crate::error::DynamicCliError::Parse(ParseError::UnknownDiscriminant {
                value, ..
            }) => {
                assert_eq!(value, "xml");
            }
            other => panic!("Expected UnknownDiscriminant error, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_legacy_drops_repeated_values() {
        // parse() (Option A design: non-breaking wrapper) must keep
        // working for definitions with no repeatable options — and
        // silently drop Repeated entries rather than erroring, since no
        // pre-DD-024 caller can represent them anyway.
        let definition = create_repeatable_test_definition();
        let parser = CliParser::new(&definition);

        let args = vec![
            "--output".to_string(),
            "csv".to_string(),
            "file=a.csv".to_string(),
        ];
        let result = parser.parse(&args).unwrap();

        assert_eq!(result.get("output"), None);
    }
}