JArgs– A Simple Command Line Parser DLL

What Is It?

I write simple C# console applications occasionally and have become frustrated at having to write code to handle command line arguments each time. So I decided instead to create a class library that can be reused. These notes detail this library.

How To Use It

Three ways:

1.Download the dll and copy it to a folder. From your code, right-click on the project name in Visual Studio’s Solution Explorer and choose ‘Add’ then ‘Reference’. Navigate to the dll, tick the box, then ‘OK’. Add ‘using JArgs’ to the ‘using’ section of your code. Use the parser as explained below.

2.Download the source files. Study the code and verify that it is not malicious. Use Solution Explorer to add them to a new ‘Class Library’ project in Visual Studio and compile a dll yourself. Use it as above.

3.Download the source files and drag them into your project in Solution Explorer. Edit the namespace code in each file. You can then call the parser directly from your code and the classes will be compiled directly into your .exe. No dll needed.

Classes Of Command Line Arguments

I decided that I would recognise three different kinds of item:

Switches: / Used for Boolean items. For example /R to recurse. -R and --R are also recognised.
Values: / Used for name values. For example /delay=10.
-/del=10 and --/d=10 are also recognised. Values may be mandatory or optional.
Strings: / Any argument not beginning with / - or -- is a string. Arguments are matched to names by the order in which they appear. May be mandatory or optional. Mandatory string must be added to the parser before any optional strings.

On the command line switch and value names may be abbreviated as long as no ambiguity arises. For example /d is acceptable in place if /delay

Two switches are added automatically: /? And /help They both bring up help text.

Reading Arguments From A File

By pointing the ArgFile property (see below) to a text file, the command line can be bypassed. If any command line argument is present, the argument file is ignored. If no command line arguments are entered, then the argument file is read.

ClassesIn the DLL

There is just one externally available class: Parser. You will need to instantiate a Parser in your code.

Public Properties

publicstring Version / Read only. / Reports the version of the Parser class that you are using.
publicstringErrorString / Read only. / Prints any error message currently in the parser.
publicstringClientVersion / Read / write / Use it tell the parser the parser the version of the client code that is using the parser.
publicstringHelpText / Read / write / Use it to set help text that will be displayed if the arguments are incorrect.
publicstringArgFile / Read / write / Use this to point the parser to an argument file instead of using the command line.

Public Methods

public Parser() / Constructor.
publicbool Parse() / Call this once the expected items have been added. Causes the command line to be read and arguments processed.
publicboolAddSwitch(
string Name, string Help) / Used to tell the parser to expect a switch.
publicboolAddValue(
string Name, string Help, bool Mandatory) / Used to tell the parser to expect a value item.
publicboolAddString(
string Name, string Help, bool Mandatory) / Used to tell the parser to expect a string item.
publicboolIsSwitchPresent(
string Name) / Returns true if the named switch is present on the command line.
publicboolIsValuePresent(
string Name) / Returns true if the named value item is present on the command line.
publicboolIsStringPresent(
string Name) / Returns true if the named string item is present.
publicstringGetValue(
string Name) / Returns the value entered on the command line if the value is present, or “” if the value is not present.
publicstringGetString(
string Name) / Returns the string entered on the command line if a string was entered, or “” if the string is not present. The value returned is ‘cleaned’ – i.e. raised to upper case and trimmed of any leading and trailing spaces.
publicstringGetRawString(
string Name) / This returns a string exactly as it was entered – without any ‘cleaning’. Useful for quoted strings where you need to retain the spaces.

Example Usage

usingJArgs;

namespace demo01

{

classProgram

{

staticvoid Main(string[] args)

{

ParserTheParser = newParser();

TheParser.HelpText = "Demo application to illustrate use of Parser class.";

TheParser.ClientVersion = "0.01";

TheParser.AddSwitch("Recurse",

"Should program recursively access subfolders?");

TheParser.AddValue("Retry",

"How many times should the program retry to read a file?",true);

TheParser.AddValue("Wait",

"How many should we wait if a folder cannot be accessed?",false);

TheParser.AddString("Root",

"Path to the directory tree to process.",true);

TheParser.AddString("Log", "Path to a log file.", false);

TheParser.AddString("ErrorLog", "Path to an error log.", false);

if (!TheParser.Parse())

{

Console.ReadKey();

return;

}

if (TheParser.IsSwitchPresent("Recurse"))

Console.WriteLine("Recurse selected");

// Retry must be present because TheParser returned true.

Console.WriteLine("Retry " + TheParser.GetValue("Retry") + " times");

if (TheParser.IsStringPresent("Log"))

Console.WriteLine("Log file path is: " + TheParser.GetString("Log"));

Console.ReadKey();

}

}

}