[Mono-list] Command Line Parsing

matt raffel matt.raffel at mindspring.com
Tue Nov 8 14:25:22 EST 2005


oh.....that is a good fix.  :=)  cool.

Matt

Abe Gillespie wrote:

>This works really well.  I had to make one small change though to get
>it to work with my non-single-character arguments.
>
>ApplicationCommandLine.cs, Ln 142 -
>Change to: string argValue = args[count];
>
>Thanks for the help!
>-Abe
>
>On 11/8/05, matt raffel <matt.raffel at mindspring.com> wrote:
>  
>
>> 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
>>
>>
>>
>>
>>    
>>
>
>  
>



More information about the Mono-list mailing list