11using System ;
22using System . Collections . Generic ;
33using System . Collections . ObjectModel ;
4+ using System . ComponentModel ;
45using System . Linq ;
56
67namespace J4JSoftware . CommandLine
@@ -9,11 +10,19 @@ public class Option
910 {
1011 private readonly List < string > _cmdLineKeys = new List < string > ( ) ;
1112 private readonly List < string > _allocatedValues = new List < string > ( ) ;
13+ private readonly MasterTextCollection _masterText ;
1214
13- internal Option ( Options container , IContextKey contextKey )
15+ private int _allowedNumValues ;
16+
17+ internal Option (
18+ Options container ,
19+ IContextKey contextKey ,
20+ MasterTextCollection masterText
21+ )
1422 {
1523 Container = container ;
1624 ContextKey = contextKey ;
25+ _masterText = masterText ;
1726
1827 if ( Container . UsesContextPath ( ContextPath ! ) )
1928 throw new ArgumentException ( $ "Duplicate context key path '{ ContextPath } '" ) ;
@@ -52,19 +61,43 @@ public List<IContextKey>? ContextPath
5261 // but they must be unique within the scope of all Options)
5362 public string FirstKey => _cmdLineKeys . OrderBy ( k => k ) . First ( ) ;
5463
55- public string ? CommandLineKeyUsed { get ; set ; }
64+ public string ? CommandLineKeyProvided { get ; set ; }
65+
66+ public bool WasAssignedValue
67+ {
68+ get
69+ {
70+ if ( string . IsNullOrEmpty ( CommandLineKeyProvided ) )
71+ return false ;
72+
73+ var numValuesAlloc = _allocatedValues . Count ;
74+
75+ return Style switch
76+ {
77+ OptionStyle . Switch => numValuesAlloc == 0 ,
78+ OptionStyle . SingleValued => numValuesAlloc == 1 ,
79+ OptionStyle . Collection => numValuesAlloc > 0 ,
80+ _ => throw new InvalidEnumArgumentException ( $ "Unsupported OptionStyle '{ Style } '")
81+ } ;
82+ }
83+ }
84+
5685 public ReadOnlyCollection < string > CommandLineValues => _allocatedValues . AsReadOnly ( ) ;
5786
58- public int AllowedNumberOfValues { get ; private set ; }
87+ public OptionStyle Style { get ; private set ; }
88+
5989 public bool Required { get ; private set ; }
6090 public string ? Description { get ; private set ; }
6191
6292 internal void AddAllocatedValue ( string value ) => _allocatedValues . Add ( value ) ;
6393
6494 public Option AddCommandLineKey ( string cmdLineKey )
6595 {
66- if ( ! Container . UsesCommandLineKey ( cmdLineKey ) )
96+ if ( ! Container . UsesCommandLineKey ( cmdLineKey ) )
97+ {
6798 _cmdLineKeys . Add ( cmdLineKey ) ;
99+ _masterText . Add ( TextUsageType . OptionKey , cmdLineKey ) ;
100+ }
68101
69102 return this ;
70103 }
@@ -79,21 +112,16 @@ public Option AddCommandLineKeys(params string[] cmdLineKeys)
79112 return this ;
80113 }
81114
82- public Option IsSwitch ( )
83- {
84- AllowedNumberOfValues = 0 ;
85- return this ;
86- }
87-
88- public Option IsCollection ( )
115+ public Option SetStyle ( OptionStyle style )
89116 {
90- AllowedNumberOfValues = Int32 . MaxValue ;
91- return this ;
92- }
117+ _allowedNumValues = style switch
118+ {
119+ OptionStyle . Collection => Int32 . MaxValue ,
120+ OptionStyle . SingleValued => 1 ,
121+ OptionStyle . Switch => 0 ,
122+ _ => throw new InvalidEnumArgumentException ( $ "Unsupported OptionStyle '{ style } '" )
123+ } ;
93124
94- public Option IsSingleValue ( )
95- {
96- AllowedNumberOfValues = 1 ;
97125 return this ;
98126 }
99127
0 commit comments