@@ -6,6 +6,8 @@ namespace ProgramArgumentsManager
66{
77 public class ArgumentsManager
88 {
9+ private const int _DEFAULT_PAD_LEFT = 20 ;
10+
911 public string AppName { get ; }
1012 public string AppDescription { get ; }
1113 public string Version { get ; }
@@ -25,7 +27,7 @@ public ArgumentsManager(string name, string version, string description)
2527 _options = new Dictionary < Argument , Argument . ArgValues > ( ) ;
2628 }
2729
28- private bool IsArgument ( string s ) => s . StartsWith ( "-" ) || s . StartsWith ( "--" ) ;
30+ private static bool IsArgument ( string s ) => s . StartsWith ( "-" ) || s . StartsWith ( "--" ) ;
2931
3032 public void AddArguments ( string format , string description )
3133 {
@@ -42,7 +44,7 @@ public void AddArguments(string format, string description)
4244 throw new ArgumentException ( "Le format '" + format + " n'est pas comaptible en tant que paramètre !" ,
4345 nameof ( format ) ) ;
4446
45- _options . Add ( new Argument ( formats ) , new Argument . ArgValues ( description ) ) ;
47+ _options . Add ( new Argument ( formats , description ) , new Argument . ArgValues ( ) ) ;
4648 }
4749
4850 public void Parse ( string [ ] args )
@@ -71,9 +73,9 @@ public void Parse(string[] args)
7173
7274 public string GetValue ( string arg ) => _options [ arg ] . Values is null ? null : string . Join ( " " , _options [ arg ] . Values ) ;
7375
74- public void ShowUsage ( ) => Console . WriteLine ( GetUsage ( ) ) ;
76+ public void ShowUsage ( int padLeft = _DEFAULT_PAD_LEFT ) => Console . WriteLine ( GetUsage ( padLeft ) ) ;
7577
76- public string GetUsage ( )
78+ public string GetUsage ( int padLeft = _DEFAULT_PAD_LEFT )
7779 {
7880 string usage = AppName + ":\n \n " ;
7981 if ( ! ( AppDescription is null ) )
@@ -83,21 +85,34 @@ public string GetUsage()
8385
8486 return _options . Aggregate ( usage ,
8587 ( current , option ) =>
86- current + option . Key . ToString ( ) . PadLeft ( 20 ) + " = " + option . Value . Description + "\n " ) ;
88+ current + option . Key . ToString ( ) . PadLeft ( padLeft ) + " = " + option . Key . Description + "\n " ) ;
8789 }
8890
8991 private class Argument
9092 {
91- / * public enum Format
92- private readonly string [ ] _names ;
93-
94- private Argument ( string name )
93+ public enum Ask
9594 {
96- _names = new [ ] { name } ;
95+ Required ,
96+ Optional
9797 }
98- public Argument ( string [ ] names )
98+
99+ private readonly string [ ] _names ;
100+ public string Description { get ; }
101+ private Ask ask ;
102+
103+ private Argument ( string name , string desc ) : this ( new [ ] { name } , desc ) { }
104+
105+ public Argument ( string [ ] names , string desc )
99106 {
100107 _names = names ;
108+ Description = desc ;
109+
110+ if ( desc . StartsWith ( "[OPTIONAL]" , StringComparison . InvariantCultureIgnoreCase ) )
111+ ask = Ask . Optional ;
112+ else if ( desc . StartsWith ( "[REQUIRED]" , StringComparison . InvariantCultureIgnoreCase ) )
113+ ask = Ask . Required ;
114+ else
115+ ask = Ask . Required ;
101116 }
102117
103118 private bool Equals ( Argument other ) => other . _names . Any ( s => _names . Contains ( s ) ) ;
@@ -130,18 +145,15 @@ public override int GetHashCode()
130145 return ! Equals ( left , right ) ;
131146 }
132147
133- public static implicit operator Argument ( string s ) => new Argument ( s ) ;
148+ public static implicit operator Argument ( string s ) => new Argument ( s , "Converted argument" ) ;
134149
135150 public class ArgValues
136151 {
137- public string Description { get ; }
138-
139152 public List < string > Values { get ; set ; }
140153 public bool Specified { get ; set ; }
141154
142- public ArgValues ( string description )
155+ public ArgValues ( )
143156 {
144- Description = description ;
145157 Values = new List < string > ( ) ;
146158 Specified = false ;
147159 }
0 commit comments