Click here to Skip to main content
11,501,513 members (76,139 online)
Click here to Skip to main content

Cinchoo - Simplified Command Line Argument Parser

, 21 Aug 2014 CPOL 10.9K 41 34
Rate this:
Please Sign up or sign in to vote.
Easy to use command line argument parser, with loaded features like usage creation, type-safe management etc

Contents

1. Introduction

Cinchoo is the application framework for .NET. One of the main functionalities it provides to the users is application configuration management. Application configuration is the information that application reads and/or writes at run-time from the source.

Most programs accept command line arguments in some forms. It is repetitive and boring task to be programmed to parse and consume the command line arguments. Making it as library letting you to concentrate yourself on the core development tasks. There are many parsers available to use and does the job for you. Cinchoo is one among them, but very easy to use in your project. Try and feel for yourself.

Cinchoo provides a clean and easy API to consume command line arguments. It allows you

  1. To display usage text
  2. Way to report syntex errors
  3. Type-safe object management
  4. Support of value converters
  5. Support of custom validations
  6. Read commandline parameters from file etc. 

2. Requirement

This command line library is written in C# for the .NET 4.0 Framework. It is part of Cinchoo framework, which is a great library with lot of features like Configuration Management, common ApplicationHost, Shell features etc.

3. "Hello World!" Sample

Lets begin by looking into a simple example of an application accepting two command line arguments name (-name) and message (-msg). name is required argument and msg is optional argument with default value "Good Morning". If msg is not passed, it will take the default value.

  • Download the latest Cinchoo binary here. (Nuget Command: Install-Package Cinchoo)
  • Open VS.NET 2010 or higher
  • Create a sample VS.NET (.NET Framework 4) Console Application project
  • Add reference to Cinchoo.Core.dll
  • Use the Cinchoo.Core.Shell namespace
  • Copy and paste the below command line object

Listing 3.1 Defining Command Line Object

[ChoCommandLineArgObject(ApplicationName = "Hello world", Copyright = "Copyright 2014 Cinchoo Inc.")]
public class HelloWorldCmdLineParams : ChoCommandLineArgObject
{
    [ChoCommandLineArg("name", IsRequired = true, Description = "Name of the person.")]
    public string Name
    {
        get;
        set;
    }
    [ChoCommandLineArg("msg", DefaultValue = "Good Morning", Description = "Greeting message.")]
    public string Message
    {
        get;
        set;
    }

    public override string ToString()
    {
        return "{0}! {1}.".FormatString(Message, Name);
    }
}

The code above illustrates about defining command line argument object. First thing define a command line argument (ex. HelloWorldCmdLineParams) class from ChoCommandLineArgObject, it indicates that this object is a command line argument object. And it must be decorated with ChoCommandLineArgObjectAttribute to complete the definition. In this example, we specify the name of application and copyright message. These values are used by parser to display the formatted message on the console window. If these options are not specified, AssemblyTitle and AssemblyCopyright of the entry assembly will be defaulted to these members respectively.

Define command line argument members name and msg either as public fields or properies with get and set in it. Decorate them with ChoCommandLineArg attribute to indicate that they are command line arguments. In this sample, name property is given name as command line switch with IsRequired as true. It means that this argument must be passed with value as command line argument to this executable when it runs. Description is specified to give short help text to this option. Message property is given msg switch name with DefaultValue. It is an optional command line argument, if not passed with value, it will be defaulted to "Good Morning" text.

It is very simple and clear way of specifying command line options using attributes.

Listing 3.2 Main Method

class Program
{
    static void Main(string[] args)
    {
        HelloWorldCmdLineParams cmdLineParams = new HelloWorldCmdLineParams();
        Console.WriteLine(cmdLineParams.ToString());
    }
}

We start by creating new instance of HelloWorldCmdLineParams object, That all. All the heavy lifting of parsing and loading command line arguments to the object done by the parser under the hood. If you run the executable with name argument value as Raj, it will display "Good Morning! Raj."

Listed below are test runs of the programs with different set of arguments and its output.

Listing 3.3 Test1.exe with no arguments

>Test1.exe
Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

Missing arg value for 'name' required command line switch.

Test1.exe [/msg:<string>] /name:<string>

        /msg    Greeting message.
        /name   Name of the person.

Listing 3.4 Test1.exe with /?

>Test1.exe /?
Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

Test1.exe [/msg:<string>] /name:<string>

        /msg    Greeting message.
        /name   Name of the person.

Listing 3.5 Test1.exe with -name argument

>Test1.exe /name:Raj
Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

Good Morning! Raj.

Listing 3.6 Test1.exe with quoted argument

>Test1.exe /name:"Raj Nagalingam"
Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

Good Morning! Raj Nagalingam.

Listing 3.7 Test1.exe with -name and -msg arguments

>Test1.exe /name:"Raj Nagalingam" /msg:"Hello world"
Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

Hello world! Raj Nagalingam.

Listing 3.8 Test1.exe with error conditions

>Test1.exe /name:" " /msg:"Hello world"
Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

Missing arg value for 'name' required command line switch.

ConsoleApplication2.exe [/msg:<string>] /name:<string>

        /msg    Greeting message.
        /name   Name of the person.

So far we have seen some of the samples of using Command line arguments in the executable. Try with some other combinations by yourself to test it out.

4. Types of Command Line arguments

There are two types of command line arguments available to choose from

  1. Positional Command Line Arguments
  2. Switch Named Command Line Arguments

4.1 Positional Command Line Argument

It is an argument defined to accept value from command line arguments from a specified position. Position starts with value 1. A command line object member decorated with ChoPositionalCommandLineArgAttribute will be called positional argument. 

Listing 4.1 Command Line Argument Object with Positional Arguments

[ChoCommandLineArgObject(ApplicationName = "Hello world", Copyright = "Copyright 2014 Cinchoo Inc.")]
public class HelloWorldCmdLineParams : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "Pos1")]
    public string PosArg1;

    [ChoPositionalCommandLineArg(2, "Pos2")]
    public string PosArg2;

    [ChoCommandLineArg("name1", Aliases="n1, m", IsRequired = true, Description = "Name of the person.")]
    public string Name;

    [ChoCommandLineArg("msg", DefaultValue = "Good Morning", Description = "Greeting message.")]
    public string Message
    {
        get;
        set;
    }
}

In the sample above illustrates that PosArg1 and PosArg2 are positional command line arguments. The parser looks at the passed command line arguments, load them with appropriate positional values to them.

Listing 4.2 Test1.exe with positional arguments

>Test1.exe CPos1 /name:"Raj" /msg:"Hello world" CPos2

Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

-- ConsoleApplication2.HelloWorldCmdLineParams State --
        Message: Hello world
        PosArg1: CPos1
        PosArg2: CPos2
        Name: Raj

After executing the program with the above command line argument, the above object members PosArg1 and PosArg2 will be loaded with APos1 and APos2 values. Please note, the positional argument values are mixed up with Switch Named command line arguments. When it comes to parse and load the positional arguments, parser identifies position arguments in the order from left to right starting with position 1 and loads them.

4.2 Switch Named Command Line Argument

It is an argument defined to accept value from command line arguments for a specified command line switch. A command line object member decorated with ChoCommandLineArgAttribute will be called switch named command line argument. 

Listing 4.3 Command Line Argument Object with Switch Named Arguments

[ChoCommandLineArgObject(ApplicationName = "Hello world", Copyright = "Copyright 2014 Cinchoo Inc.")]
public class HelloWorldCmdLineParams : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "Pos1")]
    public string PosArg1;

    [ChoPositionalCommandLineArg(2, "Pos2")]
    public string PosArg2;

    [ChoCommandLineArg("name", Aliases="n, m", IsRequired = true, Description = "Name of the person.")]
    public string Name;

    [ChoCommandLineArg("msg", DefaultValue = "Good Morning", Description = "Greeting message.")]
    public string Message
    {
        get;
        set;
    }
}

In the sample above illustrates that Name and Message are switch named command line arguments. The parser looks at the passed command line arguments for the switches 'name' and 'msg', load those arguments with the matching switch to the corresponding members in the command line object.

Listing 4.4 Test1.exe with positional arguments

>Test1.exe CPos1 /name:"Raj" /msg:"Hello world" CPos2

Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

-- ConsoleApplication2.HelloWorldCmdLineParams State --
        Message: Hello world
        PosArg1: CPos1
        PosArg2: CPos2
        Name: Raj

After executing the program with the above command line argument, the above object members Name and Message will be loaded with 'Raj' and 'Hello World' values.

5. Defining Command Line Argument Object

5.1 Attributes

5.1.1 ChoCommandLineArgObjectAttribute

This attribute is applied to the class which will recieve and loaded with command line arguments, ie. the class defining all the command line arguments available to the program. This object will reveive the values specified in the command line arguments to the executable.

  • ApplicationName - Optional. Application name. If not specified, it will be defaulted to AssemblyTitle. If AssemblyTitle is empty, it will defaulted to entry assembly Executable Name.
  • Copyright - Optional. Copyright information. If not specified, it will be defaulted to entry AssemblyCopyright.
  • Description - Optional. Description information. If not specified, it will be defaulted to entry AssemblyDescription.
  • Version - Optional. Version information. If not specified, it will be defaulted to entry AssemblyVersion.
  • AdditionalInfo - Optional. Additional detailed help text can be specified here.
  • DoNotShowUsageDetail - Optional. This flag instructs framework to show or not to show the details in the usage text. By default, it shows details in the usage text. Default value is false.
  • ShowUsageIfEmpty - Optional. This flag instructs the framework to show usage if no arguments passed to the executable at all. Default value is CommandLineParserSettings.ShowUsageIfEmpty value.

5.1.2 ChoCommandLineArgAdditionalUsageAttribute

This attribute is applied to the command line argument class in order to provide very detailed descriptive help text to the them. You may specify them multiple times to the class. These text are displayed at the end of Usage Text seperated by newline. 

  • AdditionalUsageText - Required. Detailed descriptive usage text. 

5.1.3 ChoCommandLineArgAttribute

This attribute can be applied to the fields and properties of class to designate as switch named command line arguments. This attribute designate the members of the class receive the command line argument value for the specified switch. 

5.1.3.1 Descriptive Parameters

There are three descriptive parameters in this attribute.

  • CommandLineSwitch - Required. Command line switch.
  • ShortName - Optional. Short name of the switch, used to produce help text.
  • Description - Optional. Description of the switch, used to produce help text.
  • Aliases - Optional. Specify alternative switches seperated by ',' or ';'
  • Order - Optional. Specify the order of the parameters to be validated as well as to be displayed in the usage text.
5.1.3.2 Make argument as required

IsRequired will enforce the command line argument parameter as mandatory parameter. If the value is not specified at the command line argument to the executable for the required option, will throw ChoCommandLineArgException.

5.1.3.3 Provide default value

Providing default value for a command line argument will allow the parser assign this default value to the member when the parameter is not specified at the command line to the executable. Note that setting this parameter does not prevent the user from specifying a different value on the command line, it merely provides the default value should the user opt not to provide one. 

5.1.3.4 Providing fallback value

In case the command line argument is passed for the concerned option and failed convert and assign to the member, fallback value is assigned to the member. 

5.1.3.5 Specifying Aliases for a command line argument

The Aliases property names the aliases for a command line argument. These are other names with which the command line argument may be referred to. It is a string property, and multiple names can be specified seperated by comma or semi-colon. Any alias specified here must be unique among other aliases and command line switches. Parser will not check for name uniqueness of these options. 

5.1.3.6 Formatting parameters

These parameters are optional to be used to formatting and make it to display elegant usage text. Below are the available parameters for use to format the usage text

  • NoOfTabsSwitchDescFormatSeperator - Optional. Number of TAB characters to be placed between switch and description. Default is 1.
  • DescriptionFormatLineSize - Optional. Command line switch description line size. Default is 60.
  • DescriptionFormatLineBreakChar - Optional. Command line switch description line break character. Default is a space (' ') character.

5.1.4 ChoPositionalCommandLineArgAttribute

This attribute can be applied to the fields and properties of class to designate as positional command line arguments. This attribute designate the members of the class receive the command line argument value for the specified position. Position always starts with 1.  

5.1.4.1 Descriptive Parameters

There are three descriptive parameters in this attribute.

  • Position - Required. Command line argument position. Must be value of 1 and above.
  • ShortName - Required. Short name of the switch, used to produce help text.

Besides above descriptive properties, you can specify fallback value, default value, formatting parameters to this argument. Alias can't specified to this argument.

6. Supported Types

Command Line Argument parser supports all members defined with any CLR datatype. No restriction at all. All the built-in datatypes are automatically managed by parser, no special handling of conversion is required. Below are the supported built-in types are

  • enum
  • bool
  • byte
  • sbyte
  • char
  • decimal
  • double
  • float
  • int
  • uint
  • long
  • ulong
  • short
  • ushort
  • string

If there is an exception occurs during the parser conversion and assignment of the the command line value to a member, it will throw ChoCommandLineArgException.

6.1 Special handling of bool value

This section illustrates about special way of passing and handling bool values from command line arguments. Most of the command line values can be passed to the executable in the below switch:value format

Listing 6.1 Test1.exe with switch and values

>Test1.exe /name:"Raj" /msg:"Hello world"

But bool parameters can be handled in couple of ways

6.1.1 Explicit way

In here the bool values are passed as string value (True/False). This sample illustrates that the switch -r value is passed with True text. Parser takes that value, converts and assign it to the corresponding data member.

Listing 6.2 Test1.exe with explicit bool TRUE value

>Test1.exe /name:"Raj" /msg:"Hello world" /r:True

Listing 6.3 Test1.exe with explicit bool FALSE value

>Test1.exe /name:"Raj" /msg:"Hello world" /r:False

6.1.2 Implicit way

This section illustrates how bool values are set without passing values explicitly. Parser looks for switch presents in the command line arguments for bool parameters. If the bool switch specified (/r), parser takes them as True value. If the bool switch specified as (/r-), parser takes them as False value.

Listing 6.4 Test1.exe with implicit bool TRUE value

>Test1.exe /name:"Raj" /msg:"Hello world" /r

Listing 6.5 Test1.exe with implicit bool FALSE value

>Test1.exe /name:"Raj" /msg:"Hello world" /r-

6.2 Handling of complex datatype members

This section illustrates how to handle complex UDT data type members. There are number of ways to recieve, parse and assign the values to command line argument members.

  • Callback Machanism
  • ValueConverter Mechanism

6.2.1 Callback Machanism

This is easiest way to receive, parse and load the command line argument value to a member. This gives you ultimate control over handling any type of command line argument values in your object. Override OnBeforeCommandLineArgLoaded method in the command line argument object, which will be invoked for each command argument switch by the parser. There you write custom logic for the concerned member and load the member with value. Once you handle the situation, simply return True to tell the parser that you handled the conversion.

Listing 6.4 CommandLineArgObject with Callback override

[ChoCommandLineArgObject(ApplicationName = "Hello world", Copyright = "Copyright 2014 Cinchoo Inc.")]
public class HelloWorldCmdLineParams : ChoCommandLineArgObject
{
    [ChoCommandLineArg("name", Aliases="n", IsRequired = true, Description = "Name of the person.")]
    public string Name;

    [ChoCommandLineArg("msg", DefaultValue = "Good Morning", Description = "Greeting message")]
    public string Message
    {
        get;
        set;
    }

    [ChoCommandLineArg("s", ShortName = "<int | INFINITE>", Description = "Sleep period.")]
    public int Sleep
    {
        get;
        set;
    }

    protected override bool OnBeforeCommandLineArgLoaded(string memberName, ref string value, object defaultValue, object fallbackValue)
    {
        if (memberName == "Sleep")
        {
            if (value == null)
                Sleep = 0;
            else
            {
                if (String.Compare(value.ToString(), "INFINITE", true) == 0)
                    Sleep = -1;
                else
                {
                    int timeout = 0;
                    int.TryParse(value.ToString(), out timeout);
                    Sleep = timeout;
                }
            }
            return true;
        }
        else
            return base.OnBeforeCommandLineArgLoaded(memberName, ref value, defaultValue, fallbackValue);
    }
}

In the sample above, Sleep parameter is int type. But it gives option to take either int or INFINITE value as command line argument. In the callback machanism, we handle it by overriding OnBeforeCommandLineArgLoaded method.

Listing 6.5 Test1.exe with Sleep value passed

>Test1.exe /name:"Raj" /msg:"Hello world" /s:INFINITE

Hello world [Version 1.0.0.0]
Copyright 2014 Cinchoo Inc.

-- ConsoleApplication2.HelloWorldCmdLineParams State --
        Message: Hello world
        Sleep: -1
        Name: Raj

6.2.2 ValueConverter Machanism

This is alternative way to receive, parse and load the command line argument value to a member. This gives you opportunity to perform conversion using ValueConverters. Either you can use existing converters or create new one and use them. In here I'll show you to how create a new converter and use them in command line argument members. This approach lets you to reuse the conversion logic in a pluggable architecture. 

First, lets define a ChoTimeoutConverter. It converts input string value to Timeout (int) value. You can define the converter class derived from either System.Windows.Data.IValueConverter (PresentationFramework.dll) or Cinchoo.Core.IChoValueConverter (Cinchoo.Core.dll). Both interfaces have the same methods. You can use whatever interface suited for your needs.

Listing 6.6 ChoTimeoutConverter class

public class ChoTimeoutConverter : IChoValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return 0;

        int timeout = 0;
        if (String.Compare("INFINITE", value.ToString(), true) == 0)
            return -1;
        else if (int.TryParse(value.ToString(), out timeout))
            return timeout;
        else
            return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value != null ? value.ToString() : String.Empty;
    }
}

In above, defined ChoTimeoutConverter from IChoValueConverter interface. Implemented the methods. It is self-explanatory. 

Now I'll illustrate how to use the above converter in the command line argument member. Couple of ways you can use them in command line argument object. 

  • Defining Converter Attribute and use it
  • Using ChoTypeConverterAttribute directly
6.2.2.1 Defining Converter Attribute and use it

This approach will let you define converter attribute for each converter. In the sample below shows you to define ChoTimeoutConverterAttribute for ChoTimeoutConverter class. The converter attribute must be drived from ChoTypeConverterAttribute class and pass converter type to its converter. See below example

Listing 6.7 ChoTimeoutConverterAttribute class

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class ChoTimeoutConverterAttribute : ChoTypeConverterAttribute
{
    public ChoTimeoutConverterAttribute()
        : base(typeof(ChoTimeoutConverter1))
    {
    }
}

Once you have attribute defined as above, it can be used against command line argument member to perform conversion of the argument value as below

Listing 6.8 Using ChoTimeoutConverterAttribute in a member

[ChoCommandLineArgObject(ApplicationName = "Hello world", Copyright = "Copyright 2014 Cinchoo Inc.")]
public class HelloWorldCmdLineParams : ChoCommandLineArgObject
{
    [ChoCommandLineArg("name", Aliases="n", IsRequired = true, Description = "Name of the person.")]
    public string Name;

    [ChoCommandLineArg("msg", DefaultValue = "Good Morning", Description = "Greeting message")]
    public string Message
    {
        get;
        set;
    }

    [ChoCommandLineArg("t", ShortName = "<int | INFINITE>", Description = "Timeout period.")]
    [ChoTimeoutConverter]
    public int Timeout
    {
        get;
        set;
    }
}
6.2.2.2 Using ChoTypeConverterAttribute

This illustrates how to use the converter class (ChoTimeoutConverter) directly to the command line object member without defining converter attribute for each converter class. Cinchoo framework provides common attribute class for this purpose. It is ChoTypeConverterAttribute. Simply use this attribute and pass the ChoTimeoutConverter type to its constructor as below. 

Listing 6.9 Using ChoTypeConverterAttribute in a member

[ChoCommandLineArgObject(ApplicationName = "Hello world", Copyright = "Copyright 2014 Cinchoo Inc.")]
public class HelloWorldCmdLineParams : ChoCommandLineArgObject
{
    [ChoCommandLineArg("name", Aliases="n", IsRequired = true, Description = "Name of the person.")]
    public string Name;

    [ChoCommandLineArg("msg", DefaultValue = "Good Morning", Description = "Greeting message")]
    public string Message
    {
        get;
        set;
    }

    [ChoCommandLineArg("t", ShortName = "<int | INFINITE>", Description = "Timeout period.")]
    [ChoTypeConverter(typeof(ChoTimeoutConverter))]
    public int Timeout
    {
        get;
        set;
    }
}

So far you have learned how to parse, convert and load command line argument value to its members using various methodologies. 

6.3 Special handling of Enum

This section illustrates about special way of passing and handling enum values from command line arguments. Most of the command line values can be passed to the executable in the below switch:value format

Listing 6.10 Test1.exe with switch and values

>Test1.exe /name:"Raj" /msg:"Hello world"

Lets begin by defining enum type and command line argument object with enum member as below

public enum Action { ADD, DEL, UPDATE };

[ChoCommandLineArgObject]
public class COMPUTERCmdLineArgObject : ChoCommandLineArgObject
{
 [ChoPositionalCommandLineArg(1, "<a>\\\\computername</a>", IsRequired = true, Order = 0)]
 public string ComputerName;

 [ChoCommandLineArg("action", IsRequired = true)]
 public Action ActionValue;
}

Above code illustrates that Action enum is defined. It is consumed by COMPUTERCmdLineArgObject class by declaring ActionValue member. It must be declared with ChoCommandLineArgAttribute. All enum types must be decorated with ChoCommandLineArgAttribute. 

Below is the test run shows how to pass enum value as command line argument

Listing 6.11 Test1.exe with enum value (Implicit way)

>Test1.exe "\\NYCDR1234234" /UPDATE

It illustrates that the enum values may be passed in /[EnumValue] format.

Listing 6.12 Test1.exe with enum value (Explicit way)

>Test1.exe "\\NYCDR1234234" /action:UPDATE

It illustrates that the enum values may be passed in /Switch:EnumValue format.

7. Customizations

There are ways to customize the parser, like command line switch characters, assignment seperators, usage switches, case sensitive handling of switches etc. The default settings should be good enough for most applications, but they are there for your customizations if you need to.

7.1 Specifying switch characters

This is special character used to prefix with swtiches when you pass command line arguments to the executable. Parser comes with default switch characters '/', '-'. It means that these are the available characters can be prefixed with switches. It can be customizable by opening [appExe].config or App.config file, add or edit custom configuration section 'commandLineParserSettings' to it.

Listing 7.1 CommandLineParserSettings config section

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="commandLineParserSettings" type="Cinchoo.Core.Shell.ChoCommandLineParserSettings, Cinchoo.Core" />
    </configSections>
    <commandLineParserSettings switchChars="/, -" valueSeperators = ":, =" />
</configuration>

Cinchoo.Core.Shell.ChoCommandLineParserSettings is the configuration section object. Below are the available parameters for you to customize the parser

  • switchChars - Optional. List of possible command line switch charactors can be specified here. Multiple characters acceptable. Must specify one character. Characters are separated by comma. Default values: "/, -".
  • valueSeparators - Optional. Specify list of possible switch-value separator characters. Multiple characters can be specified. Must specify atleast one character. Characters are separated by comma. Default values: ":. =".
  • usageSwitches - Optional. This is the switch used to display usage text. Specify list of possible usage switch strings. Multiple values can be specified. Must specify atleast one value. Strings are separated by comma. Default values: "?, h, help".
  • fileArgSwitches - Optional. This switch used to specify the file path containing command line arguments. Some programs may have long list of arguments. In order to avoid repeat passing the command line argument values to the executable, you can save them to a file and input the file using this switch. Multiple values can be specified. Must have atleast one value. Characters are seperated by comma. Default values: '@'.
  • ignoreCase - Optional. True, command line switches are case insensitive. Otherwise false. Default value: True.
  • showUsageIfEmpty - Optional. True, will show detailed usage text. Otherwise false. Default value: True.
  • doNotShowHeader - Optional. True, will show header information like AssemblyTitle, Version, and Description. Otherwise false. Default value: True.

7.2 Case sensitive of Command Line Switches

You have a option to specify whether the command line switches are case sensitive or not, through ignoreCase parameter in ChoCommandLineParserSettings. Default is that switches are NOT case sensitive.

8. CommandLineObject Overrides

This section illustrates some of the important methods which can be overrideable in order to customize the parser. 

8.1 GetUsage() Method

In general, the parser does the quite job of creating the usage text for you in most cases. In rare situation, you may want to customize the way you want the usage text would be. In this case, you can override GetUsage() method. It is a public method, return string value.

8.2 OnBeforeCommandLineArgLoaded Method

This method gets called before each command line argument value is loaded. In this method you have opportunity to validate the command line value, perform conversion or even replace with new value etc. Return true, instruct parser to stop processing of the value. Otherwise continue the process of loading the value.

8.3 OnAfterCommandLineArgLoaded Method

This method gets called for each command line argument is loaded. In here you have option to perform any post assignment tasks like dependent property values can be derived from this property.

8.4 OnCommandLineArgLoadError Method

This method gets called when there is an exception occurs when loading command line argument to a member. In here, you can perform exception handling. Return true, instruct the parser that you handled the exception. Otherwise false.

8.5 OnBeforeCommandLineArgObjectLoaded Method

This method gets called before the command line argument object is loaded. In this method you have opportunity to lookup the payload of the command line arguments overall, perform some pre validation or even replace the values with new ones etc. Return true, instruct parser to stop the parsing. Otherwise continue the process of loading the object.

8.6 OnAfterCommandLineArgLoaded Method

This method gets called after command line argument object is loaded. In here you have option to perform any post assignment tasks like dependent property values can be derived, validation etc.

8.7 OnCommandLineArgObjectLoadError Method

This method gets called when there is an exception occurs when parsing and loading whole command line argument object. In here, you can perform exception handling. Return true, instruct the parser that you handled the exception. Otherwise false.

8.8 OnCommandLineArgNotFound Method

This method gets called when no command line argument found in the argument list to the executable. 

9. Advanced Topics

In this topic, I'm going to talk about some of the key advanced topics to understand and use in your projects when you use this parser. It helps you how effective the parser can be utilized in your projects.

9.1 Specifying Argument with Whitespaces

Most of the cases, the command line argument value without any whitespaces can be specified as it is. In case where the value contains white spaces, it must be surrounded by double quotes. For example,

Listing 9.1 Test1.exe with value contains white spaces

>Test1.exe /name:"Raj Nagalngam" /msg:"Hello world" /r:True

9.2 Specifying Quotation Marks into the value

By default double quotes (") are the only recognized quotation marks. Inside the quotes you may escape quote character itself to insert a literal quotation mark into the value. It can be inserted as below (escaped using additional double quotes)

Listing 9.2 Test1.exe with value contains double quotes

>Test1.exe /name:"Raj""s msg" /msg:"Hello world" /r:True

9.3 Loading arguments from external file

A program may have multiple large, complex sets of command line arguments and wanted to run them multiple times, in this case you can save the values to external file and pass the file to the program. It saves time and effort of retyping the command line argument values for each run.

Listing 9.2 Test1.exe with value contains double quotes

>Test1.exe @"C:\Sample\CmdLineArgs.txt"

10. Validation

In this section, I'll talk about how to validate each command line argument member values, different approaches of validation methods, etc. Any application that accepts input from command line arguments must ensure that the information is valid in terms of some set of rules that you specify. For example, you may need to check that a customer's phone number has the correct number of digits, or that a date falls within a particular range. In addition, if the validation fails, you may need to send an error message that explains what is wrong. 

Cinchoo provides library of classes, called validators, that supplies the code for validating command line argument members. For example, one validator checks for null strings and another validator checks that a string contains only specific set of characters etc. In some cases, you may want to define your own custom validator and use them to validate the member values.

There are special validaters used to compose, aggregate other validators and group them together in a rule set.

  • ChoAndCompositeValidator - It groups other validators, all validators in this composite validator must be true for a successful validation.
  • ChoOrCompositeValidator - It groups other validators, at least one validators in the composite validator must be true for a successful validation.

If you want to avoid validations using this library feature and take control of validation, you can do so in couple of ways

  • Callback mechanism - OnAfterCommandLineArgLoaded override method can be used to put custom logic to validate the command line argument value.
  • Property Setter mechanism - This is another place where you can do custom validation on these member values.

10.1 Using Validators

The following procedures explains how to use validators in your command line argument object members. There are two ways to use validators for your command line argument members

  • ChoValidatorAttribute - An generic validator attribute, used in case specific validator does not have corresponding attribute. 
  • Validator specific attribute - These are customized validator attributes for each validator. For example, ChoStringValidatorAttribute is the attribute for ChoStringValidator class.  

Cinchoo supports the following validators out of the box

  • ChoValidators - Validators that Cinchoo library provides itself. You may find number of them under Cinchoo.Core namespace. For example, ChoNotNullValidator, ChoNotNullOrEmptyValidator etc.
  • BCL Configuration Validators - Validators that .NET BCL library provides. You can find them System.Configuration namespace. For example, StringValidator, IntegerValidator etc.
  • BCL DataAnnotation Validators - Another set of validators that .NET BCL library provides under System.ComponentModel.DataAnnotations namespace.

10.1.1 Using ChoValidatorAttribute

This is one way of specifying validation rules to command line argument members. In case where there is no validator attribute available for a specific validator, this attribute is here to rescue. This attribute takes validator type as input. The validator type can be one of below derived classes

  • Cinchoo.Core.ChoValidator - There are number of validators that Cinchoo framework library offers to use in your application.
  • System.Configuration.ConfigurationValidatorBase (System.Configuration.dll) - These set of validators .NET BCL offers to use.

Below are the available optional properties for this attribute

  • ConstructorArgs - Optional. Most of the validators accepts some mandorary parameters through constructor to perform the validation on command line object members. These parameters can be specified through this property. It takes object[] as value. These values are positional constructor arguments.
  • ConstructorArgsText - Optional string value. The constructor arguments can be specified as text and each value is seperated by ';'.
  • KeyValuePropertiesText - Optional string value. It is a key value pair seperated by ';' character to set validator property values. For example, "MinValue=1;MaxValue=10".

Listing 10.1 CommandLine Argument Object with validators

[ChoCommandLineArgObject(Description = "Utility to generate schema or class files from given source.")]
public sealed class ChoAppCmdLineParams : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "xmlfile", IsRequired = true, Description = "Name of an xml file to infer xsd schema from.")]
    [ChoValidator(typeof(ChoNotNullOrWhiteSpaceValidator))]
    [ChoValidator(typeof(StringValidator), ConstructorArgsText = "10")]
    public string XmlFilePath
    {
        get;
        set;
    }

    [ChoCommandLineArg("o", Description = "The output directory to create files in. The default is the current directory.")]
    [ChoStringValidator(MinLength = 10)]
    [ChoContainsCharactersValidator("A")]
    public string OutputDirectory
    {
        get;
        set;
    }
}

In the sample above, the member 'XmlFilePath' is specified with 2 validations. It must be not null and must contain minimum 10 characters. The sample shows how you can use ChoValidatorAttribute to define the validators those dont have corresponding custom validator attributes. Note, multiple validators specified at the top level are combined and validated as AND condition by default. Validation orders are NOT guaranteed sequencial. 

'OutputDirectory' member is decorated with custom validator attributes correponding to validators. In here, it is decorated with ChoStringValidatorAttribute and ChoContainsCharatersValidatorAttribute. It gives flexibilty, simplicity and type safety in declaring validators.

Create a console application and create an object of ChoAppCmdLineParams as below

Listing 10.2 Main method

class Program
{
    static void Main(string[] args)
    {
        ChoAppCmdLineParams cmdLineParams = new ChoAppCmdLineParams();
        Console.WriteLine(cmdLineParams.ToString());
    }
}

Listed below are test runs of the program with different arguments

Listing 10.3 Test1.exe with no arguments

>Test1.exe
ConsoleApplication2 [Version 1.0.0.0]
Copyright c  2014

Utility to generate schema or class files from given source.

Found exception while loading `xmlfile` command line argument.

The string must be at least 10 characters long.

ConsoleApplication2.exe xmlfile [/o:<string>]

        xmlfile Name of an xml file to infer xsd schema from.
        /o      The output directory to create files in. The default is the
                current directory.

Press any key to continue . . .

In the above run, the executable was run without passing any command line arguments. The application is trying to instantiate the ChoAppCmdLineParams object. While creating such object, the framework tries to validate the member values. In this case, the positional command line argument 'xmlFilePath' is failed to validate. The application termintes with exception.

Listing 10.4 Test1.exe with incorrect output directory value

>Test1.exe "C:\Windows\Systems\cpanal.xml" /o:"C:\Windows\Systems\Xcpanal.xml"
ConsoleApplication2 [Version 1.0.0.0]
Copyright c  2014

Utility to generate schema or class files from given source.

Found exception while loading `o` command line argument.

The value must contains ALL of the 'A' characters.

ConsoleApplication2.exe xmlfile [/o:<string>]

        xmlfile Name of an xml file to infer xsd schema from.
        /o      The output directory to create files in. The default is the
                current directory.

Press any key to continue . . .

In the above test run, the executable was run with arguments. The positional argument 'XmlFilePath' passed the validation (not null AND contains more than 10 characters). But it failed at validating 'OutputDirectory'. It does not contain character 'A' in it. The application terminates with exception.

Listing 10.5 Test1.exe with correct values

>Test1.exe "C:\Windows\Systems\cpanal.xml" /o:"C:\Windows\Systems\Acpanal.xml"
ConsoleApplication2 [Version 1.0.0.0]
Copyright c  2014

Utility to generate schema or class files from given source.

-- ConsoleApplication2.ChoAppCmdLineParams1 State --
        XmlFilePath: C:\Windows\Systems\cpanal.xml
        OutputDirectory: C:\Windows\Systems\Acpanal.xml

Press any key to continue . . .

In the above test run, the executable was run with valid arguments. The positional argument 'XmlFilePath' as well as 'OutputDirectory' passed the validation without any errors.

10.1.2 Using Custom Validator Attributes

Previous section I've talked about using generic validator attribute in defining validations on command line argument members. It paves the way to use the validators without validator attributes. Some validators comes with custom validator attributes. It is simple and straight forward to use and type safe. In the sample below, OutoutDirectory member defines 2 validators with its custom attributes. 

Listing 10.6 CommandLine Argument Object with validators

[ChoCommandLineArgObject(Description = "Utility to generate schema or class files from given source.")]
public sealed class ChoAppCmdLineParams : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "xmlfile", IsRequired = true, Description = "Name of an xml file to infer xsd schema from.")]
    [ChoValidator(typeof(ChoNotNullOrWhiteSpaceValidator))]
    [ChoValidator(typeof(StringValidator), ConstructorArgsText = "10")]
    public string XmlFilePath
    {
        get;
        set;
    }

    [ChoCommandLineArg("o", Description = "The output directory to create files in. The default is the current directory.")]
    [ChoStringValidator(MinLength = 10)]
    [ChoContainsCharactersValidator("A")]
    public string OutputDirectory
    {
        get;
        set;
    }
}

10.2 Callback Mechanism

In cases you want to perform some complex validation on members, you can do so with this mechanism. Simply override OnAfterCommandLineArgLoaded method in your command line class as below 

Listing 10.6 CommandLine argument object with custom validation

[ChoCommandLineArgObject(Description = "Utility to generate schema or class files from given source.")]
public sealed class ChoAppCmdLineParams1 : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "xmlfile", IsRequired = true, Description = "Name of an xml file to infer xsd schema from.")]
    [ChoValidator(typeof(ChoNotNullOrWhiteSpaceValidator))]
    [ChoValidator(typeof(StringValidator), ConstructorArgsText = "10")]
    public string XmlFilePath
    {
        get;
        set;
    }

    [ChoCommandLineArg("o", Description = "The output directory to create files in. The default is the current directory.")]
    [ChoStringValidator(MinLength = 10)]
    [ChoContainsCharactersValidator("A")]
    public string OutputDirectory
    {
        get;
        set;
    }

    protected override void OnAfterCommandLineArgLoaded(string memberName, object value)
    {
        if (memberName == "OutputDirectory")
        {
            if (!((string)value).StartsWith(@"C:\"))
                throw new ArgumentException("Invalid directory passed.");
        }
        else
            base.OnAfterCommandLineArgLoaded(memberName, value);
    }
}

In the above sample code shows how to validate members using callback mechanism. If the 'OutputDirectory' member value does not starts with 'C:\', throws an exception. 

Listing 10.7 Test1.exe with incorrect output directory value

>Test1.exe "C:\Windows\Systems\cpanal.xml" /o:"D:\Windows\Systems\Xcpanal.xml"
ConsoleApplication2 [Version 1.0.0.0]
Copyright c  2014

Utility to generate schema or class files from given source.

Found exception while loading `o` command line argument.

Invalid directory passed.

ConsoleApplication2.exe xmlfile [/o:<string>]

        xmlfile Name of an xml file to infer xsd schema from.
        /o      The output directory to create files in. The default is the
                current directory.

Press any key to continue . . .

If you run the sample with the above argument values, validation failes for 'OutputDirectory' member with exception 'Invalid directory passed.'.

10.3 Property Setter Mechanism

This is one another way to perform complex validation on members. It is exact similar approach as Callback Machanism. In here the validation can be carried out at the property setter for property command line members. See below sample

Listing 10.8 CommandLine argument object with custom validation

[ChoCommandLineArgObject(Description = "Utility to generate schema or class files from given source.")]
public sealed class ChoAppCmdLineParams1 : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "xmlfile", IsRequired = true, Description = "Name of an xml file to infer xsd schema from.")]
    [ChoValidator(typeof(ChoNotNullOrWhiteSpaceValidator))]
    [ChoValidator(typeof(StringValidator), ConstructorArgsText = "10")]
    public string XmlFilePath
    {
        get;
        set;
    }

    private string _outputDirectory;

    [ChoCommandLineArg("o", Description = "The output directory to create files in. The default is the current directory.")]
    [ChoStringValidator(MinLength = 10)]
    [ChoContainsCharactersValidator("A")]
    public string OutputDirectory
    {
        get { return _outputDirectory; }
        set
        {
            if (!value.StartsWith(@"C:\"))
                throw new ArgumentException("Invalid directory passed.");

            _outputDirectory = value;
        }
    }
}

In the above sample code shows how to validate members using property setter mechanism. The 'OutputDirectory' property validation is handled in property setter. If the value does not starts with 'C:\', throws an exception. 

Listing 10.9 Test1.exe with incorrect output directory value

>Test1.exe "C:\Windows\Systems\cpanal.xml" /o:"D:\Windows\Systems\Xcpanal.xml"
ConsoleApplication2 [Version 1.0.0.0]
Copyright c  2014

Utility to generate schema or class files from given source.

Found exception while loading `o` command line argument.

Invalid directory passed.

ConsoleApplication2.exe xmlfile [/o:<string>]

        xmlfile Name of an xml file to infer xsd schema from.
        /o      The output directory to create files in. The default is the
                current directory.

Press any key to continue . . .

If you run the sample with the above argument values, validation failes for 'OutputDirectory' member with exception 'Invalid directory passed.'.

11. Command Line Object Builder (Grouping)

So far we have learned about defining and consuming individual command line argument object in applications. It best suited for applications with single functionality with the arguments. Some applications performs multiple functions with their own arguments. For example, you have an application which processes data for various input data (network, file, internet etc.) which requires their own respective arguments. It means about having some sort of factory which you can define these contexts with. Basically the first argument would define the context to look up (network, file, internet etc.) then any following arguments would just act as that specific context's arguments.

'NET' dos command is the perfect example of this scenario. If you type 'net' produce this

c:\>net
The syntax of this command is:

NET
    [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
      HELPMSG | LOCALGROUP | PAUSE | SESSION | SHARE | START |
      STATISTICS | STOP | TIME | USE | USER | VIEW ]
In the above, ACCOUNTS, COMPUTER, CONFIG etc. are contexts to 'NET' command. Each takes their own set of arguments. If you want to see the arguments of ACCOUNTS context, type 'net ACCOUNTS /?' as below
c:\>net ACCOUNTS /?
The syntax of this command is:

NET ACCOUNTS
[/FORCELOGOFF:{minutes | NO}] [/MINPWLEN:length]
              [/MAXPWAGE:{days | UNLIMITED}] [/MINPWAGE:days]
              [/UNIQUEPW:number] [/DOMAIN]
Here I'm going to walk you through mimicing 'NET' command with START, COMPUTER, STOP contexts only using Cinchoo library. First define a Command Line Object Builder object as below

11.1 Defining Command Line Argument Builder

First define command line object builder as below

Listing 11.1 Command line argument builder object

[ChoCommandLineArgBuilder]
public class NetCmdBuilder : ChoCommandLineArgBuilder
{
    [ChoCommandLineArgBuilderCommand("START", typeof(STARTCmdLineArgObject), Order = 0)]
    [ChoCommandLineArgBuilderCommand("STOP", typeof(STOPCmdLineArgObject), Order = 1)]
    [ChoCommandLineArgBuilderCommand("COMPUTER", typeof(COMPUTERCmdLineArgObject), Order = -1)]
    public NetCmdBuilder()
    {
    }
}

The code above illustrates about defining command line object builder object. First thing define a builder class (ex. NetCmdBuilder) from ChoCommandLineArgBuilder class, it indicates that this object is a command line argument builder object. And it must be decorated with ChoCommandLineArgBuilderAttribute to complete the definition. Besides that, we need to start defining commands mapping using ChoCommandLineArgBuilderCommandAttribute in the default constructor of the builder. In the above example, the builder defined with 3 commands START, STOP and COMPUTER mapped with STARTCmdLineArgObject, STOPCmdLineArgObject and COMPUTERCmdLineArgObject respectively. The order in the attribute describes about the display order of the commands in the usage text.

Listing 11.2 COMPUTERCmdLineArgObject

public enum Action { ADD, DEL };

[ChoCommandLineArgObject(DoNotShowUsageDetail = true)]
public class COMPUTERCmdLineArgObject : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "\\\\computername", IsRequired = true, Order = 0)]
    public string ComputerName;

    [ChoCommandLineArg("action", IsRequired = true)]
    public Action Action;
}

The above command line argument contains two members, ComputerName - positional required argument and Action - an required enum argument. Note, DoNotShowUsageDetail is true tells the framework not to show the details of each member argument in the usage text.

Listing 11.3 STARTCmdLineArgObject

[ChoCommandLineArgObject]
public class STARTCmdLineArgObject : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "service")]
    public string ServiceName;
}

Listing 11.4 STOPCmdLineArgObject

[ChoCommandLineArgObject(DoNotShowUsageDetail = true)]
public class STOPCmdLineArgObject : ChoCommandLineArgObject
{
    [ChoPositionalCommandLineArg(1, "service", IsRequired = true)]
    public string ServiceName;
}

Listing 11.5 Main Method

class Program
{
    static void Main(string[] args)
    {
        NetCmdBuilder netCmdBuilder = new NetCmdBuilder();
        Console.WriteLine(netCmdBuilder.CommandLineArgObject.ToString());
    }
}

Lets start by creating new instance of netCmdBuilder object. Thats all. The framework does the major lifting of the work behind the scene. After successful load of the arguments, CommandLineArgObject member of the builder object is loaded with matched command object based on the context passed to the executable as argument.

Listed below are test runs of the programs with different set of arguments and its output.

Listing 11.6 net.exe with no arguments

c:\>net

The syntax of this command is:

NET [COMPUTER | START | STOP]

Press any key to continue . . .

Listing 11.7 net.exe with COMPUTER context, no computername passed

c:\>net COMPUTER

Missing '\\computername' argument.

NET COMPUTER \\computername {/ADD | /DEL}
Press any key to continue . . .<span id="cke_bm_151E" style="display: none;"> </span>

Listing 11.7 net.exe with COMPUTER context with valid arguments

c:\>net COMPUTER \\NYC123453 /DEL

-- CommadLineObjectBuilderSample.COMPUTERCmdLineArgObject State --
        ComputerName: \\NYC123453
        Action: DEL

Press any key to continue . . .

In the above sample successful run, netCmdBuilder.CommandLineArgObject is populated with instance of COMPUTERCmdLineArgObject object.

11.2 Attributes

11.2.1 ChoCommandLineArgObjectAttribute

This attribute is applied to the class which will construct the command line argument object for the passed context and load with them the command line arguments. It contains same set of properties as ChoCommandLineArgObjectAttribute. Please refer the section.

11.2.2 ChoCommandLineArgBuilderCommandAttribute

This attribute is applied to the constructor of the builder class to define the mapping of the commands with command line argument classes. Multiple attributes may be used to define the mapping.

Listing 11.8 Sample command line builder object

[ChoCommandLineArgBuilder]
public class NetCmdBuilder : ChoCommandLineArgBuilder
{
    [ChoCommandLineArgBuilderCommand("START", typeof(STARTCmdLineArgObject), Order = 0)]
    [ChoCommandLineArgBuilderCommand("STOP", typeof(STOPCmdLineArgObject), Order = 1)]
    [ChoCommandLineArgBuilderCommand("COMPUTER", typeof(COMPUTERCmdLineArgObject), Order = -1)]
    public NetCmdBuilder()
    {
    }
}

The above code illustrates that the constructor defined with three commands using ChoCommandLineArgBuildCommandAttribute.

  • Command - Required. Command name.
  • CommandType - Required. A command line argument object type.
  • Order - Optional order to display the commands in the usage text.

11.3 Overrrides

11.2.2 GetCommandLineArgObjectType()

This method can be overridden to take control of discovering the command type for the passed command. Usually all the mapping is done using ChoCommandLineArgBuildCommandAttribute in the builder class. Occationally you may want avoid doing so by overriding this method to handle it.

[ChoCommandLineArgBuilder]
public class ChoNETCommandLineArgBuilder : ChoCommandLineArgBuilder
{
    [ChoCommandLineArgBuilderCommand("ACCOUNTS", typeof(ChoAppCmdLineParams))]
    public ChoNETCommandLineArgBuilder()
    {
    }

    public override Type GetCommandLineArgObjectType(string command)
    {
        if (command == "COMPUTER")
            return typeof(COMPUTERCmdLineArgObject);
        else
            return base.GetCommandLineArgObjectType(command);
    }
}

The above code illustrates how to override GetCommandLineArgObjectType method.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Cinchoo

United States United States
No Biography provided

Comments and Discussions

 
SuggestionProviding source code or binaries Pin
Guirec22-Jul-14 17:53
professionalGuirec22-Jul-14 17:53 
GeneralRe: Providing source code or binaries Pin
Cinchoo3-Sep-14 8:16
memberCinchoo3-Sep-14 8:16 
GeneralMy vote of 5 Pin
Duncan Edwards Jones21-Jul-14 6:07
professionalDuncan Edwards Jones21-Jul-14 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 21 Aug 2014
Article Copyright 2014 by Cinchoo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid