33
44/*
55 * Taken from https://cmdline.codeplex.com/
6- *
6+ *
77 */
88
99namespace LogExpert . Classes
@@ -15,23 +15,19 @@ public class CmdLineException : Exception
1515 {
1616 #region cTor
1717
18- public CmdLineException ( string parameter , string message )
19- :
20- base ( string . Format ( "Syntax error of parameter -{0}: {1}" , parameter , message ) )
18+ public CmdLineException ( string parameter , string message ) : base ( $ "Syntax error of parameter -{ parameter } : { message } ")
2119 {
2220 }
2321
24- public CmdLineException ( string message )
25- :
26- base ( message )
22+ public CmdLineException ( string message ) : base ( message )
2723 {
2824 }
2925
3026 #endregion
3127 }
3228
3329 /// <summary>
34- /// Represents a command line parameter.
30+ /// Represents a command line parameter.
3531 /// Parameters are words in the command line beginning with a hyphen (-).
3632 /// The value of the parameter is the next word in
3733 /// </summary>
@@ -103,7 +99,7 @@ public virtual void SetValue(string value)
10399 }
104100
105101 /// <summary>
106- /// Represents an integer command line parameter.
102+ /// Represents an integer command line parameter.
107103 /// </summary>
108104 public class CmdLineInt : CmdLineParameter
109105 {
@@ -162,22 +158,25 @@ public CmdLineInt(string name, bool required, string helpMessage, int min, int m
162158 public override void SetValue ( string value )
163159 {
164160 base . SetValue ( value ) ;
165- int i = 0 ;
161+ int i ;
162+
166163 try
167164 {
168165 i = Convert . ToInt32 ( value ) ;
169166 }
170167 catch ( Exception )
171168 {
172- throw new CmdLineException ( base . Name , "Value is not an integer." ) ;
169+ throw new CmdLineException ( Name , "Value is not an integer." ) ;
173170 }
171+
174172 if ( i < _min )
175173 {
176- throw new CmdLineException ( base . Name , string . Format ( "Value must be greather or equal to {0 }." , _min ) ) ;
174+ throw new CmdLineException ( Name , $ "Value must be greather or equal to { _min } .") ;
177175 }
176+
178177 if ( i > _max )
179178 {
180- throw new CmdLineException ( base . Name , string . Format ( "Value must be less or equal to {0 }." , _max ) ) ;
179+ throw new CmdLineException ( Name , $ "Value must be less or equal to { _max } .") ;
181180 }
182181 Value = i ;
183182 }
@@ -253,11 +252,11 @@ public CmdLineParameter this[string name]
253252 {
254253 get
255254 {
256- if ( ! parameters . ContainsKey ( name ) )
255+ if ( ! parameters . TryGetValue ( name , out CmdLineParameter value ) )
257256 {
258257 throw new CmdLineException ( name , "Not a registered parameter." ) ;
259258 }
260- return parameters [ name ] ;
259+ return value ;
261260 }
262261 }
263262
@@ -307,8 +306,8 @@ public string[] Parse(string[] args)
307306 if ( args [ i ] . Length > 1 && args [ i ] [ 0 ] == '-' )
308307 {
309308 // The current string is a parameter name
310- string key = args [ i ] . Substring ( 1 , args [ i ] . Length - 1 ) . ToLower ( ) ;
311- string value = "" ;
309+ string key = args [ i ] [ 1 .. ] . ToLower ( ) ;
310+ string value = string . Empty ;
312311 i ++ ;
313312 if ( i < args . Length )
314313 {
@@ -323,17 +322,17 @@ public string[] Parse(string[] args)
323322 i ++ ;
324323 }
325324 }
326- if ( ! parameters . ContainsKey ( key ) )
325+ if ( ! parameters . TryGetValue ( key , out CmdLineParameter cmdLineParameter ) )
327326 {
328327 throw new CmdLineException ( key , "Parameter is not allowed." ) ;
329328 }
330329
331- if ( parameters [ key ] . Exists )
330+ if ( cmdLineParameter . Exists )
332331 {
333332 throw new CmdLineException ( key , "Parameter is specified more than once." ) ;
334333 }
335334
336- parameters [ key ] . SetValue ( value ) ;
335+ cmdLineParameter . SetValue ( value ) ;
337336 }
338337 else
339338 {
@@ -343,7 +342,7 @@ public string[] Parse(string[] args)
343342 }
344343
345344
346- // Check that required parameters are present in the command line.
345+ // Check that required parameters are present in the command line.
347346 foreach ( string key in parameters . Keys )
348347 {
349348 if ( parameters [ key ] . Required && ! parameters [ key ] . Exists )
@@ -394,7 +393,7 @@ public class ConsoleCmdLine : CmdLine
394393
395394 public ConsoleCmdLine ( )
396395 {
397- base . RegisterParameter ( new CmdLineString ( "help" , false , "Prints the help screen." ) ) ;
396+ RegisterParameter ( new CmdLineString ( "help" , false , "Prints the help screen." ) ) ;
398397 }
399398
400399 #endregion
@@ -404,7 +403,7 @@ public ConsoleCmdLine()
404403 public new string [ ] Parse ( string [ ] args )
405404 {
406405 string [ ] ret = null ;
407- string error = "" ;
406+ string error = string . Empty ;
408407 try
409408 {
410409 ret = base . Parse ( args ) ;
@@ -418,15 +417,15 @@ public ConsoleCmdLine()
418417 {
419418 //foreach(string s in base.HelpScreen().Split('\n'))
420419 // Console.WriteLine(s);
421- Console . WriteLine ( base . HelpScreen ( ) ) ;
422- System . Environment . Exit ( 0 ) ;
420+ Console . WriteLine ( HelpScreen ( ) ) ;
421+ Environment . Exit ( 0 ) ;
423422 }
424423
425424 if ( error != "" )
426425 {
427426 Console . WriteLine ( error ) ;
428427 Console . WriteLine ( "Use -help for more information." ) ;
429- System . Environment . Exit ( 1 ) ;
428+ Environment . Exit ( 1 ) ;
430429 }
431430 return ret ;
432431 }
0 commit comments