Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

Automatic Command Line Parsing in C#

Rate me:
Please Sign up or sign in to vote.
4.78/5 (78 votes)
1 May 2003CPOL7 min read 378.3K   3.3K   139   82
A utility class allowing automatic command line switches and parsing.

Sample Image - CommandLineParser.png

Introduction

One of the first things I tend to write whenever I try something new in a programming language, are console test applications. These test applications tend to start turning into complete test harnesses - quickly start outgrowing the original functionality. Typically they also start handling all of the special cases that I uncover as I develop.

Eventually, I end up having to supply switches on the command line to change the functionality of the application. What I always wanted was a clever command-line parser to minimize the amount of coding I needed to do to add such functionality.

This article shows my current (and work in progress) command line parser based upon .net's attributes and using regular expressions to split the sections apart.

At the end of this article I've included a wish list (for myself) and will continue this class as an ongoing development. Updates and change-lists to be added to the original article.

Reading the Command Line

As most developers know, there are many ways to look at the command line supplied arguments for a running application. The two typical methods are:

  • The Main function takes an array of strings representing each of the white space separated arguments (or including white space if quoted correctly).

    C#
    public static int Main( string[] cmdLine )
    {
        Console.WriteLine("Command-line strings are:");
        foreach( string s in cmdLine )
            Console.WriteLine(s);
        return 0;
    }
  • The second method involves looking at the environment command-line.

    C#
    public static int Main( string[] cmdLine )
    {
        // Ignore the cmdLine passed in above....
        Console.WriteLine("Command-line is: {0}",
            System.Environment.CommandLine);
        return 0;
    }

Both of the above methods have pros and cons when trying to pass in command line arguments. The first is very easy to walk through when, for example, individual files-names have been supplied. The latter method allows complex switches, e.g. ones taking optional parameters, to be parsed.

In most cases, a programmer needs to walk through the command line switches looking for a textual match for their intended switch. This tends to make code look like a big series of if statements or a huge switch statement.

It was my intention to remove this clutter and constant rewriting of similar code from my test applications, indeed, very minimal coding is now required.

Registering Switches

The parser class has two different ways of being made aware of command line switches. Programmatically added switches or automatically added switches.

Programmatic Switches

This is the typical way of adding something to a class. After construction of the parser class (but prior to running the parser), switches may be added. Currently this is achieved by calling the member functions (notice the overloading in the example below) called AddSwitch.

C#
Parser parser = new Parser( System.Environment.CommandLine);

// Programmatically add some switches to the command line parser.
parser.AddSwitch( "Wibble", "Do something silly" );

// Add a switches with aliases for the first name, "help" and "a".
parser.AddSwitch(new string[] { "help", @"\?" }, "show help");
parser.AddSwitch(new string[] { "a", "b", "c", "d", "e", "f" }, "Early alphabet");

// Parse the command line.
parser.Parse();

In the example above, there are two examples of adding aliases to switches. For example, "help" may be addressed with either help or /?. The reason that the question mark is quoted belays the underlying use of Regular Expressions. It is necessary to quote the strings in such a way to prevent causing an exception in the regular expression handler.

Note: Currently, it is only possible to register a Boolean type switch. The AddSwitch function is likely to change in the next revision to take a type argument.

After the parser has been executed, it is possible to see if the switch has been set by calling the following:

C#
if ( parser["help"] != null )
  Console.WriteLine("Request for help = {0}", parser["help"]);
else
  Console.WriteLine("Request for help has no associated value.");

Automatic Switches

Using the wonderful attributes and reflection mechanisms available in C# and .net, it is possible to make the above code even simpler. Consider when you develop a class, typically you create properties for the different things your application does. Let's for example have a Boolean to specify whether or not the user wishes to see some help.

C#
public class Application
{
    private bool m_ShowHelp = false;
    public bool ShowHelp
    {
        get { return m_ShowHelp; }
        set { m_ShowHelp = value; }
    }

    // ... other code.
}

Rather than adding the programmatic code we would normally have done, consider the following adaptation:

C#
public class Application
{
    private bool m_ShowHelp = false;

    [CommandLineSwitch("help","Show the help")]
    public bool ShowHelp
    {
        get { return m_ShowHelp; }
        set { m_ShowHelp = value; }
    }

    // ... other code.
}

The attribute CommandLineSwitchAttribute (that may be shortened to CommandLineSwitch when used as above) assigned to the get/set methods of the property indicating that ShowHelp is a command line switch of "help". The only different thing we need to do is to pass the instance of the class into the constructor of the parser.

C#
public class Application
{
    private bool m_ShowHelp = false;

    [CommandLineSwitch("help","Show the help")]
    public bool ShowHelp
    {
        get { return m_ShowHelp; }
        set { m_ShowHelp = value; }
    }

    // ... other code.

    private void Run()
    {
        Console.WriteLine("Show help defaults to FALSE. " + 
                          "Currently it is : " + ShowHelp);

        // Add a reference to this class (instance needed) to the parser.
        Parser parser = new Parser( System.Environment.CommandLine, this );
        parser.Parse();

        Console.WriteLine("The current value of ShowHelp : " + ShowHelp);
    }

    public static int Main( string[] cmdLine );
    {
        Application a = new Application();
        a.Run();
        return 0;
    }
}

The parser class will now walk through the methods and properties of the Application class (as passed as second parameter in the Parser class constructor) looking for any CommandLineSwitchAttributes. In the event of finding a match when parsing the command-line, the internal value of ShowHelp will be modified directly, without any need to look at the results of the parser class.

C#
C:\> test
Show help defaults to FALSE.  Currently it is : false
The current value of ShowHelp : false

C:\> test /help
Show help defaults to FALSE.  Currently it is : false
The current value of ShowHelp : true

C:\> test --help
Show help defaults to FALSE.  Currently it is : false
The current value of ShowHelp : true

Switches

There are currently three types of switches handled by the parser (NOTE: the programmatically added switches only support Boolean types so far).

  • Boolean
  • String
  • Integer
  • Enumerations (** new **)

Using the Switches

All switches may be used with any of the following escapes, see below for examples of the "help" switch. The types may currently be intermingled within the same command-line.

  • /help Single forward slash
  • -help Single hyphen
  • -help Double hyphen

Boolean Switches

These switches are the most basic, supported by both the programmatic and automatic mechanisms. An occurrence of the switch toggles its state (this is important for the automatic switches, since they could default to true). Continuing using "help" as an example, the following switch directives may be given:

  • /help Toggle the state. When enrolled programmatically, this will set the state to true.
  • /help+ Set the Boolean to true (irrespective to its old state)
  • /help- Set the Boolean to false (irrespective to its old state)

Note that the single and double hyphen prefixes are still legal. So --help+ is identical to /help+.

String Switches

Currently string type switches are only implemented with the automatically enrolled switches.

To introduce a new example switch, say we have a user-name in our application defined as:

C#
private string m_UserName = "Unknown";

[CommandLineSwitch("user","Set the user name")]
public bool UserName
{
    get { return m_UserName; }
    set { m_UserName = value; }
}

We can supply a new user-name to the application in one of the following ways:

  • /user:new-user-name
  • /user new-user-name
  • /user:"user name with spaces"
  • /user "user name with spaces"

Note that the single and double hyphen prefixes are still legal. So --user:fred + is identical to -user:fred

Enumerated Type Switches (** new **)

Imagine if you had an enumerated type for the days of the week, you would implement something akin to the following.

C#
public enum DaysOfWeek 
{ 
   Sun, 
   Mon, 
   Tue, 
   Wed, 
   Thu, 
   Fri, 
   Sat 
};

Should it prove necessary for a command line switch to use this enumeration, this is now supported.  Placing the attribute type on an enumeration will allow the command line to accept enumerations (similar to how it accepts strings).  Conside the following:

C#
[CommandLineSwitch("Day","Day of the week")]

public DaysOfWeek DoW { ... }

Thus, the following will be legal on the command line:

  • /day:Mon
  • /day:tue   (note that it is not case sensitive)
  • /day sun

The following are illegal uses of the switch

  • /day tomorrow
  • /day:thur  (because it should be thu)

If a Set method is provided for the property, the value will be correctly set to the approprate value.

Accessing the remainder of the command-line

After all of the command-line switches, what about accessing any non-switch parameters? Well, nothing too complex. After all of the switches have been stripped from the command-line, what remains is split (by the parser) into white-space delimited (or quoted) strings, returned by the property Parameters.

C#
Console.WriteLine("Non-switch Params : {0}", parser.Parameters.Length);
for ( int j=0; j<parser.Parameters.Length; j++ )
    Console.WriteLine("{0} : {1}",j,parser.Parameters[j]);

Wish List

The list that follows is basically my wish list, should you think of anything and tell me, I might add your wishes to it too. :-)

  • Expand the article about the use of regular expressions within the parser.
  • Expand the article about the use of attributes.
  • Complete the commenting of the source.
  • Add more switch types.
  • Reimplement the AddSwitch function to take all switch types.
  • Allow a switch-file to be parsed (as well as the command line), e.g. if the file config contains switches for the run-time application to use (not new switches, but settings for existing ones), then putting @config on the command line will include those settings too.

Licence

I don't want to get too heavy with licences, so here it is simply. I retain copyright of the code, I offer no warrenties for its suitability for your job. If you include this implementation or a derivative in your code/article, credit me as the original author. If you include it in commerical software, put my name and a reference to codeproject in your about box/help information.

Oh yeah... send me an email or attach a comment below saying where you're using it.

History 

DateVersionChange Description
25 March 20030.1Initial Public Release
26 March 20030.2
  • Update to article.
  • Code updated to support using Enum types.
  • Utility function added to retrieve parsed switches, for error handling.

License

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


Written By
Product Manager
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey25-Apr-12 0:34
professionalManoj Kumar Choubey25-Apr-12 0:34 
GeneralHow to allow multiple aliases. Pin
BrianMaverickPost10-Apr-12 21:15
BrianMaverickPost10-Apr-12 21:15 
The code is easily modified to handle multiple aliases.

Replace the AttributeUsage above the class CommandLineAliasAttribute

From
[AttributeUsage( AttributeTargets.Property, AllowMultiple = true )]
To
[AttributeUsage( AttributeTargets.Property )]
QuestionArguments with double quotes Pin
BrianMaverickPost10-Apr-12 20:08
BrianMaverickPost10-Apr-12 20:08 
GeneralMy vote of 5 Pin
Yvan Rodrigues28-Feb-12 12:49
professionalYvan Rodrigues28-Feb-12 12:49 
GeneralMy vote of 5 Pin
JunfengGuo17-Mar-11 15:17
JunfengGuo17-Mar-11 15:17 
AnswerAnother parsing issue related to quoted strings Pin
marksn_ms29-Aug-10 11:41
marksn_ms29-Aug-10 11:41 
GeneralUsed your code in my app Pin
Dan Handevik23-Jun-09 20:04
Dan Handevik23-Jun-09 20:04 
GeneralLicense and commercial software with no about box Pin
holzbrenner23-May-09 3:29
holzbrenner23-May-09 3:29 
GeneralRe: License and commercial software with no about box Pin
Ray Hayes23-May-09 9:04
Ray Hayes23-May-09 9:04 
GeneralRe: License and commercial software with no about box Pin
holzbrenner25-May-09 1:22
holzbrenner25-May-09 1:22 
GeneralRe: License and commercial software with no about box Pin
Ray Hayes25-May-09 1:44
Ray Hayes25-May-09 1:44 
GeneralRe: License and commercial software with no about box Pin
holzbrenner25-May-09 5:46
holzbrenner25-May-09 5:46 
GeneralBug using 2 string parameters with quoted values Pin
Member 45339917-May-09 10:27
Member 45339917-May-09 10:27 
GeneralBug Pin
Member 26556754-Dec-08 1:40
Member 26556754-Dec-08 1:40 
GeneralRe: Bug Pin
psiwrx2-Feb-09 4:53
psiwrx2-Feb-09 4:53 
GeneralNConsoler Pin
EgorRubansky23-Aug-08 22:09
EgorRubansky23-Aug-08 22:09 
QuestionSmall problem Pin
Artheseus7-May-08 8:18
Artheseus7-May-08 8:18 
AnswerRe: Small problem Pin
Ray Hayes7-May-08 9:16
Ray Hayes7-May-08 9:16 
QuestionCode License Pin
mbt6-Mar-08 5:46
mbt6-Mar-08 5:46 
GeneralRe: Code License Pin
Ray Hayes6-Mar-08 6:00
Ray Hayes6-Mar-08 6:00 
GeneralRe: Code License Pin
mbt6-Mar-08 6:34
mbt6-Mar-08 6:34 
GeneralRe: Code License Pin
Ray Hayes6-Mar-08 7:14
Ray Hayes6-Mar-08 7:14 
GeneralRe: Code License Pin
mbt8-Mar-08 7:22
mbt8-Mar-08 7:22 
GeneralRe: Code License Pin
Ray Hayes8-Mar-08 12:01
Ray Hayes8-Mar-08 12:01 
GeneralRe: Code License Pin
Jonathan C Dickinson12-Jun-08 3:57
Jonathan C Dickinson12-Jun-08 3:57 

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

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