virtualSoC  0.6.2
baskett
DotArgs.CommandLineArgs Class Reference

Class for defining, validating and processing command line arguments. More...

Public Member Functions

 CommandLineArgs ()
 Initializes a new instance of the CommandLineArgs class. More...
 
void AddExample (string description, string commandLine)
 Adds an example that will be displayed on the help page. More...
 
GetValue< T > (string name)
 Gets the value of an argument. More...
 
void PrintHelp (string errorMessage=null)
 Prints a help message describing the effects of all available options. More...
 
void Process ()
 Processes all registered arguments that have their Argument.Processor set. More...
 
void RegisterAlias (string originalName, string alias)
 Registers an alias for an existing entry. More...
 
void RegisterArgument (string name, Argument arg)
 Registers a new argument. More...
 
void RegisterHelpArgument (string name="help")
 Registers a help argument that will display the help page for the program if set by the user. More...
 
void SetDefaultArgument (string argument)
 Sets the default argument that will be filled when no argument name is given. More...
 
bool Validate (string[] args, OptionalOut< string[]> outErrors=null)
 Processes a set of command line arguments. More...
 
bool Validate (string args, OptionalOut< string[]> outErrors=null)
 Processes a set of command line arguments. More...
 
 CommandLineArgs ()
 Initializes a new instance of the CommandLineArgs class. More...
 
void AddExample (string description, string commandLine)
 Adds an example that will be displayed on the help page. More...
 
GetValue< T > (string name)
 Gets the value of an argument. More...
 
void PrintHelp (string errorMessage=null)
 Prints a help message describing the effects of all available options. More...
 
void Process ()
 Processes all registered arguments that have their Argument.Processor set. More...
 
void RegisterAlias (string originalName, string alias)
 Registers an alias for an existing entry. More...
 
void RegisterArgument (string name, Argument arg)
 Registers a new argument. More...
 
void RegisterHelpArgument (string name="help")
 Registers a help argument that will display the help page for the program if set by the user. More...
 
void SetDefaultArgument (string argument)
 Sets the default argument that will be filled when no argument name is given. More...
 
bool Validate (string[] args, OptionalOut< string[]> outErrors=null)
 Processes a set of command line arguments. More...
 
bool Validate (string args, OptionalOut< string[]> outErrors=null)
 Processes a set of command line arguments. More...
 
 CommandLineArgs ()
 Initializes a new instance of the CommandLineArgs class. More...
 
void AddExample (string description, string commandLine)
 Adds an example that will be displayed on the help page. More...
 
GetValue< T > (string name)
 Gets the value of an argument. More...
 
void PrintHelp (string errorMessage=null)
 Prints a help message describing the effects of all available options. More...
 
void Process ()
 Processes all registered arguments that have their Argument.Processor set. More...
 
void RegisterAlias (string originalName, string alias)
 Registers an alias for an existing entry. More...
 
void RegisterArgument (string name, Argument arg)
 Registers a new argument. More...
 
void RegisterHelpArgument (string name="help")
 Registers a help argument that will display the help page for the program if set by the user. More...
 
void SetDefaultArgument (string argument)
 Sets the default argument that will be filled when no argument name is given. More...
 
bool Validate (string[] args, OptionalOut< string[]> outErrors=null)
 Processes a set of command line arguments. More...
 
bool Validate (string args, OptionalOut< string[]> outErrors=null)
 Processes a set of command line arguments. More...
 

Properties

string ApplicationInfo [get, set]
 Information about the application that will be displayed in the usage page. More...
 
string ExecuteableName [get, set]
 Name of the executeable that will be displayed in the usage page. More...
 
TextWriter OutputWriter [get, set]
 The TextWriter that is used to write the output. The default value is to use Console.Out More...
 

Detailed Description

Class for defining, validating and processing command line arguments.

Constructor & Destructor Documentation

◆ CommandLineArgs() [1/3]

DotArgs.CommandLineArgs.CommandLineArgs ( )

Initializes a new instance of the CommandLineArgs class.

208  {
209  OutputWriter = Console.Out;
210  ExecuteableName = Path.GetFileNameWithoutExtension( Assembly.GetCallingAssembly().Location );
211  }
TextWriter OutputWriter
The TextWriter that is used to write the output. The default value is to use Console.Out
Definition: CommandLineArgs.cs:671
string ExecuteableName
Name of the executeable that will be displayed in the usage page.
Definition: CommandLineArgs.cs:666

◆ CommandLineArgs() [2/3]

DotArgs.CommandLineArgs.CommandLineArgs ( )

Initializes a new instance of the CommandLineArgs class.

208  {
209  OutputWriter = Console.Out;
210  ExecuteableName = Path.GetFileNameWithoutExtension( Assembly.GetCallingAssembly().Location );
211  }
TextWriter OutputWriter
The TextWriter that is used to write the output. The default value is to use Console.Out
Definition: CommandLineArgs.cs:671
string ExecuteableName
Name of the executeable that will be displayed in the usage page.
Definition: CommandLineArgs.cs:666

◆ CommandLineArgs() [3/3]

DotArgs.CommandLineArgs.CommandLineArgs ( )

Initializes a new instance of the CommandLineArgs class.

208  {
209  OutputWriter = Console.Out;
210  ExecuteableName = Path.GetFileNameWithoutExtension( Assembly.GetCallingAssembly().Location );
211  }
TextWriter OutputWriter
The TextWriter that is used to write the output. The default value is to use Console.Out
Definition: CommandLineArgs.cs:671
string ExecuteableName
Name of the executeable that will be displayed in the usage page.
Definition: CommandLineArgs.cs:666

Member Function Documentation

◆ AddExample() [1/3]

void DotArgs.CommandLineArgs.AddExample ( string  description,
string  commandLine 
)

Adds an example that will be displayed on the help page.

Parameters
descriptionThe name or description for this example.
commandLineThe command line to display for this example.
217  {
218  Examples.Add( description, commandLine );
219  }

◆ AddExample() [2/3]

void DotArgs.CommandLineArgs.AddExample ( string  description,
string  commandLine 
)

Adds an example that will be displayed on the help page.

Parameters
descriptionThe name or description for this example.
commandLineThe command line to display for this example.
217  {
218  Examples.Add( description, commandLine );
219  }

◆ AddExample() [3/3]

void DotArgs.CommandLineArgs.AddExample ( string  description,
string  commandLine 
)

Adds an example that will be displayed on the help page.

Parameters
descriptionThe name or description for this example.
commandLineThe command line to display for this example.
217  {
218  Examples.Add( description, commandLine );
219  }

◆ GetValue< T >() [1/3]

T DotArgs.CommandLineArgs.GetValue< T > ( string  name)

Gets the value of an argument.

Parameters
nameName of the argument to read.
Returns
The effective value of the argument. If the argument was omitted in the arguments, the default value will be returned.
Exceptions
System.Collections.Generic.KeyNotFoundExceptionAn argument with the name name was not registered.
231  {
232  if( !Arguments.ContainsKey( name ) )
233  {
234  throw new KeyNotFoundException( string.Format( "An collection with the name {0} was not registered.", name ) );
235  }
236 
237  Argument entry = Arguments[name];
238  return (T)entry.GetValue();
239  }

◆ GetValue< T >() [2/3]

T DotArgs.CommandLineArgs.GetValue< T > ( string  name)

Gets the value of an argument.

Parameters
nameName of the argument to read.
Returns
The effective value of the argument. If the argument was omitted in the arguments, the default value will be returned.
Exceptions
System.Collections.Generic.KeyNotFoundExceptionAn argument with the name name was not registered.
231  {
232  if( !Arguments.ContainsKey( name ) )
233  {
234  throw new KeyNotFoundException( string.Format( "An collection with the name {0} was not registered.", name ) );
235  }
236 
237  Argument entry = Arguments[name];
238  return (T)entry.GetValue();
239  }

◆ GetValue< T >() [3/3]

T DotArgs.CommandLineArgs.GetValue< T > ( string  name)

Gets the value of an argument.

Parameters
nameName of the argument to read.
Returns
The effective value of the argument. If the argument was omitted in the arguments, the default value will be returned.
Exceptions
System.Collections.Generic.KeyNotFoundExceptionAn argument with the name name was not registered.
231  {
232  if( !Arguments.ContainsKey( name ) )
233  {
234  throw new KeyNotFoundException( string.Format( "An collection with the name {0} was not registered.", name ) );
235  }
236 
237  Argument entry = Arguments[name];
238  return (T)entry.GetValue();
239  }

◆ PrintHelp() [1/3]

void DotArgs.CommandLineArgs.PrintHelp ( string  errorMessage = null)

Prints a help message describing the effects of all available options.

Parameters
errorMessageOptional error message to display.
244  {
245  string argList = string.Join( " ", Arguments.OrderBy( k => k.Key ).Select( a => ArgumentToArgList( a.Key, a.Value ) ) );
246 
247  OutputWriter.WriteLine( ApplicationInfo );
248  OutputWriter.WriteLine();
249  if( !string.IsNullOrWhiteSpace( errorMessage ) )
250  {
251  OutputWriter.WriteLine( errorMessage );
252  OutputWriter.WriteLine();
253  }
254  OutputWriter.WriteLine( "Usage:" );
255  OutputWriter.WriteLine( "{0} {1}", ExecuteableName, argList );
256 
257  foreach( KeyValuePair<string, Argument> kvp in Arguments.OrderBy( k => k.Key ) )
258  {
259  OutputWriter.WriteLine();
260  OutputWriter.WriteLine( "{0,-10}{1}", kvp.Key, kvp.Value.HelpMessage );
261  OutputWriter.WriteLine( "{0,-10}{1}", "", GetArgumentInfo( kvp.Value ) );
262  }
263 
264  if( Examples.Any() )
265  {
266  OutputWriter.WriteLine();
267  OutputWriter.WriteLine( "Examples:" );
268 
269  foreach( KeyValuePair<string, string> kvp in Examples.OrderBy( k => k.Key ) )
270  {
271  OutputWriter.WriteLine();
272  OutputWriter.WriteLine( kvp.Key );
273  OutputWriter.WriteLine( kvp.Value );
274  }
275  }
276  }
TextWriter OutputWriter
The TextWriter that is used to write the output. The default value is to use Console.Out
Definition: CommandLineArgs.cs:671
string ApplicationInfo
Information about the application that will be displayed in the usage page.
Definition: CommandLineArgs.cs:659
string ExecuteableName
Name of the executeable that will be displayed in the usage page.
Definition: CommandLineArgs.cs:666

◆ PrintHelp() [2/3]

void DotArgs.CommandLineArgs.PrintHelp ( string  errorMessage = null)

Prints a help message describing the effects of all available options.

Parameters
errorMessageOptional error message to display.
244  {
245  string argList = string.Join( " ", Arguments.OrderBy( k => k.Key ).Select( a => ArgumentToArgList( a.Key, a.Value ) ) );
246 
247  OutputWriter.WriteLine( ApplicationInfo );
248  OutputWriter.WriteLine();
249  if( !string.IsNullOrWhiteSpace( errorMessage ) )
250  {
251  OutputWriter.WriteLine( errorMessage );
252  OutputWriter.WriteLine();
253  }
254  OutputWriter.WriteLine( "Usage:" );
255  OutputWriter.WriteLine( "{0} {1}", ExecuteableName, argList );
256 
257  foreach( KeyValuePair<string, Argument> kvp in Arguments.OrderBy( k => k.Key ) )
258  {
259  OutputWriter.WriteLine();
260  OutputWriter.WriteLine( "{0,-10}{1}", kvp.Key, kvp.Value.HelpMessage );
261  OutputWriter.WriteLine( "{0,-10}{1}", "", GetArgumentInfo( kvp.Value ) );
262  }
263 
264  if( Examples.Any() )
265  {
266  OutputWriter.WriteLine();
267  OutputWriter.WriteLine( "Examples:" );
268 
269  foreach( KeyValuePair<string, string> kvp in Examples.OrderBy( k => k.Key ) )
270  {
271  OutputWriter.WriteLine();
272  OutputWriter.WriteLine( kvp.Key );
273  OutputWriter.WriteLine( kvp.Value );
274  }
275  }
276  }
TextWriter OutputWriter
The TextWriter that is used to write the output. The default value is to use Console.Out
Definition: CommandLineArgs.cs:671
string ApplicationInfo
Information about the application that will be displayed in the usage page.
Definition: CommandLineArgs.cs:659
string ExecuteableName
Name of the executeable that will be displayed in the usage page.
Definition: CommandLineArgs.cs:666

◆ PrintHelp() [3/3]

void DotArgs.CommandLineArgs.PrintHelp ( string  errorMessage = null)

Prints a help message describing the effects of all available options.

Parameters
errorMessageOptional error message to display.
244  {
245  string argList = string.Join( " ", Arguments.OrderBy( k => k.Key ).Select( a => ArgumentToArgList( a.Key, a.Value ) ) );
246 
247  OutputWriter.WriteLine( ApplicationInfo );
248  OutputWriter.WriteLine();
249  if( !string.IsNullOrWhiteSpace( errorMessage ) )
250  {
251  OutputWriter.WriteLine( errorMessage );
252  OutputWriter.WriteLine();
253  }
254  OutputWriter.WriteLine( "Usage:" );
255  OutputWriter.WriteLine( "{0} {1}", ExecuteableName, argList );
256 
257  foreach( KeyValuePair<string, Argument> kvp in Arguments.OrderBy( k => k.Key ) )
258  {
259  OutputWriter.WriteLine();
260  OutputWriter.WriteLine( "{0,-10}{1}", kvp.Key, kvp.Value.HelpMessage );
261  OutputWriter.WriteLine( "{0,-10}{1}", "", GetArgumentInfo( kvp.Value ) );
262  }
263 
264  if( Examples.Any() )
265  {
266  OutputWriter.WriteLine();
267  OutputWriter.WriteLine( "Examples:" );
268 
269  foreach( KeyValuePair<string, string> kvp in Examples.OrderBy( k => k.Key ) )
270  {
271  OutputWriter.WriteLine();
272  OutputWriter.WriteLine( kvp.Key );
273  OutputWriter.WriteLine( kvp.Value );
274  }
275  }
276  }
TextWriter OutputWriter
The TextWriter that is used to write the output. The default value is to use Console.Out
Definition: CommandLineArgs.cs:671
string ApplicationInfo
Information about the application that will be displayed in the usage page.
Definition: CommandLineArgs.cs:659
string ExecuteableName
Name of the executeable that will be displayed in the usage page.
Definition: CommandLineArgs.cs:666

◆ Process() [1/3]

void DotArgs.CommandLineArgs.Process ( )

Processes all registered arguments that have their Argument.Processor set.

282  {
283  foreach( Argument arg in Arguments.Values.Where( a => !( a is AliasArgument ) ) )
284  {
285  if( arg.Processor != null )
286  {
287  arg.Processor( arg.GetValue() );
288  }
289  }
290  }

◆ Process() [2/3]

void DotArgs.CommandLineArgs.Process ( )

Processes all registered arguments that have their Argument.Processor set.

282  {
283  foreach( Argument arg in Arguments.Values.Where( a => !( a is AliasArgument ) ) )
284  {
285  if( arg.Processor != null )
286  {
287  arg.Processor( arg.GetValue() );
288  }
289  }
290  }

◆ Process() [3/3]

void DotArgs.CommandLineArgs.Process ( )

Processes all registered arguments that have their Argument.Processor set.

282  {
283  foreach( Argument arg in Arguments.Values.Where( a => !( a is AliasArgument ) ) )
284  {
285  if( arg.Processor != null )
286  {
287  arg.Processor( arg.GetValue() );
288  }
289  }
290  }

◆ RegisterAlias() [1/3]

void DotArgs.CommandLineArgs.RegisterAlias ( string  originalName,
string  alias 
)

Registers an alias for an existing entry.

Parameters
originalNameName of the original option.
aliasThe alias to add for the option.
Exceptions
System.Collections.Generic.KeyNotFoundExceptionAn entry with the name originalName was not registered.
299  {
300  if( !Arguments.ContainsKey( originalName ) )
301  {
302  throw new KeyNotFoundException( string.Format( "An entry with the name {0} was not registered.", originalName ) );
303  }
304 
305  AliasArgument entry = new AliasArgument( Arguments[originalName] );
306  Arguments[alias] = entry;
307  }

◆ RegisterAlias() [2/3]

void DotArgs.CommandLineArgs.RegisterAlias ( string  originalName,
string  alias 
)

Registers an alias for an existing entry.

Parameters
originalNameName of the original option.
aliasThe alias to add for the option.
Exceptions
System.Collections.Generic.KeyNotFoundExceptionAn entry with the name originalName was not registered.
299  {
300  if( !Arguments.ContainsKey( originalName ) )
301  {
302  throw new KeyNotFoundException( string.Format( "An entry with the name {0} was not registered.", originalName ) );
303  }
304 
305  AliasArgument entry = new AliasArgument( Arguments[originalName] );
306  Arguments[alias] = entry;
307  }

◆ RegisterAlias() [3/3]

void DotArgs.CommandLineArgs.RegisterAlias ( string  originalName,
string  alias 
)

Registers an alias for an existing entry.

Parameters
originalNameName of the original option.
aliasThe alias to add for the option.
Exceptions
System.Collections.Generic.KeyNotFoundExceptionAn entry with the name originalName was not registered.
299  {
300  if( !Arguments.ContainsKey( originalName ) )
301  {
302  throw new KeyNotFoundException( string.Format( "An entry with the name {0} was not registered.", originalName ) );
303  }
304 
305  AliasArgument entry = new AliasArgument( Arguments[originalName] );
306  Arguments[alias] = entry;
307  }

◆ RegisterArgument() [1/3]

void DotArgs.CommandLineArgs.RegisterArgument ( string  name,
Argument  arg 
)

Registers a new argument.

Parameters
nameName of the argument to register.
argThe argument's configuration.
313  {
314  Arguments[name] = arg;
315  }

◆ RegisterArgument() [2/3]

void DotArgs.CommandLineArgs.RegisterArgument ( string  name,
Argument  arg 
)

Registers a new argument.

Parameters
nameName of the argument to register.
argThe argument's configuration.
313  {
314  Arguments[name] = arg;
315  }

◆ RegisterArgument() [3/3]

void DotArgs.CommandLineArgs.RegisterArgument ( string  name,
Argument  arg 
)

Registers a new argument.

Parameters
nameName of the argument to register.
argThe argument's configuration.
313  {
314  Arguments[name] = arg;
315  }

◆ RegisterHelpArgument() [1/3]

void DotArgs.CommandLineArgs.RegisterHelpArgument ( string  name = "help")

Registers a help argument that will display the help page for the program if set by the user.

Parameters
nameName of the flag. The default value is "help".
322  {
323  FlagArgument arg = new FlagArgument();
324  arg.Processor = ( v ) => PrintHelp();
325  arg.HelpMessage = "Displays this help.";
326 
327  RegisterArgument( name, arg );
328  }
void PrintHelp(string errorMessage=null)
Prints a help message describing the effects of all available options.
Definition: CommandLineArgs.cs:243
void RegisterArgument(string name, Argument arg)
Registers a new argument.
Definition: CommandLineArgs.cs:312

◆ RegisterHelpArgument() [2/3]

void DotArgs.CommandLineArgs.RegisterHelpArgument ( string  name = "help")

Registers a help argument that will display the help page for the program if set by the user.

Parameters
nameName of the flag. The default value is "help".
322  {
323  FlagArgument arg = new FlagArgument();
324  arg.Processor = ( v ) => PrintHelp();
325  arg.HelpMessage = "Displays this help.";
326 
327  RegisterArgument( name, arg );
328  }
void PrintHelp(string errorMessage=null)
Prints a help message describing the effects of all available options.
Definition: CommandLineArgs.cs:243
void RegisterArgument(string name, Argument arg)
Registers a new argument.
Definition: CommandLineArgs.cs:312

◆ RegisterHelpArgument() [3/3]

void DotArgs.CommandLineArgs.RegisterHelpArgument ( string  name = "help")

Registers a help argument that will display the help page for the program if set by the user.

Parameters
nameName of the flag. The default value is "help".
322  {
323  FlagArgument arg = new FlagArgument();
324  arg.Processor = ( v ) => PrintHelp();
325  arg.HelpMessage = "Displays this help.";
326 
327  RegisterArgument( name, arg );
328  }
void PrintHelp(string errorMessage=null)
Prints a help message describing the effects of all available options.
Definition: CommandLineArgs.cs:243
void RegisterArgument(string name, Argument arg)
Registers a new argument.
Definition: CommandLineArgs.cs:312

◆ SetDefaultArgument() [1/3]

void DotArgs.CommandLineArgs.SetDefaultArgument ( string  argument)

Sets the default argument that will be filled when no argument name is given.

Parameters
argumentName of the argument to use as the default.
335  {
336  if( !Arguments.ContainsKey( argument ) )
337  {
338  throw new ArgumentException( string.Format( "Argument {0} was not registered", argument ), "argument" );
339  }
340 
341  DefaultArgument = argument;
342  }

◆ SetDefaultArgument() [2/3]

void DotArgs.CommandLineArgs.SetDefaultArgument ( string  argument)

Sets the default argument that will be filled when no argument name is given.

Parameters
argumentName of the argument to use as the default.
335  {
336  if( !Arguments.ContainsKey( argument ) )
337  {
338  throw new ArgumentException( string.Format( "Argument {0} was not registered", argument ), "argument" );
339  }
340 
341  DefaultArgument = argument;
342  }

◆ SetDefaultArgument() [3/3]

void DotArgs.CommandLineArgs.SetDefaultArgument ( string  argument)

Sets the default argument that will be filled when no argument name is given.

Parameters
argumentName of the argument to use as the default.
335  {
336  if( !Arguments.ContainsKey( argument ) )
337  {
338  throw new ArgumentException( string.Format( "Argument {0} was not registered", argument ), "argument" );
339  }
340 
341  DefaultArgument = argument;
342  }

◆ Validate() [1/6]

bool DotArgs.CommandLineArgs.Validate ( string []  args,
OptionalOut< string[]>  outErrors = null 
)

Processes a set of command line arguments.

Parameters
argsCommand line arguments to process. This is usally coming from your Main method.
outErrorsOptional "out" parameter that holds error strings for every encountered error.
Returns
true if the arguments in args are valid; otherwise false .
356  {
357  return Validate( string.Join( " ", args ), outErrors );
358  }
bool Validate(string[] args, OptionalOut< string[]> outErrors=null)
Processes a set of command line arguments.
Definition: CommandLineArgs.cs:355

◆ Validate() [2/6]

bool DotArgs.CommandLineArgs.Validate ( string []  args,
OptionalOut< string[]>  outErrors = null 
)

Processes a set of command line arguments.

Parameters
argsCommand line arguments to process. This is usally coming from your Main method.
outErrorsOptional "out" parameter that holds error strings for every encountered error.
Returns
true if the arguments in args are valid; otherwise false .
356  {
357  return Validate( string.Join( " ", args ), outErrors );
358  }
bool Validate(string[] args, OptionalOut< string[]> outErrors=null)
Processes a set of command line arguments.
Definition: CommandLineArgs.cs:355

◆ Validate() [3/6]

bool DotArgs.CommandLineArgs.Validate ( string []  args,
OptionalOut< string[]>  outErrors = null 
)

Processes a set of command line arguments.

Parameters
argsCommand line arguments to process. This is usally coming from your Main method.
outErrorsOptional "out" parameter that holds error strings for every encountered error.
Returns
true if the arguments in args are valid; otherwise false .
356  {
357  return Validate( string.Join( " ", args ), outErrors );
358  }
bool Validate(string[] args, OptionalOut< string[]> outErrors=null)
Processes a set of command line arguments.
Definition: CommandLineArgs.cs:355

◆ Validate() [4/6]

bool DotArgs.CommandLineArgs.Validate ( string  args,
OptionalOut< string[]>  outErrors = null 
)

Processes a set of command line arguments.

Parameters
argsCommand line arguments to process. This is usally coming from your Main method.
outErrorsOptional "out" parameter that holds error strings for every encountered error.
Returns
true if the arguments in args are valid; otherwise false .
372  {
373  Reset();
374 
375  bool ignoreAlreadyHandled = false;
376  if( DefaultArgument != null )
377  {
378  ignoreAlreadyHandled = Arguments[DefaultArgument].SupportsMultipleValues;
379  }
380 
381  bool handledDefault = false;
382  bool errors = false;
383  List<string> errorList = new List<string>();
384 
385  List<string> parts = SplitCommandLine( args );
386  for( int i = 0; i < parts.Count; ++i )
387  {
388  string arg = GetArgName( parts[i] );
389  if( !IsArgumentName( parts[i] ) )
390  {
391  if( !handledDefault || ignoreAlreadyHandled )
392  {
393  parts[i] = string.Format( "/{0}={1}", DefaultArgument, arg );
394  arg = DefaultArgument;
395 
396  handledDefault = true;
397  }
398  }
399 
400  if( !Arguments.ContainsKey( arg ) )
401  {
402  if( DefaultArgument != null && ( !handledDefault || ignoreAlreadyHandled ) )
403  {
404  parts[i] = string.Format( "/{0}={1}", DefaultArgument, arg );
405  arg = DefaultArgument;
406 
407  handledDefault = true;
408  }
409  else
410  {
411  errorList.Add( string.Format( "Unknown option: '{0}'", arg ) );
412 
413  errors = true;
414  continue;
415  }
416  }
417 
418  Argument entry = Arguments[arg];
419 
420  if( entry.NeedsValue )
421  {
422  // Not so simple cases: Collection and Option
423  string value = ExtractValueFromArg( parts[i] );
424 
425  if( value == null && i < parts.Count - 1 )
426  {
427  value = parts[i + 1];
428 
429  if( Arguments.ContainsKey( GetArgName( value ) ) )
430  {
431  value = null;
432  }
433  else
434  {
435  i++;
436  }
437  }
438 
439  if( value != null )
440  {
441  entry.SetValue( value );
442  }
443  else
444  {
445  // Missing argument
446  errorList.Add( string.Format( "Missing value for option '{0}'", arg ) );
447  errors = true;
448  }
449  }
450  else // Simple case: a flag
451  {
452  entry.SetValue( true );
453  }
454  }
455 
456  foreach( KeyValuePair<string, Argument> kvp in Arguments )
457  {
458  Argument entry = kvp.Value;
459  object value = entry.GetValue();
460 
461  if( entry.IsRequired && value == null )
462  {
463  errorList.Add( string.Format( "Missing value for option '{0}'", kvp.Key ) );
464  errors = true;
465  }
466 
467  if( !entry.Validate( value ) )
468  {
469  errorList.Add( string.Format( "{0}: Invalid value {1}", kvp.Key, value ) );
470  errors = true;
471  }
472  }
473 
474  if( outErrors != null )
475  {
476  outErrors.Result = errorList.Distinct().ToArray();
477  }
478 
479  return !errors;
480  }

◆ Validate() [5/6]

bool DotArgs.CommandLineArgs.Validate ( string  args,
OptionalOut< string[]>  outErrors = null 
)

Processes a set of command line arguments.

Parameters
argsCommand line arguments to process. This is usally coming from your Main method.
outErrorsOptional "out" parameter that holds error strings for every encountered error.
Returns
true if the arguments in args are valid; otherwise false .
372  {
373  Reset();
374 
375  bool ignoreAlreadyHandled = false;
376  if( DefaultArgument != null )
377  {
378  ignoreAlreadyHandled = Arguments[DefaultArgument].SupportsMultipleValues;
379  }
380 
381  bool handledDefault = false;
382  bool errors = false;
383  List<string> errorList = new List<string>();
384 
385  List<string> parts = SplitCommandLine( args );
386  for( int i = 0; i < parts.Count; ++i )
387  {
388  string arg = GetArgName( parts[i] );
389  if( !IsArgumentName( parts[i] ) )
390  {
391  if( !handledDefault || ignoreAlreadyHandled )
392  {
393  parts[i] = string.Format( "/{0}={1}", DefaultArgument, arg );
394  arg = DefaultArgument;
395 
396  handledDefault = true;
397  }
398  }
399 
400  if( !Arguments.ContainsKey( arg ) )
401  {
402  if( DefaultArgument != null && ( !handledDefault || ignoreAlreadyHandled ) )
403  {
404  parts[i] = string.Format( "/{0}={1}", DefaultArgument, arg );
405  arg = DefaultArgument;
406 
407  handledDefault = true;
408  }
409  else
410  {
411  errorList.Add( string.Format( "Unknown option: '{0}'", arg ) );
412 
413  errors = true;
414  continue;
415  }
416  }
417 
418  Argument entry = Arguments[arg];
419 
420  if( entry.NeedsValue )
421  {
422  // Not so simple cases: Collection and Option
423  string value = ExtractValueFromArg( parts[i] );
424 
425  if( value == null && i < parts.Count - 1 )
426  {
427  value = parts[i + 1];
428 
429  if( Arguments.ContainsKey( GetArgName( value ) ) )
430  {
431  value = null;
432  }
433  else
434  {
435  i++;
436  }
437  }
438 
439  if( value != null )
440  {
441  entry.SetValue( value );
442  }
443  else
444  {
445  // Missing argument
446  errorList.Add( string.Format( "Missing value for option '{0}'", arg ) );
447  errors = true;
448  }
449  }
450  else // Simple case: a flag
451  {
452  entry.SetValue( true );
453  }
454  }
455 
456  foreach( KeyValuePair<string, Argument> kvp in Arguments )
457  {
458  Argument entry = kvp.Value;
459  object value = entry.GetValue();
460 
461  if( entry.IsRequired && value == null )
462  {
463  errorList.Add( string.Format( "Missing value for option '{0}'", kvp.Key ) );
464  errors = true;
465  }
466 
467  if( !entry.Validate( value ) )
468  {
469  errorList.Add( string.Format( "{0}: Invalid value {1}", kvp.Key, value ) );
470  errors = true;
471  }
472  }
473 
474  if( outErrors != null )
475  {
476  outErrors.Result = errorList.Distinct().ToArray();
477  }
478 
479  return !errors;
480  }

◆ Validate() [6/6]

bool DotArgs.CommandLineArgs.Validate ( string  args,
OptionalOut< string[]>  outErrors = null 
)

Processes a set of command line arguments.

Parameters
argsCommand line arguments to process. This is usally coming from your Main method.
outErrorsOptional "out" parameter that holds error strings for every encountered error.
Returns
true if the arguments in args are valid; otherwise false .
372  {
373  Reset();
374 
375  bool ignoreAlreadyHandled = false;
376  if( DefaultArgument != null )
377  {
378  ignoreAlreadyHandled = Arguments[DefaultArgument].SupportsMultipleValues;
379  }
380 
381  bool handledDefault = false;
382  bool errors = false;
383  List<string> errorList = new List<string>();
384 
385  List<string> parts = SplitCommandLine( args );
386  for( int i = 0; i < parts.Count; ++i )
387  {
388  string arg = GetArgName( parts[i] );
389  if( !IsArgumentName( parts[i] ) )
390  {
391  if( !handledDefault || ignoreAlreadyHandled )
392  {
393  parts[i] = string.Format( "/{0}={1}", DefaultArgument, arg );
394  arg = DefaultArgument;
395 
396  handledDefault = true;
397  }
398  }
399 
400  if( !Arguments.ContainsKey( arg ) )
401  {
402  if( DefaultArgument != null && ( !handledDefault || ignoreAlreadyHandled ) )
403  {
404  parts[i] = string.Format( "/{0}={1}", DefaultArgument, arg );
405  arg = DefaultArgument;
406 
407  handledDefault = true;
408  }
409  else
410  {
411  errorList.Add( string.Format( "Unknown option: '{0}'", arg ) );
412 
413  errors = true;
414  continue;
415  }
416  }
417 
418  Argument entry = Arguments[arg];
419 
420  if( entry.NeedsValue )
421  {
422  // Not so simple cases: Collection and Option
423  string value = ExtractValueFromArg( parts[i] );
424 
425  if( value == null && i < parts.Count - 1 )
426  {
427  value = parts[i + 1];
428 
429  if( Arguments.ContainsKey( GetArgName( value ) ) )
430  {
431  value = null;
432  }
433  else
434  {
435  i++;
436  }
437  }
438 
439  if( value != null )
440  {
441  entry.SetValue( value );
442  }
443  else
444  {
445  // Missing argument
446  errorList.Add( string.Format( "Missing value for option '{0}'", arg ) );
447  errors = true;
448  }
449  }
450  else // Simple case: a flag
451  {
452  entry.SetValue( true );
453  }
454  }
455 
456  foreach( KeyValuePair<string, Argument> kvp in Arguments )
457  {
458  Argument entry = kvp.Value;
459  object value = entry.GetValue();
460 
461  if( entry.IsRequired && value == null )
462  {
463  errorList.Add( string.Format( "Missing value for option '{0}'", kvp.Key ) );
464  errors = true;
465  }
466 
467  if( !entry.Validate( value ) )
468  {
469  errorList.Add( string.Format( "{0}: Invalid value {1}", kvp.Key, value ) );
470  errors = true;
471  }
472  }
473 
474  if( outErrors != null )
475  {
476  outErrors.Result = errorList.Distinct().ToArray();
477  }
478 
479  return !errors;
480  }

Property Documentation

◆ ApplicationInfo

string DotArgs.CommandLineArgs.ApplicationInfo
getset

Information about the application that will be displayed in the usage page.

MyCoolProgram v1.2 Copyright (C) John Smith <smith.nosp@m.@exa.nosp@m.mple..nosp@m.com>

◆ ExecuteableName

string DotArgs.CommandLineArgs.ExecuteableName
getset

Name of the executeable that will be displayed in the usage page.

The default value for this is the name of the assembly containing the code that created this object.

◆ OutputWriter

TextWriter DotArgs.CommandLineArgs.OutputWriter
getset

The TextWriter that is used to write the output. The default value is to use Console.Out


The documentation for this class was generated from the following file: