33
44/*
55 * Taken from https://cmdline.codeplex.com/
6- *
6+ *
77 */
88
99namespace LogExpert . Classes
@@ -15,19 +15,23 @@ public class CmdLineException : Exception
1515 {
1616 #region cTor
1717
18- public CmdLineException ( string parameter , string message ) : base ( $ "Syntax error of parameter -{ parameter } : { message } ")
18+ public CmdLineException ( string parameter , string message )
19+ :
20+ base ( string . Format ( "Syntax error of parameter -{0}: {1}" , parameter , message ) )
1921 {
2022 }
2123
22- public CmdLineException ( string message ) : base ( message )
24+ public CmdLineException ( string message )
25+ :
26+ base ( message )
2327 {
2428 }
2529
2630 #endregion
2731 }
2832
2933 /// <summary>
30- /// Represents a command line parameter.
34+ /// Represents a command line parameter.
3135 /// Parameters are words in the command line beginning with a hyphen (-).
3236 /// The value of the parameter is the next word in
3337 /// </summary>
@@ -99,7 +103,7 @@ public virtual void SetValue(string value)
99103 }
100104
101105 /// <summary>
102- /// Represents an integer command line parameter.
106+ /// Represents an integer command line parameter.
103107 /// </summary>
104108 public class CmdLineInt : CmdLineParameter
105109 {
@@ -158,25 +162,22 @@ public CmdLineInt(string name, bool required, string helpMessage, int min, int m
158162 public override void SetValue ( string value )
159163 {
160164 base . SetValue ( value ) ;
161- int i ;
162-
165+ int i = 0 ;
163166 try
164167 {
165168 i = Convert . ToInt32 ( value ) ;
166169 }
167170 catch ( Exception )
168171 {
169- throw new CmdLineException ( Name , "Value is not an integer." ) ;
172+ throw new CmdLineException ( base . Name , "Value is not an integer." ) ;
170173 }
171-
172174 if ( i < _min )
173175 {
174- throw new CmdLineException ( Name , $ "Value must be greather or equal to { _min } .") ;
176+ throw new CmdLineException ( base . Name , string . Format ( "Value must be greather or equal to {0 }." , _min ) ) ;
175177 }
176-
177178 if ( i > _max )
178179 {
179- throw new CmdLineException ( Name , $ "Value must be less or equal to { _max } .") ;
180+ throw new CmdLineException ( base . Name , string . Format ( "Value must be less or equal to {0 }." , _max ) ) ;
180181 }
181182 Value = i ;
182183 }
@@ -252,11 +253,11 @@ public CmdLineParameter this[string name]
252253 {
253254 get
254255 {
255- if ( ! parameters . TryGetValue ( name , out CmdLineParameter value ) )
256+ if ( ! parameters . ContainsKey ( name ) )
256257 {
257258 throw new CmdLineException ( name , "Not a registered parameter." ) ;
258259 }
259- return value ;
260+ return parameters [ name ] ;
260261 }
261262 }
262263
@@ -306,8 +307,8 @@ public string[] Parse(string[] args)
306307 if ( args [ i ] . Length > 1 && args [ i ] [ 0 ] == '-' )
307308 {
308309 // The current string is a parameter name
309- string key = args [ i ] [ 1 .. ] . ToLower ( ) ;
310- string value = string . Empty ;
310+ string key = args [ i ] . Substring ( 1 , args [ i ] . Length - 1 ) . ToLower ( ) ;
311+ string value = "" ;
311312 i ++ ;
312313 if ( i < args . Length )
313314 {
@@ -322,17 +323,17 @@ public string[] Parse(string[] args)
322323 i ++ ;
323324 }
324325 }
325- if ( ! parameters . TryGetValue ( key , out CmdLineParameter cmdLineParameter ) )
326+ if ( ! parameters . ContainsKey ( key ) )
326327 {
327328 throw new CmdLineException ( key , "Parameter is not allowed." ) ;
328329 }
329330
330- if ( cmdLineParameter . Exists )
331+ if ( parameters [ key ] . Exists )
331332 {
332333 throw new CmdLineException ( key , "Parameter is specified more than once." ) ;
333334 }
334335
335- cmdLineParameter . SetValue ( value ) ;
336+ parameters [ key ] . SetValue ( value ) ;
336337 }
337338 else
338339 {
@@ -342,7 +343,7 @@ public string[] Parse(string[] args)
342343 }
343344
344345
345- // Check that required parameters are present in the command line.
346+ // Check that required parameters are present in the command line.
346347 foreach ( string key in parameters . Keys )
347348 {
348349 if ( parameters [ key ] . Required && ! parameters [ key ] . Exists )
@@ -393,7 +394,7 @@ public class ConsoleCmdLine : CmdLine
393394
394395 public ConsoleCmdLine ( )
395396 {
396- RegisterParameter ( new CmdLineString ( "help" , false , "Prints the help screen." ) ) ;
397+ base . RegisterParameter ( new CmdLineString ( "help" , false , "Prints the help screen." ) ) ;
397398 }
398399
399400 #endregion
@@ -403,7 +404,7 @@ public ConsoleCmdLine()
403404 public new string [ ] Parse ( string [ ] args )
404405 {
405406 string [ ] ret = null ;
406- string error = string . Empty ;
407+ string error = "" ;
407408 try
408409 {
409410 ret = base . Parse ( args ) ;
@@ -417,15 +418,15 @@ public ConsoleCmdLine()
417418 {
418419 //foreach(string s in base.HelpScreen().Split('\n'))
419420 // Console.WriteLine(s);
420- Console . WriteLine ( HelpScreen ( ) ) ;
421- Environment . Exit ( 0 ) ;
421+ Console . WriteLine ( base . HelpScreen ( ) ) ;
422+ System . Environment . Exit ( 0 ) ;
422423 }
423424
424425 if ( error != "" )
425426 {
426427 Console . WriteLine ( error ) ;
427428 Console . WriteLine ( "Use -help for more information." ) ;
428- Environment . Exit ( 1 ) ;
429+ System . Environment . Exit ( 1 ) ;
429430 }
430431 return ret ;
431432 }
0 commit comments