This project is read-only.

The following code snippet demonstrates the usage of the code, in C#.

static void Main(string[] args)
    CommandLine cmdl = new CommandLine(true);
    cmdl.DefineOption("file", "Input file", true, false);
    cmdl.DefineOption("j", "Jpeg compression quality, [0-10]", false, true, "8");
    if (!cmdl.LoadArguments(args))

    int jpegCompr = Convert.ToInt32(cmdl.GetOptionValue("j")[0]);
    string inputFile = cmdl.GetOptionValue("file")[0];

    Console.WriteLine("Input file: " + inputFile);
    if (jpegCompr >= 0)
        Console.WriteLine("JPEG compression: " + jpegCompr);

First, the CommandLine object is instantiated. Passing ‘true’ into the constructor will make the default ‘/h’ flag to print the usage.


Then, the options are declared. The method takes the option name, description, is it mandatory, and if multiple instances are OK. If an options in

not mandatory, then a default value is supplied.


Then, the LoadArguments method is called, which takes the ‘args’ from the command line. If True is returned, everything is OK.


Then, the values for the arguments are queried in the code.


Similarly, flags can be defined. Flags are like options, but they don’t have any values. For example:

MyProgram.exe /o1=xyz /d

/o1 is an option, and /d is a flag.


In this example, multiple instances of an option are passed:

MyProgram.exe /file=x.txt /file=y.txt /file=z.txt


The C++ methods are slightly different, but functionally equivalent.



Last edited Dec 15, 2011 at 12:26 AM by pdx_goran, version 3


pdx_goran Mar 15, 2011 at 7:07 PM 
For C++ version, make sure _UNICODE and UNICODE precompile definitions are not set, or modify the code to use wide version of string.