Project Description

 The project implements a command line parser to ease the work with command line context. The following is a list of currently supported features:

  • A single command to parse all the options;
  • A single location to define all option set behaviors;
  • Default values for arguments;
  • Boolean flag arguments - for example /bcd can be interpreted (if defined as separate boolean options) as separate boolean arguments, if they are specified as such;
  • A boolean argument can have its value set to true only by specifying the argument itself, if the default value is not set differently. For example ‘/b’ would be the same as ‘/b=true’;
  • Multiple delimiters support: each argument can be delimited by ‘/’ and ‘-‘ and their values by ‘=’ or ‘:’. If a value has to include spaces, it must be enclosed by “ or ‘.
  • Support for command aliases;
  • Support for command groups - an option set can contain multiple groups, each argument being required or not for a specific group;
  • Custom type converters - besides the default type converters, an argument can implement a custom type, which is recognized by the parser using a custom type converter;
  • A help system that has the possibility to override the contents; it also can be called by specifying ‘/?’ or ‘/help’ as arguments in command line;
  • Possibility to implement custom validations;
  • Support to display usage scenarios for arguments;

 

Example:

    public class Options : OptionSet
    {
        [Option("v", "Specifies to display all output messages to the console window.", Name = "verbose mode", Alias = "verbose", Required = true)]
        public bool Verbose { get; set; }

        protected override string[] GetUsageScenarios()
        {
            List<string> list = new List<string>();

            list.Add("[-v|-verbose]");

            return list.ToArray();
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Options opt = CommandLineParser<Options>.GetOptions();
        }
    } 

 

Defining argument groups

There are times when an argument may be required in certain situations, but optional in others. In this case a group comes in hand. For example:

public class IOOperationOptions : OptionSet
    {
        [Option("d", "Specifies the directory name.", Name = "Directory name", Alias = "dir")]
        [OptionGroup("folderOps", Required = true)]
        public string DirectoryName { get; set; }

        [Option("f", "Specifies the file name.", Name = "File name", Alias = "file")]
        [OptionGroup("fileOps", Required = true)]
        public string FileName { get; set; }

        protected override string[] GetUsageScenarios()
        {
            List<string> list = new List<string>();

            list.Add("[-d|-f]");

            return list.ToArray();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IOOperationOptions opt = CommandLineParser<IOOperationOptions>.GetOptions();

            switch (opt.GetGroup())
            {
                case "fileOps":
                    string fileName = opt.FileName;

                    //code omitted for brevity
                    break;
                case "folderOps":
                    string folderName = opt.DirectoryName;

                    //code omitted for brevity
                    break;
                default:
                    break;
            }
        }
    }

   

Defining Custom type converters

In order to have a property with a custom type, you must implement a command parser type converter, like the example below:

    public class TestType
    {
        public int Value { get; set; }

        public TestType() { }
    }

    class TestConverter : BaseTypeConverter<TestType>
    {
        public override TestType ConvertFromString(string value)
        {
            TestType t = new TestType();
            t.Value = int.Parse(value);
            return t;
        }

        public override string ConvertToString(object value)
        {
            if (value == null)
            {
                return null;
            }
            return (value as TestType).Value.ToString();
        }
    }

    class Options : OptionSet
    {
        [Option("t", "A new type", Name = "dummy type")]
        public TestType TestT { get; set; }

        public override Cmd.TypeConverters.ITypeConverter[] GetCustomConverters()
        {
            return new Cmd.TypeConverters.ITypeConverter[1] { new TestConverter() };
        }
    }

Note: The library has built in type converters for the following types, with their nullable representation where the case requires: Boolean, Byte, Char, DateTime, DBNull, Decimal, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64, Array, Collection, Enum.

Last edited Jan 19, 2012 at 12:25 PM by ionut4net, version 25