[Mono-list] Command Line Parsing

matt raffel matt.raffel at mindspring.com
Tue Nov 8 12:28:18 EST 2005


take a look at to http://www.mindspring.com/~matt.raffel/code/NET/ 

There are 3 classes that might help you
ApplicationCommandLine.cs
CommandlineArgument.cs
CommandlineException.cs

To use them do something like this:


First build an array of acceptable commandline arguements, such as ...


ApplicationCommandLine   _cmdProcessor = new ApplicationCommandLine();

// create the help argument
_cmdProcessor.AddArg(new CommandLineArgument("?", "prints help"));

// create the toggled argument of erase only or overwrite file options
SwitchableCommandLineArgument eraseOnly = new 
SwitchableCommandLineArgument("e", "only erases the file, content IS NOT 
overwritten first");
SwitchableCommandLineArgument overwriteFile = new 
SwitchableCommandLineArgument("w", "overwrites the file prior to erasing 
[default]");
eraseOnly.SwitchArg = overwriteFile;
overwriteFile.SwitchArg = eraseOnly;
overwriteFile.Selected = true;
_cmdProcessor.AddArg(eraseOnly);
_cmdProcessor.AddArg(overwriteFile);

// create an options
_cmdProcessor.AddArg(new CommandLineArgument("z", "overwrites with 
zeroes"));

// create an option that takes data
_cmdProcessor.AddArg(new DataCommandLineArgument("n", "number of times 
the file should be overwritten"));




then you need to process the command line by calling

// assuming args is the string[] from main
_cmdProcessor.ParseCmdLineToArgs(args);

then you need to loop through to find the ones that have been set.  I do 
something like this

foreach(CommandLineArgument argument in _cmdProcessor)
{
if (true == argument.Selected)
{
    char ch = argument.Argument.ToString().ToCharArray()[0];
    switch (ch)
    {
        case 'z':
            // do something
            break;
        case '?':
            PrintHelp();
            break;
        default:
            throw new Exception(String.Format("found an weird arugment 
{0}", ch));
    }
}



There maybe better ways but this works for me.  I use it in my command 
line tools all the time.  Hope it helps...

Matt


Abe Gillespie wrote:

>1.  Is there a utility class out there that helps parse command line
>arguments in a standard way?
>2.  I think I ran into a bug playing around with the command line. 
>When I send an asterisk "*" as one of the arguments I get some weird
>stuff.  On Windows it seems to work ok.  Try the following with an
>asterisk as one of the args:
>
>using System;
>class Program
>{
>    static void Main(string[] args)
>    {
>        foreach (string s in args)
>            Console.WriteLine(s);
>    }
>}
>
>Thanks.
>-Abe
>_______________________________________________
>Mono-list maillist  -  Mono-list at lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-list
>
>  
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20051108/f2776488/attachment.html


More information about the Mono-list mailing list