Yet Another Command Line Options Parser

As you may have noticed I’m a command line type of guy, even more now thanks to Powershell, which by the way is now shipped with Windows 7 along with an integrated scripting environment (Powershell ISE). Take that for awesomeness…

Back to the topic. Was another options parser really needed? Probably not, but combine to much free time with wanting to do some more advanced stuff with regular expressions and also with the frequent development of command line utilities and you get this outcome.

I think the main advantage to other options parsing libraries is the in-depth knowledge I have of how it works. No advantage to you, I know, sorry about that.

Nonetheless, if you want to have a look, you can find the source code and binaries here (Command Line Parser). It supports the following syntax:

-v -useTransaction false -dataFile data.dat -schemaFile "My Schema.scm"

and here’s how to use it in your own applications.

static void Main(string[] args)
{
    string processName = Process.GetCurrentProcess().ProcessName;

    CommandLineParser parser = new CommandLineParser(processName);

    OptionDefinition definition;

    definition = parser.AcceptFlag("verbose", "v");
    definition.Description = "Runs the application in verbose mode.";

    definition = parser.ExpectSingle("datafile");
    definition.Description = "Specifies the data file.";

    definition = parser.ExpectSingle("schemafile");
    definition.Description = "Specifies the schema file.";

    definition = parser.AcceptSingle("connectionstring");
    definition.Description = "Specifies the connection string.";

    definition = parser.AcceptSwitch("useTransaction");
    definition.Description = "Specifies if a transaction is used.";
    definition.DefaultValue = true;

    ParsedCommandLine cmdLine;
    try
    {
        cmdLine = parser.Parse(args);
    }
    catch (InvalidCommandLineException parseError)
    {
        Console.WriteLine(parseError.Message);
        Console.WriteLine();
        Console.WriteLine(parser.UsageInfo);

        return;
    }

    // Mandatory options
    string dataFile = (string)cmdLine.Options["DataFile"].Value;
    string schemaFile = (string)cmdLine.Options["SchemaFile"].Value;

    // Flag and Switch options always have a value
    bool verbose = (bool)cmdLine.Options["Verbose"].Value;
    bool test = (bool)cmdLine.Options["UseTransaction"].Value;

    // Check if the value for a non mandatory option is available
    string connString;
    if (cmdLine.Options["ConnectionString"].Available)
    {
        connString = (string)cmdLine.Options["ConnectionString"].Value;
    }
    else
    {
        var main = ConfigurationManager.ConnectionStrings["Main"];

        connString = main.ConnectionString;
    }
}
Advertisements

4 thoughts on “Yet Another Command Line Options Parser”

    1. Yes, for an option that has no arguments you can specify a Flag option using the method AcceptFlag().

      Flag options have the boolean false value if not specified in the command line, otherwise they have the boolean true value.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s