1+ /*
2+ * Taken from https://cmdline.codeplex.com/
3+ *
4+ */
5+
6+ //TODO: Replace with https://github.com/commandlineparser/commandline
7+ //TODO: or with this https://github.com/natemcmaster/CommandLineUtils
8+ namespace LogExpert . Classes . CommandLine
9+ {
10+ /// <summary>
11+ /// Represents an string command line parameter.
12+ /// </summary>
13+ public class CmdLineString ( string name , bool required , string helpMessage ) : CmdLineParameter ( name , required , helpMessage )
14+ {
15+
16+ #region Public methods
17+
18+ public static implicit operator string ( CmdLineString s )
19+ {
20+ return s . Value ;
21+ }
22+
23+ #endregion
24+ }
25+
26+ /// <summary>
27+ /// Provides a simple strongly typed interface to work with command line parameters.
28+ /// </summary>
29+ public class CmdLine
30+ {
31+ #region Fields
32+
33+ // A private dictonary containing the parameters.
34+ private readonly Dictionary < string , CmdLineParameter > parameters = [ ] ;
35+
36+ #endregion
37+
38+ #region cTor
39+
40+ /// <summary>
41+ /// Creats a new empty command line object.
42+ /// </summary>
43+ public CmdLine ( )
44+ {
45+ }
46+
47+ #endregion
48+
49+ #region Properties
50+
51+ /// <summary>
52+ /// Returns a command line parameter by the name.
53+ /// </summary>
54+ /// <param name="name">The name of the parameter (the word after the initial hyphen (-).</param>
55+ /// <returns>A reference to the named comman line object.</returns>
56+ public CmdLineParameter this [ string name ]
57+ {
58+ get
59+ {
60+ if ( parameters . TryGetValue ( name , out CmdLineParameter value ) == false )
61+ {
62+ throw new CmdLineException ( name , "Not a registered parameter." ) ;
63+ }
64+ return value ;
65+ }
66+ }
67+
68+ #endregion
69+
70+ #region Public methods
71+
72+ /// <summary>
73+ /// Registers a parameter to be used and adds it to the help screen.
74+ /// </summary>
75+ /// <param name="parameter">The parameter to add.</param>
76+ public void RegisterParameter ( CmdLineParameter parameter )
77+ {
78+ if ( parameters . ContainsKey ( parameter . Name ) )
79+ {
80+ throw new CmdLineException ( parameter . Name , "Parameter is already registered." ) ;
81+ }
82+ parameters . Add ( parameter . Name , parameter ) ;
83+ }
84+
85+ /// <summary>
86+ /// Registers parameters to be used and adds hem to the help screen.
87+ /// </summary>
88+ /// <param name="parameters">The parameter to add.</param>
89+ public void RegisterParameter ( CmdLineParameter [ ] parameters )
90+ {
91+ foreach ( CmdLineParameter p in parameters )
92+ {
93+ RegisterParameter ( p ) ;
94+ }
95+ }
96+
97+
98+ /// <summary>
99+ /// Parses the command line and sets the value of each registered parmaters.
100+ /// </summary>
101+ /// <param name="args">The arguments array sent to main()</param>
102+ /// <returns>Any reminding strings after arguments has been processed.</returns>
103+ public string [ ] Parse ( string [ ] args )
104+ {
105+ int i = 0 ;
106+
107+ List < string > new_args = [ ] ;
108+
109+ while ( i < args . Length )
110+ {
111+ if ( args [ i ] . Length > 1 && args [ i ] [ 0 ] == '-' )
112+ {
113+ // The current string is a parameter name
114+ string key = args [ i ] [ 1 ..] . ToLower ( ) ;
115+ string argsValue = string . Empty ;
116+ i ++ ;
117+ if ( i < args . Length )
118+ {
119+ if ( args [ i ] . Length > 0 && args [ i ] [ 0 ] == '-' )
120+ {
121+ // The next string is a new parameter, do not nothing
122+ }
123+ else
124+ {
125+ // The next string is a value, read the value and move forward
126+ argsValue = args [ i ] ;
127+ i ++ ;
128+ }
129+ }
130+ if ( parameters . TryGetValue ( key , out CmdLineParameter cmdLineParameter ) == false )
131+ {
132+ throw new CmdLineException ( key , "Parameter is not allowed." ) ;
133+ }
134+
135+ if ( cmdLineParameter . Exists )
136+ {
137+ throw new CmdLineException ( key , "Parameter is specified more than once." ) ;
138+ }
139+
140+ cmdLineParameter . SetValue ( argsValue ) ;
141+ }
142+ else
143+ {
144+ new_args . Add ( args [ i ] ) ;
145+ i ++ ;
146+ }
147+ }
148+
149+
150+ // Check that required parameters are present in the command line.
151+ foreach ( string key in parameters . Keys )
152+ {
153+ if ( parameters [ key ] . Required && parameters [ key ] . Exists == false )
154+ {
155+ throw new CmdLineException ( key , "Required parameter is not found." ) ;
156+ }
157+ }
158+
159+ return new_args . ToArray ( ) ;
160+ }
161+
162+ /// <summary>
163+ /// Generates the help screen.
164+ /// </summary>
165+ public string HelpScreen ( )
166+ {
167+ int len = 0 ;
168+ foreach ( string key in parameters . Keys )
169+ {
170+ len = Math . Max ( len , key . Length ) ;
171+ }
172+
173+ string help = "\n Parameters:\n \n " ;
174+ foreach ( string key in parameters . Keys )
175+ {
176+ string s = "-" + parameters [ key ] . Name ;
177+ while ( s . Length < len + 3 )
178+ {
179+ s += " " ;
180+ }
181+ s += parameters [ key ] . Help + "\n " ;
182+ help += s ;
183+ }
184+ return help ;
185+ }
186+
187+ #endregion
188+ }
189+ }
0 commit comments