Adding support for Generics

Aug 25, 2009 at 6:13 AM
Edited Aug 25, 2009 at 6:14 AM

This is very nice library. Especially, I like the fact that it mimics modern Linux syntax.

It would be very convenient if this library provides simpler API by using C# generics.

Please consider this code:

namespace CommandLineParser
{
 /// 
 /// Defines generic Parser
 /// 
 internal class Parser<T> : CommandLineParser.CommandLineParser where T : class, new()
 {
  /// 
  /// Initializes a new instance of the  class.
  /// 
  public Parser()
  {
   Values = new T();
   ExtractArgumentAttributes(Values);
  }
  /// 
  /// Gets the parsed values object.
  /// 
  /// The values.
  public T Values { get; private set; }
 }
}

Then if I write my class for argument values as

internal class Arguments
{
  [SwitchArgument('a', "arga")]
  public bool ArgA { get; set; }

  [SwitchArgument('b', "argb")]
  public bool ArgB { get; set; }
}

Then I can use this new Parser class in the Main(string[] args) as

var parser = new Parser<Arguments>();

parser.ParseCommandLine(args);

var argA = parser.Values.ArgA;

var argB = parser.Values.ArgB;

What do you think?

 

Coordinator
Feb 21, 2011 at 6:57 PM

Certainly seems a good suggestion.