zfish 0.1.10

Ultra-light, zero-dependency Rust CLI framework for building beautiful command-line applications
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
//! # Command and Subcommand System
//!
//! This module provides a production-grade subcommand system for building complex CLI applications.
//! It supports subcommands (like `git commit`, `cargo build`), positional and flag arguments,
//! automatic help generation, validation, and advanced features like environment variable fallbacks.
//!
//! ## Features (v0.2.1)
//!
//! - **Subcommands**: Multi-level command hierarchies with aliases
//! - **Positional Arguments**: Required and optional positional args (`<FILE>`, `[OUTPUT]`)
//! - **Variadic Arguments**: Capture multiple values (`[FILES]...`)
//! - **Flags & Options**: Short/long flags with values (`-v`, `--verbose`, `--output file.txt`)
//! - **Environment Variables**: Automatic fallback to env vars (`.env("APP_CONFIG")`)
//! - **Value Delimiters**: Parse comma-separated values (`--tags rust,cli,tool`)
//! - **Argument Dependencies**: Require other arguments (`.requires("format")`)
//! - **Conflict Detection**: Prevent incompatible args (`.conflicts_with("quiet")`)
//! - **Argument Groups**: Mutually exclusive argument sets
//! - **Auto Help**: Automatic `--help` generation for all commands
//! - **Validation**: Required arguments, possible values, custom validators
//! - **Error Handling**: Detailed error messages for debugging
//! - **Cross-Platform**: Works on Linux, macOS, Windows
//!
//! ## Example
//!
//! ```rust
//! use zfish::command::{App, Command, Arg};
//!
//! let app = App::new("myapp")
//!     .version("1.0.0")
//!     .about("My awesome CLI")
//!     .arg(Arg::new("verbose").short('v').long("verbose"))
//!     .subcommand(
//!         Command::new("init")
//!             .about("Initialize a new project")
//!             .arg(Arg::new("name").required(true))
//!     )
//!     .subcommand(
//!         Command::new("build")
//!             .about("Build the project")
//!             .arg(Arg::new("release").long("release"))
//!     );
//!
//! let matches = app.get_matches();
//!
//! match matches.subcommand() {
//!     Some(("init", sub_matches)) => {
//!         let name = sub_matches.value_of("name").unwrap();
//!         println!("Initializing project: {}", name);
//!     }
//!     Some(("build", sub_matches)) => {
//!         if sub_matches.is_present("release") {
//!             println!("Building in release mode");
//!         }
//!     }
//!     _ => println!("No subcommand provided"),
//! }
//! ```

use std::collections::HashMap;
use std::fmt;

/// Represents a parsed command-line argument value
#[derive(Debug, Clone, PartialEq)]
pub enum ArgValue {
    /// A single string value
    Single(String),
    /// Multiple string values (for repeated arguments)
    Multiple(Vec<String>),
    /// A flag (present/absent)
    Flag(bool),
}

impl ArgValue {
    /// Returns the value as a string reference, if it's a single value
    pub fn as_str(&self) -> Option<&str> {
        match self {
            ArgValue::Single(s) => Some(s),
            _ => None,
        }
    }

    /// Returns the value as a boolean, if it's a flag
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            ArgValue::Flag(b) => Some(*b),
            _ => None,
        }
    }

    /// Returns the values as a slice, if multiple
    pub fn as_vec(&self) -> Option<&[String]> {
        match self {
            ArgValue::Multiple(v) => Some(v),
            _ => None,
        }
    }
}

/// Errors that can occur during command parsing
#[derive(Debug, Clone, PartialEq)]
pub enum CommandError {
    /// An argument is missing
    MissingArgument(String),
    /// An unknown argument was provided
    UnknownArgument(String),
    /// An unknown subcommand was provided
    UnknownSubcommand(String),
    /// An argument validation failed
    ValidationError(String, String), // (arg_name, error_message)
    /// Invalid value for an argument
    InvalidValue(String, String), // (arg_name, value)
    /// Help was requested
    HelpRequested,
    /// Version was requested
    VersionRequested,
    /// Arguments conflict with each other
    ArgumentConflict(String, String), // (arg1, arg2)
    /// Required dependency is missing
    MissingDependency(String, String), // (arg, required_arg)
}

impl fmt::Display for CommandError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CommandError::MissingArgument(name) => {
                write!(f, "error: the argument '{}' is required", name)
            }
            CommandError::UnknownArgument(name) => {
                write!(f, "error: unknown argument '{}'", name)
            }
            CommandError::UnknownSubcommand(name) => {
                write!(f, "error: unknown subcommand '{}'", name)
            }
            CommandError::ValidationError(name, msg) => {
                write!(f, "error: validation failed for '{}': {}", name, msg)
            }
            CommandError::InvalidValue(name, value) => {
                write!(f, "error: invalid value '{}' for '{}'", value, name)
            }
            CommandError::HelpRequested => write!(f, "help requested"),
            CommandError::VersionRequested => write!(f, "version requested"),
            CommandError::ArgumentConflict(arg1, arg2) => {
                write!(
                    f,
                    "error: the argument '{}' cannot be used with '{}'",
                    arg1, arg2
                )
            }
            CommandError::MissingDependency(arg, required) => {
                write!(f, "error: the argument '{}' requires '{}'", arg, required)
            }
        }
    }
}

impl std::error::Error for CommandError {}

/// Result type for command operations
pub type CommandResult<T> = Result<T, CommandError>;

/// Represents a group of mutually exclusive arguments
#[derive(Debug, Clone)]
pub struct ArgGroup {
    name: String,
    args: Vec<String>,
    required: bool,
}

impl ArgGroup {
    /// Creates a new argument group
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            args: Vec::new(),
            required: false,
        }
    }

    /// Adds an argument to this group
    pub fn arg(mut self, arg: impl Into<String>) -> Self {
        self.args.push(arg.into());
        self
    }

    /// Adds multiple arguments to this group
    pub fn args(mut self, args: &[&str]) -> Self {
        for arg in args {
            self.args.push(arg.to_string());
        }
        self
    }

    /// Makes this group required (at least one argument must be present)
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }
}

/// Represents a single command-line argument definition
#[derive(Debug, Clone)]
pub struct Arg {
    name: String,
    short: Option<char>,
    long: Option<String>,
    help: Option<String>,
    required: bool,
    takes_value: bool,
    multiple: bool,
    default_value: Option<String>,
    possible_values: Option<Vec<String>>,
    #[allow(clippy::type_complexity)]
    validator: Option<fn(&str) -> Result<(), String>>,
    // New fields for v0.2.1+
    index: Option<usize>,          // Position for positional arguments
    env: Option<String>,           // Environment variable name
    requires: Vec<String>,         // Arguments this arg depends on
    conflicts_with: Vec<String>,   // Arguments this arg conflicts with
    value_delimiter: Option<char>, // Delimiter for splitting values (e.g., ',')
    last: bool,                    // Variadic positional (FILES...)
}

impl Arg {
    /// Creates a new argument with the given name
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            short: None,
            long: None,
            help: None,
            required: false,
            takes_value: true,
            multiple: false,
            default_value: None,
            possible_values: Some(Vec::new()),
            validator: None,
            index: None,
            env: None,
            requires: Vec::new(),
            conflicts_with: Vec::new(),
            value_delimiter: None,
            last: false,
        }
    }

    /// Sets the short flag (e.g., `-v`)
    pub fn short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }

    /// Sets the long flag (e.g., `--verbose`)
    pub fn long(mut self, long: impl Into<String>) -> Self {
        self.long = Some(long.into());
        self
    }

    /// Sets the help text for this argument
    pub fn about(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    /// Marks this argument as required
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }

    /// Sets whether this argument takes a value
    pub fn takes_value(mut self, takes_value: bool) -> Self {
        self.takes_value = takes_value;
        self
    }

    /// Allows this argument to be specified multiple times
    pub fn multiple(mut self, multiple: bool) -> Self {
        self.multiple = multiple;
        self
    }

    /// Sets the default value for this argument
    pub fn default_value(mut self, value: impl Into<String>) -> Self {
        self.default_value = Some(value.into());
        self
    }

    /// Sets the possible values for this argument
    pub fn possible_values(mut self, values: &[&str]) -> Self {
        self.possible_values = Some(values.iter().map(|s| s.to_string()).collect());
        self
    }

    /// Sets a custom validator function
    pub fn validator(mut self, validator: fn(&str) -> Result<(), String>) -> Self {
        self.validator = Some(validator);
        self
    }

    /// Makes this a positional argument at the given index (0-based)
    /// Example: `Arg::new("file").index(0)` for `<FILE>`
    pub fn index(mut self, index: usize) -> Self {
        self.index = Some(index);
        self
    }

    /// Sets an environment variable to read from if the argument is not provided
    /// Example: `Arg::new("config").env("APP_CONFIG")`
    pub fn env(mut self, env: impl Into<String>) -> Self {
        self.env = Some(env.into());
        self
    }

    /// Specifies arguments that this argument requires
    /// Example: `Arg::new("output").requires("format")`
    pub fn requires(mut self, arg: impl Into<String>) -> Self {
        self.requires.push(arg.into());
        self
    }

    /// Specifies arguments that conflict with this argument
    /// Example: `Arg::new("quiet").conflicts_with("verbose")`
    pub fn conflicts_with(mut self, arg: impl Into<String>) -> Self {
        self.conflicts_with.push(arg.into());
        self
    }

    /// Sets a delimiter for parsing multiple values from a single input
    /// Example: `Arg::new("tags").value_delimiter(',')` parses "rust,cli,tool"
    pub fn value_delimiter(mut self, delimiter: char) -> Self {
        self.value_delimiter = Some(delimiter);
        self.multiple = true; // Automatically enable multiple
        self
    }

    /// Makes this a variadic positional argument (captures all remaining args)
    /// Example: `Arg::new("files").last(true)` for `[FILES]...`
    pub fn last(mut self, last: bool) -> Self {
        self.last = last;
        if last {
            self.multiple = true;
            self.index = Some(usize::MAX); // Marker for last position
        }
        self
    }

    /// Gets the name of this argument
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Checks if this argument matches a short flag
    fn matches_short(&self, c: char) -> bool {
        self.short == Some(c)
    }

    /// Validates a value against this argument's constraints
    fn validate(&self, value: &str) -> Result<(), String> {
        // Check possible values
        if let Some(ref possible) = self.possible_values
            && !possible.is_empty()
            && !possible.contains(&value.to_string())
        {
            return Err(format!(
                "invalid value '{}', expected one of: {}",
                value,
                possible.join(", ")
            ));
        }

        // Run custom validator
        if let Some(validator) = self.validator {
            validator(value)?;
        }

        Ok(())
    }
}

/// Represents the result of parsing a command
#[derive(Debug, Clone)]
pub struct ArgMatches {
    command_name: String,
    args: HashMap<String, ArgValue>,
    subcommand: Option<Box<(String, ArgMatches)>>,
}

impl ArgMatches {
    /// Creates a new empty ArgMatches
    fn new(command_name: impl Into<String>) -> Self {
        Self {
            command_name: command_name.into(),
            args: HashMap::new(),
            subcommand: None,
        }
    }

    /// Gets the name of this command
    pub fn command_name(&self) -> &str {
        &self.command_name
    }

    /// Checks if an argument is present
    pub fn is_present(&self, name: &str) -> bool {
        self.args.contains_key(name)
    }

    /// Gets the value of an argument as a string
    pub fn value_of(&self, name: &str) -> Option<&str> {
        self.args.get(name).and_then(|v| v.as_str())
    }

    /// Gets the value of an argument as a boolean
    pub fn is_flag_set(&self, name: &str) -> bool {
        self.args
            .get(name)
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    /// Gets multiple values for an argument
    pub fn values_of(&self, name: &str) -> Option<&[String]> {
        self.args.get(name).and_then(|v| v.as_vec())
    }

    /// Gets the subcommand, if any
    pub fn subcommand(&self) -> Option<(&str, &ArgMatches)> {
        self.subcommand
            .as_ref()
            .map(|boxed| (boxed.0.as_str(), &boxed.1))
    }

    /// Gets the subcommand name, if any
    pub fn subcommand_name(&self) -> Option<&str> {
        self.subcommand.as_ref().map(|boxed| boxed.0.as_str())
    }

    /// Gets the subcommand matches, if any
    pub fn subcommand_matches(&self, name: &str) -> Option<&ArgMatches> {
        self.subcommand.as_ref().and_then(|boxed| {
            if boxed.0 == name {
                Some(&boxed.1)
            } else {
                None
            }
        })
    }

    /// Inserts an argument value
    fn insert(&mut self, name: String, value: ArgValue) {
        self.args.insert(name, value);
    }

    /// Sets the subcommand
    fn set_subcommand(&mut self, name: String, matches: ArgMatches) {
        self.subcommand = Some(Box::new((name, matches)));
    }
}

/// Represents a command or subcommand
#[derive(Debug, Clone)]
pub struct Command {
    name: String,
    about: Option<String>,
    long_about: Option<String>,
    version: Option<String>,
    args: Vec<Arg>,
    subcommands: Vec<Command>,
    groups: Vec<ArgGroup>,
    aliases: Vec<String>,
}

impl Command {
    /// Creates a new command with the given name
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            about: None,
            long_about: None,
            version: None,
            args: Vec::new(),
            subcommands: Vec::new(),
            groups: Vec::new(),
            aliases: Vec::new(),
        }
    }

    /// Sets the short description for this command
    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = Some(about.into());
        self
    }

    /// Sets the long description for this command
    pub fn long_about(mut self, long_about: impl Into<String>) -> Self {
        self.long_about = Some(long_about.into());
        self
    }

    /// Sets the version for this command
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Adds an argument to this command
    pub fn arg(mut self, arg: Arg) -> Self {
        self.args.push(arg);
        self
    }

    /// Adds multiple arguments to this command
    pub fn args(mut self, args: &[Arg]) -> Self {
        self.args.extend_from_slice(args);
        self
    }

    /// Adds a subcommand to this command
    pub fn subcommand(mut self, subcommand: Command) -> Self {
        self.subcommands.push(subcommand);
        self
    }

    /// Adds multiple subcommands to this command
    pub fn subcommands(mut self, subcommands: &[Command]) -> Self {
        self.subcommands.extend_from_slice(subcommands);
        self
    }

    /// Adds an argument group to this command
    pub fn group(mut self, group: ArgGroup) -> Self {
        self.groups.push(group);
        self
    }

    /// Adds an alias for this command
    /// Example: `Command::new("build").alias("b")`
    pub fn alias(mut self, alias: impl Into<String>) -> Self {
        self.aliases.push(alias.into());
        self
    }

    /// Adds multiple aliases for this command
    pub fn aliases(mut self, aliases: &[&str]) -> Self {
        for alias in aliases {
            self.aliases.push(alias.to_string());
        }
        self
    }

    /// Gets the name of this command
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Finds an argument by name, short, or long flag
    fn find_arg(&self, identifier: &str) -> Option<&Arg> {
        self.args.iter().find(|arg| {
            arg.name == identifier
                || arg.short.map(|c| format!("{}", c)) == Some(identifier.to_string())
                || arg.long.as_deref() == Some(identifier)
        })
    }

    /// Finds a subcommand by name or alias
    fn find_subcommand(&self, name: &str) -> Option<&Command> {
        self.subcommands
            .iter()
            .find(|cmd| cmd.name == name || cmd.aliases.contains(&name.to_string()))
    }

    /// Generates help text for this command
    pub fn generate_help(&self) -> String {
        let mut help = String::new();

        self.generate_header(&mut help);
        self.generate_usage(&mut help);
        self.generate_args_section(&mut help);
        self.generate_options_section(&mut help);
        self.generate_subcommands_section(&mut help);

        help
    }

    /// Generate header section (about and version)
    fn generate_header(&self, help: &mut String) {
        if let Some(ref about) = self.about {
            help.push_str(&format!("{}\n", about));
        }

        if let Some(ref version) = self.version {
            help.push_str(&format!("\nVersion: {}\n", version));
        }
    }

    /// Generate usage line
    fn generate_usage(&self, help: &mut String) {
        help.push_str(&format!("\nUSAGE:\n    {}", self.name));

        let mut positional_args: Vec<&Arg> =
            self.args.iter().filter(|a| a.index.is_some()).collect();
        positional_args.sort_by_key(|a| a.index.unwrap());

        if self.args.iter().any(|a| a.index.is_none()) {
            help.push_str(" [OPTIONS]");
        }

        for arg in &positional_args {
            self.append_positional_usage(arg, help);
        }

        if !self.subcommands.is_empty() {
            help.push_str(" <COMMAND>");
        }

        help.push('\n');
    }

    /// Append single positional arg to usage line
    fn append_positional_usage(&self, arg: &Arg, help: &mut String) {
        if arg.last {
            help.push_str(&format!(" [{}]...", arg.name.to_uppercase()));
        } else if arg.required {
            help.push_str(&format!(" <{}>", arg.name.to_uppercase()));
        } else {
            help.push_str(&format!(" [{}]", arg.name.to_uppercase()));
        }
    }

    /// Generate ARGS section for positional arguments
    fn generate_args_section(&self, help: &mut String) {
        let mut positional_args: Vec<&Arg> =
            self.args.iter().filter(|a| a.index.is_some()).collect();
        positional_args.sort_by_key(|a| a.index.unwrap());

        if positional_args.is_empty() {
            return;
        }

        help.push_str("\nARGS:\n");
        for arg in &positional_args {
            self.format_arg_line(arg, help);
        }
    }

    /// Format a single positional argument line
    fn format_arg_line(&self, arg: &Arg, help: &mut String) {
        let mut arg_line = format!("    <{}>", arg.name.to_uppercase());

        while arg_line.len() < 30 {
            arg_line.push(' ');
        }

        if let Some(ref help_text) = arg.help {
            arg_line.push_str(help_text);
        }

        if arg.required {
            arg_line.push_str(" [required]");
        }

        help.push_str(&format!("{}\n", arg_line));
    }

    /// Generate OPTIONS section for flags and options
    fn generate_options_section(&self, help: &mut String) {
        let option_args: Vec<&Arg> = self.args.iter().filter(|a| a.index.is_none()).collect();

        if option_args.is_empty() {
            return;
        }

        help.push_str("\nOPTIONS:\n");
        for arg in &option_args {
            self.format_option_line(arg, help);
        }
    }

    /// Format a single option/flag line
    fn format_option_line(&self, arg: &Arg, help: &mut String) {
        let mut arg_line = String::from("    ");

        if let Some(short) = arg.short {
            arg_line.push_str(&format!("-{}", short));
            if arg.long.is_some() {
                arg_line.push_str(", ");
            }
        }

        if let Some(ref long) = arg.long {
            arg_line.push_str(&format!("--{}", long));
        }

        if arg.takes_value {
            arg_line.push_str(&format!(" <{}>", arg.name.to_uppercase()));
        }

        while arg_line.len() < 30 {
            arg_line.push(' ');
        }

        self.append_option_metadata(arg, &mut arg_line);
        help.push_str(&format!("{}\n", arg_line));
    }

    /// Append help text, required flag, and default value to option line
    fn append_option_metadata(&self, arg: &Arg, arg_line: &mut String) {
        if let Some(ref help_text) = arg.help {
            arg_line.push_str(help_text);
        }

        if arg.required {
            arg_line.push_str(" [required]");
        }

        if let Some(ref default) = arg.default_value {
            arg_line.push_str(&format!(" [default: {}]", default));
        }
    }

    /// Generate COMMANDS section for subcommands
    fn generate_subcommands_section(&self, help: &mut String) {
        if self.subcommands.is_empty() {
            return;
        }

        help.push_str("\nCOMMANDS:\n");
        for subcmd in &self.subcommands {
            self.format_subcommand_line(subcmd, help);
        }

        help.push_str("\nRun '<COMMAND> --help' for more information on a specific command.\n");
    }

    /// Format a single subcommand line
    fn format_subcommand_line(&self, subcmd: &Command, help: &mut String) {
        let mut cmd_line = format!("    {}", subcmd.name);

        if !subcmd.aliases.is_empty() {
            cmd_line.push_str(&format!(" ({})", subcmd.aliases.join(", ")));
        }

        while cmd_line.len() < 30 {
            cmd_line.push(' ');
        }

        if let Some(ref about) = subcmd.about {
            cmd_line.push_str(about);
        }

        help.push_str(&format!("{}\n", cmd_line));
    }

    /// Helper: Process a value with delimiter support
    fn process_value(&self, arg: &Arg, value: &str, matches: &mut ArgMatches) -> CommandResult<()> {
        if let Some(delimiter) = arg.value_delimiter {
            // Split by delimiter
            let values: Vec<String> = value
                .split(delimiter)
                .map(|s| s.trim().to_string())
                .collect();
            // Validate each value
            for v in &values {
                arg.validate(v)
                    .map_err(|err| CommandError::ValidationError(arg.name.clone(), err))?;
            }
            matches.insert(arg.name.clone(), ArgValue::Multiple(values));
        } else if arg.multiple {
            // Accumulate multiple values
            let current = matches
                .args
                .entry(arg.name.clone())
                .or_insert(ArgValue::Multiple(Vec::new()));
            if let ArgValue::Multiple(vec) = current {
                arg.validate(value)
                    .map_err(|err| CommandError::ValidationError(arg.name.clone(), err))?;
                vec.push(value.to_string());
            }
        } else {
            // Single value
            arg.validate(value)
                .map_err(|err| CommandError::ValidationError(arg.name.clone(), err))?;
            matches.insert(arg.name.clone(), ArgValue::Single(value.to_string()));
        }
        Ok(())
    }

    /// Parses command-line arguments
    fn parse_args(&self, args: &[String]) -> CommandResult<ArgMatches> {
        let mut matches = ArgMatches::new(&self.name);
        let mut positional_values: Vec<String> = Vec::new();

        self.parse_command_line(args, &mut matches, &mut positional_values)?;
        self.process_positional_args(&positional_values, &mut matches);
        self.validate_matches(&mut matches)?;

        Ok(matches)
    }

    /// Parse command line arguments (flags, options, subcommands)
    fn parse_command_line(
        &self,
        args: &[String],
        matches: &mut ArgMatches,
        positional_values: &mut Vec<String>,
    ) -> CommandResult<()> {
        let mut i = 0;

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

            // Check for special flags
            if self.handle_special_flags(arg)? {
                return Ok(());
            }

            // Check for subcommand
            if !arg.starts_with('-') {
                if let Some(subcmd) = self.find_subcommand(arg) {
                    let sub_args = &args[i + 1..];
                    let sub_matches = subcmd.parse_args(sub_args)?;
                    matches.set_subcommand(arg.clone(), sub_matches);
                    return Ok(());
                }
                positional_values.push(arg.clone());
                i += 1;
                continue;
            }

            i += self.parse_flag_or_option(arg, args, i, matches)?;
        }

        Ok(())
    }

    /// Handle special flags like --help and --version
    fn handle_special_flags(&self, arg: &str) -> CommandResult<bool> {
        if arg == "--help" || arg == "-h" {
            return Err(CommandError::HelpRequested);
        }
        if (arg == "--version" || arg == "-V") && self.version.is_some() {
            return Err(CommandError::VersionRequested);
        }
        Ok(false)
    }

    /// Parse a single flag or option and return number of args consumed
    fn parse_flag_or_option(
        &self,
        arg: &str,
        args: &[String],
        index: usize,
        matches: &mut ArgMatches,
    ) -> CommandResult<usize> {
        if arg.starts_with("--") && arg.contains('=') {
            self.parse_long_flag_with_equals(arg, matches)?;
            Ok(1)
        } else if arg.starts_with("--") {
            self.parse_long_flag(arg, args, index, matches)
        } else if arg.starts_with('-') && arg.len() > 1 {
            self.parse_short_flags(arg, args, index, matches)
        } else {
            Ok(1)
        }
    }

    /// Parse long flag with = (--flag=value)
    fn parse_long_flag_with_equals(
        &self,
        arg: &str,
        matches: &mut ArgMatches,
    ) -> CommandResult<()> {
        let parts: Vec<&str> = arg.splitn(2, '=').collect();
        let flag_name = parts[0].trim_start_matches("--");
        let value = parts[1];

        if let Some(found_arg) = self.find_arg(flag_name) {
            self.process_value(found_arg, value, matches)?;
        } else {
            return Err(CommandError::UnknownArgument(flag_name.to_string()));
        }
        Ok(())
    }

    /// Parse long flag (--flag value or --flag)
    fn parse_long_flag(
        &self,
        arg: &str,
        args: &[String],
        index: usize,
        matches: &mut ArgMatches,
    ) -> CommandResult<usize> {
        let flag_name = arg.trim_start_matches("--");
        let found_arg = self
            .find_arg(flag_name)
            .ok_or_else(|| CommandError::UnknownArgument(flag_name.to_string()))?;

        if found_arg.takes_value {
            if index + 1 < args.len() && !args[index + 1].starts_with('-') {
                self.process_value(found_arg, &args[index + 1], matches)?;
                Ok(2) // Consumed current + next
            } else if let Some(ref default) = found_arg.default_value {
                matches.insert(found_arg.name.clone(), ArgValue::Single(default.clone()));
                Ok(1)
            } else {
                Ok(1)
            }
        } else {
            matches.insert(found_arg.name.clone(), ArgValue::Flag(true));
            Ok(1)
        }
    }

    /// Parse short flag(s) (-v or -abc)
    fn parse_short_flags(
        &self,
        arg: &str,
        args: &[String],
        index: usize,
        matches: &mut ArgMatches,
    ) -> CommandResult<usize> {
        let flags = arg.trim_start_matches('-');
        let mut consumed = 1;

        for (idx, c) in flags.chars().enumerate() {
            let found_arg = self
                .args
                .iter()
                .find(|a| a.matches_short(c))
                .ok_or_else(|| CommandError::UnknownArgument(c.to_string()))?;

            if found_arg.takes_value && idx == flags.len() - 1 {
                // Last flag can take value from next arg
                if index + 1 < args.len() && !args[index + 1].starts_with('-') {
                    self.process_value(found_arg, &args[index + 1], matches)?;
                    consumed = 2;
                } else if let Some(ref default) = found_arg.default_value {
                    matches.insert(found_arg.name.clone(), ArgValue::Single(default.clone()));
                }
            } else {
                matches.insert(found_arg.name.clone(), ArgValue::Flag(true));
            }
        }

        Ok(consumed)
    }

    /// Process positional arguments
    fn process_positional_args(&self, positional_values: &[String], matches: &mut ArgMatches) {
        let mut positional_args: Vec<&Arg> =
            self.args.iter().filter(|a| a.index.is_some()).collect();
        positional_args.sort_by_key(|a| a.index.unwrap());

        for (idx, arg) in positional_args.iter().enumerate() {
            if arg.last {
                let remaining: Vec<String> = positional_values.iter().skip(idx).cloned().collect();
                if !remaining.is_empty() {
                    matches.insert(arg.name.clone(), ArgValue::Multiple(remaining));
                }
            } else if idx < positional_values.len() {
                matches.insert(
                    arg.name.clone(),
                    ArgValue::Single(positional_values[idx].clone()),
                );
            }
        }
    }

    /// Validate matches: check required args, apply defaults, check dependencies
    fn validate_matches(&self, matches: &mut ArgMatches) -> CommandResult<()> {
        self.check_required_args(matches)?;
        self.apply_defaults_and_env(matches)?;
        self.check_dependencies(matches)?;
        self.check_conflicts(matches)?;
        self.validate_groups(matches)?;
        Ok(())
    }

    /// Check for required arguments
    fn check_required_args(&self, matches: &ArgMatches) -> CommandResult<()> {
        for arg in &self.args {
            if arg.required && !matches.is_present(&arg.name) {
                return Err(CommandError::MissingArgument(arg.name.clone()));
            }
        }
        Ok(())
    }

    /// Apply default values and environment variables
    fn apply_defaults_and_env(&self, matches: &mut ArgMatches) -> CommandResult<()> {
        for arg in &self.args {
            if !matches.is_present(&arg.name) {
                if let Some(ref env_var) = arg.env
                    && let Ok(value) = std::env::var(env_var)
                {
                    matches.insert(arg.name.clone(), ArgValue::Single(value));
                    continue;
                }
                if let Some(ref default) = arg.default_value {
                    matches.insert(arg.name.clone(), ArgValue::Single(default.clone()));
                }
            }
        }
        Ok(())
    }

    /// Check argument dependencies
    fn check_dependencies(&self, matches: &ArgMatches) -> CommandResult<()> {
        for arg in &self.args {
            if matches.is_present(&arg.name) {
                for required in &arg.requires {
                    if !matches.is_present(required) {
                        return Err(CommandError::MissingDependency(
                            arg.name.clone(),
                            required.clone(),
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    /// Check argument conflicts
    fn check_conflicts(&self, matches: &ArgMatches) -> CommandResult<()> {
        for arg in &self.args {
            if matches.is_present(&arg.name) {
                for conflict in &arg.conflicts_with {
                    if matches.is_present(conflict) {
                        return Err(CommandError::ArgumentConflict(
                            arg.name.clone(),
                            conflict.clone(),
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    /// Validate argument groups
    fn validate_groups(&self, matches: &ArgMatches) -> CommandResult<()> {
        for group in &self.groups {
            let present_count = group.args.iter().filter(|a| matches.is_present(a)).count();

            if group.required && present_count == 0 {
                return Err(CommandError::MissingArgument(format!(
                    "{} (one of: {})",
                    group.name,
                    group.args.join(", ")
                )));
            }

            // Groups are mutually exclusive by default
            if present_count > 1 {
                let present: Vec<&String> = group
                    .args
                    .iter()
                    .filter(|a| matches.is_present(a))
                    .collect();
                return Err(CommandError::ArgumentConflict(
                    present[0].clone(),
                    present[1].clone(),
                ));
            }
        }

        Ok(())
    }
}

/// Represents the main application
#[derive(Debug, Clone)]
pub struct App {
    command: Command,
}

impl App {
    /// Creates a new application with the given name
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            command: Command::new(name),
        }
    }

    /// Sets the version for this application
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.command = self.command.version(version);
        self
    }

    /// Sets the description for this application
    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.command = self.command.about(about);
        self
    }

    /// Adds an argument to this application
    pub fn arg(mut self, arg: Arg) -> Self {
        self.command = self.command.arg(arg);
        self
    }

    /// Adds a subcommand to this application
    pub fn subcommand(mut self, subcommand: Command) -> Self {
        self.command = self.command.subcommand(subcommand);
        self
    }

    /// Parses command-line arguments from `std::env::args()`
    pub fn get_matches(self) -> ArgMatches {
        self.get_matches_from(std::env::args())
    }

    /// Parses command-line arguments from an iterator
    pub fn get_matches_from<I, T>(self, args: I) -> ArgMatches
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        let args: Vec<String> = args.into_iter().map(|a| a.into()).collect();
        let args_slice = if args.len() > 1 { &args[1..] } else { &[] };

        match self.command.parse_args(args_slice) {
            Ok(matches) => matches,
            Err(CommandError::HelpRequested) => {
                println!("{}", self.command.generate_help());
                std::process::exit(0);
            }
            Err(CommandError::VersionRequested) => {
                if let Some(version) = self.command.version {
                    println!("{} {}", self.command.name, version);
                } else {
                    println!("{}", self.command.name);
                }
                std::process::exit(0);
            }
            Err(e) => {
                eprintln!("{}", e);
                eprintln!("\nFor more information try --help");
                std::process::exit(1);
            }
        }
    }

    /// Tries to parse arguments and returns a Result instead of exiting
    pub fn try_get_matches(self) -> CommandResult<ArgMatches> {
        self.try_get_matches_from(std::env::args())
    }

    /// Tries to parse arguments from an iterator
    pub fn try_get_matches_from<I, T>(self, args: I) -> CommandResult<ArgMatches>
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        let args: Vec<String> = args.into_iter().map(|a| a.into()).collect();
        let args_slice = if args.len() > 1 { &args[1..] } else { &[] };
        self.command.parse_args(args_slice)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_arg_creation() {
        let arg = Arg::new("test")
            .short('t')
            .long("test")
            .about("Test argument")
            .required(true);

        assert_eq!(arg.name(), "test");
        assert_eq!(arg.short, Some('t'));
        assert_eq!(arg.long, Some("test".to_string()));
    }

    #[test]
    fn test_simple_command() {
        let app = App::new("test").version("1.0.0").about("Test app").arg(
            Arg::new("verbose")
                .short('v')
                .long("verbose")
                .takes_value(false),
        );

        let matches = app.try_get_matches_from(vec!["test", "--verbose"]).unwrap();

        assert!(matches.is_flag_set("verbose"));
    }

    #[test]
    fn test_subcommand_parsing() {
        let app = App::new("git").subcommand(
            Command::new("commit").arg(
                Arg::new("message")
                    .short('m')
                    .long("message")
                    .required(true),
            ),
        );

        let matches = app
            .try_get_matches_from(vec!["git", "commit", "-m", "Initial commit"])
            .unwrap();

        assert_eq!(matches.subcommand_name(), Some("commit"));
        let sub = matches.subcommand_matches("commit").unwrap();
        assert_eq!(sub.value_of("message"), Some("Initial commit"));
    }

    #[test]
    fn test_help_generation() {
        let cmd = Command::new("test")
            .about("Test command")
            .version("1.0.0")
            .arg(
                Arg::new("verbose")
                    .short('v')
                    .long("verbose")
                    .about("Verbose output"),
            );

        let help = cmd.generate_help();
        assert!(help.contains("Test command"));
        assert!(help.contains("--verbose"));
        assert!(help.contains("Verbose output"));
    }
}