flag-rs 0.10.0

A Cobra-inspired CLI framework with dynamic completions
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
//! Flag system for command-line argument parsing
//!
//! This module provides a flexible flag parsing system that supports:
//! - Multiple value types (string, bool, int, float, string slice)
//! - Short and long flag names
//! - Required and optional flags
//! - Default values
//! - Hierarchical flag inheritance from parent commands

use crate::completion::{CompletionFunc, CompletionResult};
use crate::error::{Error, Result};
use std::collections::HashSet;

/// Represents the value of a parsed flag
///
/// `FlagValue` is an enum that can hold different types of values
/// that flags can have. This allows for type-safe access to flag values.
///
/// # Examples
///
/// ```
/// use flag_rs::flag::{FlagValue, FlagType, Flag};
///
/// // Parse different types of values
/// let string_flag = Flag::new("name").value_type(FlagType::String);
/// let value = string_flag.parse_value("John").unwrap();
/// assert_eq!(value.as_string().unwrap(), "John");
///
/// let bool_flag = Flag::new("verbose").value_type(FlagType::Bool);
/// let value = bool_flag.parse_value("true").unwrap();
/// assert!(value.as_bool().unwrap());
/// ```
#[derive(Clone, Debug, PartialEq)]
pub enum FlagValue {
    /// A string value
    String(String),
    /// A boolean value
    Bool(bool),
    /// An integer value
    Int(i64),
    /// A floating-point value
    Float(f64),
    /// A slice of strings (for repeated flags)
    StringSlice(Vec<String>),
}

impl FlagValue {
    /// Returns the value as a string reference
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if the value is not a string
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::FlagValue;
    ///
    /// let value = FlagValue::String("hello".to_string());
    /// assert_eq!(value.as_string().unwrap(), "hello");
    ///
    /// let value = FlagValue::Bool(true);
    /// assert!(value.as_string().is_err());
    /// ```
    pub fn as_string(&self) -> Result<&String> {
        match self {
            Self::String(s) => Ok(s),
            _ => Err(Error::flag_parsing("Flag value is not a string")),
        }
    }

    /// Returns the value as a boolean
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if the value is not a boolean
    pub fn as_bool(&self) -> Result<bool> {
        match self {
            Self::Bool(b) => Ok(*b),
            _ => Err(Error::flag_parsing("Flag value is not a boolean")),
        }
    }

    /// Returns the value as an integer
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if the value is not an integer
    pub fn as_int(&self) -> Result<i64> {
        match self {
            Self::Int(i) => Ok(*i),
            _ => Err(Error::flag_parsing("Flag value is not an integer")),
        }
    }

    /// Returns the value as a float
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if the value is not a float
    pub fn as_float(&self) -> Result<f64> {
        match self {
            Self::Float(f) => Ok(*f),
            _ => Err(Error::flag_parsing("Flag value is not a float")),
        }
    }

    /// Returns the value as a string slice reference
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if the value is not a string slice
    pub fn as_string_slice(&self) -> Result<&Vec<String>> {
        match self {
            Self::StringSlice(v) => Ok(v),
            _ => Err(Error::flag_parsing("Flag value is not a string slice")),
        }
    }
}

/// Represents constraints that can be applied to flags
///
/// Flag constraints allow you to define relationships between flags,
/// such as mutual exclusivity or dependencies.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FlagConstraint {
    /// This flag is required if another flag is set
    RequiredIf(String),
    /// This flag conflicts with other flags (mutually exclusive)
    ConflictsWith(Vec<String>),
    /// This flag requires other flags to be set
    Requires(Vec<String>),
}

/// Represents a command-line flag
///
/// A `Flag` defines a command-line option that can be passed to a command.
/// Flags can have both long names (e.g., `--verbose`) and short names (e.g., `-v`).
///
/// # Examples
///
/// ```
/// use flag_rs::flag::{Flag, FlagType, FlagValue};
///
/// // Create a boolean flag
/// let verbose = Flag::new("verbose")
///     .short('v')
///     .usage("Enable verbose output")
///     .value_type(FlagType::Bool)
///     .default(FlagValue::Bool(false));
///
/// // Create a string flag with validation
/// let name = Flag::new("name")
///     .short('n')
///     .usage("Name of the resource")
///     .value_type(FlagType::String)
///     .required();
/// ```
pub struct Flag {
    /// The long name of the flag (e.g., "verbose" for --verbose)
    pub name: String,
    /// The optional short name of the flag (e.g., 'v' for -v)
    pub short: Option<char>,
    /// A description of what the flag does
    pub usage: String,
    /// The default value if the flag is not provided
    pub default: Option<FlagValue>,
    /// Whether this flag must be provided
    pub required: bool,
    /// The type of value this flag accepts
    pub value_type: FlagType,
    /// Constraints applied to this flag
    pub constraints: Vec<FlagConstraint>,
    /// Optional completion function for this flag's values
    pub completion: Option<CompletionFunc>,
}

/// Represents the type of value a flag accepts
///
/// This enum determines how flag values are parsed from string input.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FlagType {
    /// Accepts any string value
    String,
    /// Accepts boolean values (true/false, yes/no, 1/0)
    Bool,
    /// Accepts integer values
    Int,
    /// Accepts floating-point values
    Float,
    /// Accepts multiple string values (can be specified multiple times)
    StringSlice,
    /// Accepts multiple string values with accumulation (--tag=a --tag=b)
    StringArray,
    /// Must be one of a predefined set of values
    Choice(Vec<String>),
    /// Numeric value within a specific range
    Range(i64, i64),
    /// Must be a valid file path
    File,
    /// Must be a valid directory path
    Directory,
}

impl Flag {
    /// Creates a new flag with the given name
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::new("verbose");
    /// assert_eq!(flag.name, "verbose");
    /// ```
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            short: None,
            usage: String::new(),
            default: None,
            required: false,
            value_type: FlagType::String,
            constraints: Vec::new(),
            completion: None,
        }
    }

    /// Creates a new boolean flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::bool("verbose");
    /// ```
    #[must_use]
    pub fn bool(name: impl Into<String>) -> Self {
        Self::new(name).value_type(FlagType::Bool)
    }

    /// Creates a new integer flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::int("port");
    /// ```
    #[must_use]
    pub fn int(name: impl Into<String>) -> Self {
        Self::new(name).value_type(FlagType::Int)
    }

    /// Creates a new float flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::float("ratio");
    /// ```
    #[must_use]
    pub fn float(name: impl Into<String>) -> Self {
        Self::new(name).value_type(FlagType::Float)
    }

    /// Creates a new string flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::string("name");
    /// ```
    #[must_use]
    pub fn string(name: impl Into<String>) -> Self {
        Self::new(name) // String is the default type
    }

    /// Creates a new string slice flag (can be specified multiple times)
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::string_slice("tag");
    /// ```
    #[must_use]
    pub fn string_slice(name: impl Into<String>) -> Self {
        Self::new(name).value_type(FlagType::StringSlice)
    }

    /// Creates a new choice flag with allowed values
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::choice("format", &["json", "yaml", "xml"]);
    /// ```
    #[must_use]
    pub fn choice(name: impl Into<String>, choices: &[&str]) -> Self {
        let choices: Vec<String> = choices.iter().map(|&s| s.to_string()).collect();
        Self::new(name).value_type(FlagType::Choice(choices))
    }

    /// Creates a new range flag with min and max values
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::range("workers", 1, 16);
    /// ```
    #[must_use]
    pub fn range(name: impl Into<String>, min: i64, max: i64) -> Self {
        Self::new(name).value_type(FlagType::Range(min, max))
    }

    /// Creates a new file flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::file("config");
    /// ```
    #[must_use]
    pub fn file(name: impl Into<String>) -> Self {
        Self::new(name).value_type(FlagType::File)
    }

    /// Creates a new directory flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::directory("output");
    /// ```
    #[must_use]
    pub fn directory(name: impl Into<String>) -> Self {
        Self::new(name).value_type(FlagType::Directory)
    }

    /// Sets the short name for this flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::new("verbose").short('v');
    /// assert_eq!(flag.short, Some('v'));
    /// ```
    #[must_use]
    pub const fn short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }

    /// Sets the usage description for this flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::new("verbose").usage("Enable verbose output");
    /// assert_eq!(flag.usage, "Enable verbose output");
    /// ```
    #[must_use]
    pub fn usage(mut self, usage: impl Into<String>) -> Self {
        self.usage = usage.into();
        self
    }

    /// Sets the default value for this flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagValue};
    ///
    /// let flag = Flag::new("count").default(FlagValue::Int(10));
    /// assert_eq!(flag.default, Some(FlagValue::Int(10)));
    /// ```
    #[must_use]
    pub fn default(mut self, value: FlagValue) -> Self {
        self.default = Some(value);
        self
    }

    /// Sets a default boolean value
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagValue};
    ///
    /// let flag = Flag::bool("verbose").default_bool(true);
    /// assert_eq!(flag.default, Some(FlagValue::Bool(true)));
    /// ```
    #[must_use]
    pub fn default_bool(self, value: bool) -> Self {
        self.default(FlagValue::Bool(value))
    }

    /// Sets a default string value
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagValue};
    ///
    /// let flag = Flag::string("name").default_str("anonymous");
    /// assert_eq!(flag.default, Some(FlagValue::String("anonymous".to_string())));
    /// ```
    #[must_use]
    pub fn default_str(self, value: &str) -> Self {
        self.default(FlagValue::String(value.to_string()))
    }

    /// Sets a default integer value
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagValue};
    ///
    /// let flag = Flag::int("port").default_int(8080);
    /// assert_eq!(flag.default, Some(FlagValue::Int(8080)));
    /// ```
    #[must_use]
    pub fn default_int(self, value: i64) -> Self {
        self.default(FlagValue::Int(value))
    }

    /// Sets a default float value
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagValue};
    ///
    /// let flag = Flag::float("ratio").default_float(0.5);
    /// assert_eq!(flag.default, Some(FlagValue::Float(0.5)));
    /// ```
    #[must_use]
    pub fn default_float(self, value: f64) -> Self {
        self.default(FlagValue::Float(value))
    }

    /// Marks this flag as required
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    ///
    /// let flag = Flag::new("name").required();
    /// assert!(flag.required);
    /// ```
    #[must_use]
    pub const fn required(mut self) -> Self {
        self.required = true;
        self
    }

    /// Sets the value type for this flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagType};
    ///
    /// let flag = Flag::new("count").value_type(FlagType::Int);
    /// ```
    #[must_use]
    pub fn value_type(mut self, value_type: FlagType) -> Self {
        self.value_type = value_type;
        self
    }

    /// Adds a constraint to this flag
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagConstraint};
    ///
    /// let flag = Flag::new("ssl")
    ///     .constraint(FlagConstraint::RequiredIf("port".to_string()))
    ///     .constraint(FlagConstraint::ConflictsWith(vec!["no-ssl".to_string()]));
    /// ```
    #[must_use]
    pub fn constraint(mut self, constraint: FlagConstraint) -> Self {
        self.constraints.push(constraint);
        self
    }

    /// Sets a completion function for this flag's values
    ///
    /// # Arguments
    ///
    /// * `completion` - A function that generates completions for flag values
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::Flag;
    /// use flag_rs::completion::CompletionResult;
    ///
    /// let flag = Flag::file("config")
    ///     .completion(|ctx, prefix| {
    ///         // In a real application, you might list config files
    ///         let configs = vec!["default.conf", "production.conf", "test.conf"];
    ///         Ok(CompletionResult::new().extend(
    ///             configs.into_iter()
    ///                 .filter(|c| c.starts_with(prefix))
    ///                 .map(String::from)
    ///         ))
    ///     });
    /// ```
    #[must_use]
    pub fn completion<F>(mut self, completion: F) -> Self
    where
        F: Fn(&crate::Context, &str) -> Result<CompletionResult> + Send + Sync + 'static,
    {
        self.completion = Some(Box::new(completion));
        self
    }

    /// Parses a string value according to this flag's type
    ///
    /// # Arguments
    ///
    /// * `input` - The string value to parse
    ///
    /// # Returns
    ///
    /// Returns the parsed `FlagValue` on success
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if the input cannot be parsed as the expected type
    ///
    /// # Examples
    ///
    /// ```
    /// use flag_rs::flag::{Flag, FlagType, FlagValue};
    ///
    /// let int_flag = Flag::new("count").value_type(FlagType::Int);
    /// match int_flag.parse_value("42") {
    ///     Ok(FlagValue::Int(n)) => assert_eq!(n, 42),
    ///     _ => panic!("Expected Int value"),
    /// }
    ///
    /// let bool_flag = Flag::new("verbose").value_type(FlagType::Bool);
    /// match bool_flag.parse_value("true") {
    ///     Ok(FlagValue::Bool(b)) => assert!(b),
    ///     _ => panic!("Expected Bool value"),
    /// }
    /// ```
    pub fn parse_value(&self, input: &str) -> Result<FlagValue> {
        match &self.value_type {
            FlagType::String => Ok(FlagValue::String(input.to_string())),
            FlagType::Bool => match input.to_lowercase().as_str() {
                "true" | "t" | "1" | "yes" | "y" => Ok(FlagValue::Bool(true)),
                "false" | "f" | "0" | "no" | "n" => Ok(FlagValue::Bool(false)),
                _ => Err(Error::flag_parsing_with_suggestions(
                    format!("Invalid boolean value: '{input}'"),
                    self.name.clone(),
                    vec![
                        "true, false".to_string(),
                        "yes, no".to_string(),
                        "1, 0".to_string(),
                    ],
                )),
            },
            FlagType::Int => input.parse::<i64>().map(FlagValue::Int).map_err(|_| {
                Error::flag_parsing_with_suggestions(
                    format!("Invalid integer value: '{input}'"),
                    self.name.clone(),
                    vec!["a whole number (e.g., 42, -10, 0)".to_string()],
                )
            }),
            FlagType::Float => input.parse::<f64>().map(FlagValue::Float).map_err(|_| {
                Error::flag_parsing_with_suggestions(
                    format!("Invalid float value: '{input}'"),
                    self.name.clone(),
                    vec!["a decimal number (e.g., 3.14, -0.5, 1e10)".to_string()],
                )
            }),
            FlagType::StringSlice | FlagType::StringArray => {
                Ok(FlagValue::StringSlice(vec![input.to_string()]))
            }
            FlagType::Choice(choices) => {
                if choices.contains(&input.to_string()) {
                    Ok(FlagValue::String(input.to_string()))
                } else {
                    Err(Error::flag_parsing_with_suggestions(
                        format!("Invalid choice: '{input}'"),
                        self.name.clone(),
                        choices.clone(),
                    ))
                }
            }
            FlagType::Range(min, max) => {
                let value = input.parse::<i64>().map_err(|_| {
                    Error::flag_parsing_with_suggestions(
                        format!("Invalid integer value: '{input}'"),
                        self.name.clone(),
                        vec![format!("a number between {min} and {max}")],
                    )
                })?;
                if value >= *min && value <= *max {
                    Ok(FlagValue::Int(value))
                } else {
                    Err(Error::flag_parsing_with_suggestions(
                        format!("Value {value} is out of range"),
                        self.name.clone(),
                        vec![format!("a number between {min} and {max} (inclusive)")],
                    ))
                }
            }
            FlagType::File => Self::parse_path(input, &self.name, PathKind::File),
            FlagType::Directory => Self::parse_path(input, &self.name, PathKind::Directory),
        }
    }

    fn parse_path(input: &str, name: &str, kind: PathKind) -> Result<FlagValue> {
        use std::path::Path;
        let path = Path::new(input);
        if path.exists() && kind.matches(path) {
            Ok(FlagValue::String(input.to_string()))
        } else if !path.exists() {
            Err(Error::flag_parsing_with_suggestions(
                format!("{} not found: '{input}'", kind.capitalized()),
                name.to_string(),
                vec![kind.not_found_suggestion().to_string()],
            ))
        } else {
            Err(Error::flag_parsing_with_suggestions(
                format!("Path exists but is not a {}: '{input}'", kind.lowercase()),
                name.to_string(),
                vec![kind.wrong_kind_suggestion().to_string()],
            ))
        }
    }

    /// Validates this flag's constraints against the provided flags
    ///
    /// # Arguments
    ///
    /// * `flag_name` - The name of this flag
    /// * `provided_flags` - Set of flag names that were provided
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if all constraints are satisfied
    ///
    /// # Errors
    ///
    /// Returns `Error::FlagParsing` if any constraint is violated
    pub fn validate_constraints(
        &self,
        flag_name: &str,
        provided_flags: &HashSet<String>,
    ) -> Result<()> {
        for constraint in &self.constraints {
            match constraint {
                FlagConstraint::RequiredIf(other_flag) => {
                    if provided_flags.contains(other_flag) && !provided_flags.contains(flag_name) {
                        return Err(Error::flag_parsing_with_suggestions(
                            format!(
                                "Flag '--{flag_name}' is required when '--{other_flag}' is set"
                            ),
                            flag_name.to_string(),
                            vec![format!("add --{flag_name} <value>")],
                        ));
                    }
                }
                FlagConstraint::ConflictsWith(conflicting_flags) => {
                    if provided_flags.contains(flag_name) {
                        for conflict in conflicting_flags {
                            if provided_flags.contains(conflict) {
                                return Err(Error::flag_parsing_with_suggestions(
                                    format!("Flag '--{flag_name}' conflicts with '--{conflict}'"),
                                    flag_name.to_string(),
                                    vec![format!(
                                        "use either --{flag_name} or --{conflict}, not both"
                                    )],
                                ));
                            }
                        }
                    }
                }
                FlagConstraint::Requires(required_flags) => {
                    if provided_flags.contains(flag_name) {
                        for required in required_flags {
                            if !provided_flags.contains(required) {
                                return Err(Error::flag_parsing_with_suggestions(
                                    format!(
                                        "Flag '--{flag_name}' requires '--{required}' to be set"
                                    ),
                                    flag_name.to_string(),
                                    vec![format!("add --{required} <value>")],
                                ));
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

impl Clone for Flag {
    fn clone(&self) -> Self {
        Self {
            name: self.name.clone(),
            short: self.short,
            usage: self.usage.clone(),
            default: self.default.clone(),
            required: self.required,
            value_type: self.value_type.clone(),
            constraints: self.constraints.clone(),
            completion: None, // Don't clone the completion function
        }
    }
}

#[derive(Clone, Copy)]
enum PathKind {
    File,
    Directory,
}

impl PathKind {
    fn matches(self, path: &std::path::Path) -> bool {
        match self {
            Self::File => path.is_file(),
            Self::Directory => path.is_dir(),
        }
    }

    const fn capitalized(self) -> &'static str {
        match self {
            Self::File => "File",
            Self::Directory => "Directory",
        }
    }

    const fn lowercase(self) -> &'static str {
        match self {
            Self::File => "file",
            Self::Directory => "directory",
        }
    }

    const fn not_found_suggestion(self) -> &'static str {
        match self {
            Self::File => "path to an existing file",
            Self::Directory => "path to an existing directory",
        }
    }

    const fn wrong_kind_suggestion(self) -> &'static str {
        match self {
            Self::File => "path to a regular file (not a directory)",
            Self::Directory => "path to a directory (not a file)",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[allow(clippy::approx_constant)]
    const PI: f64 = 3.14;

    #[test]
    fn test_flag_value_conversions() {
        let string_val = FlagValue::String("hello".to_string());
        assert_eq!(string_val.as_string().unwrap(), "hello");
        assert!(string_val.as_bool().is_err());

        let bool_val = FlagValue::Bool(true);
        assert!(bool_val.as_bool().unwrap());
        assert!(bool_val.as_string().is_err());

        let int_val = FlagValue::Int(42);
        assert_eq!(int_val.as_int().unwrap(), 42);
        assert!(int_val.as_float().is_err());

        let float_val = FlagValue::Float(PI);
        assert!((float_val.as_float().unwrap() - PI).abs() < f64::EPSILON);
        assert!(float_val.as_int().is_err());

        let slice_val = FlagValue::StringSlice(vec!["a".to_string(), "b".to_string()]);
        assert_eq!(
            slice_val.as_string_slice().unwrap(),
            &vec!["a".to_string(), "b".to_string()]
        );
        assert!(slice_val.as_string().is_err());
    }

    #[test]
    fn test_flag_parsing() {
        let string_flag = Flag::new("name").value_type(FlagType::String);
        assert_eq!(
            string_flag.parse_value("test").unwrap(),
            FlagValue::String("test".to_string())
        );

        let bool_flag = Flag::new("verbose").value_type(FlagType::Bool);
        assert_eq!(
            bool_flag.parse_value("true").unwrap(),
            FlagValue::Bool(true)
        );
        assert_eq!(
            bool_flag.parse_value("false").unwrap(),
            FlagValue::Bool(false)
        );
        assert_eq!(bool_flag.parse_value("1").unwrap(), FlagValue::Bool(true));
        assert_eq!(bool_flag.parse_value("0").unwrap(), FlagValue::Bool(false));
        assert_eq!(bool_flag.parse_value("yes").unwrap(), FlagValue::Bool(true));
        assert_eq!(bool_flag.parse_value("no").unwrap(), FlagValue::Bool(false));
        assert!(bool_flag.parse_value("invalid").is_err());

        let int_flag = Flag::new("count").value_type(FlagType::Int);
        assert_eq!(int_flag.parse_value("42").unwrap(), FlagValue::Int(42));
        assert_eq!(int_flag.parse_value("-10").unwrap(), FlagValue::Int(-10));
        assert!(int_flag.parse_value("not_a_number").is_err());

        let float_flag = Flag::new("ratio").value_type(FlagType::Float);
        assert_eq!(
            float_flag.parse_value("3.14").unwrap(),
            FlagValue::Float(PI)
        );
        assert_eq!(
            float_flag.parse_value("-2.5").unwrap(),
            FlagValue::Float(-2.5)
        );
        assert!(float_flag.parse_value("not_a_float").is_err());
    }

    #[test]
    fn test_flag_builder() {
        let flag = Flag::new("verbose")
            .short('v')
            .usage("Enable verbose output")
            .default(FlagValue::Bool(false))
            .value_type(FlagType::Bool);

        assert_eq!(flag.name, "verbose");
        assert_eq!(flag.short, Some('v'));
        assert_eq!(flag.usage, "Enable verbose output");
        assert_eq!(flag.default, Some(FlagValue::Bool(false)));
        assert!(!flag.required);
    }

    #[test]
    fn test_choice_flag() {
        let choice_flag = Flag::new("environment").value_type(FlagType::Choice(vec![
            "dev".to_string(),
            "staging".to_string(),
            "prod".to_string(),
        ]));

        assert_eq!(
            choice_flag.parse_value("dev").unwrap(),
            FlagValue::String("dev".to_string())
        );
        assert_eq!(
            choice_flag.parse_value("staging").unwrap(),
            FlagValue::String("staging".to_string())
        );
        assert!(choice_flag.parse_value("test").is_err());
    }

    #[test]
    fn test_range_flag() {
        let range_flag = Flag::new("port").value_type(FlagType::Range(1024, 65535));

        assert_eq!(
            range_flag.parse_value("8080").unwrap(),
            FlagValue::Int(8080)
        );
        assert_eq!(
            range_flag.parse_value("1024").unwrap(),
            FlagValue::Int(1024)
        );
        assert_eq!(
            range_flag.parse_value("65535").unwrap(),
            FlagValue::Int(65535)
        );
        assert!(range_flag.parse_value("80").is_err());
        assert!(range_flag.parse_value("70000").is_err());
        assert!(range_flag.parse_value("not_a_number").is_err());
    }

    #[test]
    fn test_file_flag() {
        use std::fs::File;
        use std::io::Write;
        let temp_file = "test_file_flag.tmp";
        let mut file = File::create(temp_file).unwrap();
        writeln!(file, "test").unwrap();

        let file_flag = Flag::new("config").value_type(FlagType::File);
        assert_eq!(
            file_flag.parse_value(temp_file).unwrap(),
            FlagValue::String(temp_file.to_string())
        );
        assert!(file_flag.parse_value("nonexistent.file").is_err());

        std::fs::remove_file(temp_file).unwrap();
    }

    #[test]
    fn test_directory_flag() {
        let dir_flag = Flag::new("output").value_type(FlagType::Directory);

        // Test with current directory
        assert_eq!(
            dir_flag.parse_value(".").unwrap(),
            FlagValue::String(".".to_string())
        );

        // Test with src directory (should exist in the project)
        assert_eq!(
            dir_flag.parse_value("src").unwrap(),
            FlagValue::String("src".to_string())
        );

        assert!(dir_flag.parse_value("nonexistent_directory").is_err());
    }

    #[test]
    fn test_string_array_flag() {
        let array_flag = Flag::new("tags").value_type(FlagType::StringArray);

        assert_eq!(
            array_flag.parse_value("tag1").unwrap(),
            FlagValue::StringSlice(vec!["tag1".to_string()])
        );
    }

    #[test]
    fn test_flag_constraints() {
        let mut provided_flags = HashSet::new();

        // Test RequiredIf constraint
        let ssl_flag = Flag::new("ssl").constraint(FlagConstraint::RequiredIf("port".to_string()));

        // Should pass when port flag is not set
        assert!(
            ssl_flag
                .validate_constraints("ssl", &provided_flags)
                .is_ok()
        );

        // Should fail when port is set but ssl is not
        provided_flags.insert("port".to_string());
        assert!(
            ssl_flag
                .validate_constraints("ssl", &provided_flags)
                .is_err()
        );

        // Should pass when both are set
        provided_flags.insert("ssl".to_string());
        assert!(
            ssl_flag
                .validate_constraints("ssl", &provided_flags)
                .is_ok()
        );

        // Test ConflictsWith constraint
        let encrypt_flag = Flag::new("encrypt").constraint(FlagConstraint::ConflictsWith(vec![
            "no-encrypt".to_string(),
        ]));

        provided_flags.clear();
        provided_flags.insert("encrypt".to_string());
        assert!(
            encrypt_flag
                .validate_constraints("encrypt", &provided_flags)
                .is_ok()
        );

        provided_flags.insert("no-encrypt".to_string());
        assert!(
            encrypt_flag
                .validate_constraints("encrypt", &provided_flags)
                .is_err()
        );

        // Test Requires constraint
        let output_flag =
            Flag::new("output").constraint(FlagConstraint::Requires(vec!["format".to_string()]));

        provided_flags.clear();
        provided_flags.insert("output".to_string());
        assert!(
            output_flag
                .validate_constraints("output", &provided_flags)
                .is_err()
        );

        provided_flags.insert("format".to_string());
        assert!(
            output_flag
                .validate_constraints("output", &provided_flags)
                .is_ok()
        );
    }
}