2005-05-06 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
authorRafael Teixeira <monoman@gmail.com>
Fri, 6 May 2005 15:38:13 +0000 (15:38 -0000)
committerRafael Teixeira <monoman@gmail.com>
Fri, 6 May 2005 15:38:13 +0000 (15:38 -0000)
* Mono.GetOptions/Options.cs
Added helper properties string FirstArgument, SecondArgument, ThirdArgument, FourthArgument,
FifthArgument and bool GotNoArguments
Added a helper property bool RunningOnWindows
Added new constructor Options(string[] args) and an overridable InitializeOtherDefaults method
to allow one-line construction and options processing
* Mono.GetOptions/OptionDetails.cs
Cleaning some logic to avoid throwing exceptions during option parsing
Corrected verbose message when setting a boolean option to show the real value being set

svn path=/trunk/mcs/; revision=44136

mcs/class/Mono.GetOptions/ChangeLog
mcs/class/Mono.GetOptions/Mono.GetOptions/OptionDetails.cs
mcs/class/Mono.GetOptions/Mono.GetOptions/OptionList.cs
mcs/class/Mono.GetOptions/Mono.GetOptions/Options.cs

index 3ba08ea616783dfa486b5ce866f1f1d30e825b93..d7bdc71490cd15482302113d6af835597936bbda 100644 (file)
@@ -1,3 +1,14 @@
+2005-05-06  Rafael Teixeira  <rafaelteixeirabr@hotmail.com>
+       * Mono.GetOptions/Options.cs
+               Added helper properties string FirstArgument, SecondArgument, ThirdArgument, FourthArgument,
+               FifthArgument and bool GotNoArguments
+               Added a helper property bool RunningOnWindows
+               Added new constructor Options(string[] args) and an overridable InitializeOtherDefaults method 
+               to allow one-line construction and options processing
+       * Mono.GetOptions/OptionDetails.cs 
+               Cleaning some logic to avoid throwing exceptions during option parsing
+               Corrected verbose message when setting a boolean option to show the real value being set
+
 2005-02-28  Rafael Teixeira  <rafaelteixeirabr@hotmail.com>
        * Mono.GetOptions/OptionAttribute.cs, Mono.GetOptions/OptionDetails.cs, Mono.GetOptions/OptionList: 
                Implemented support for vbc style booleans: /debug+ /debug- . Usage: [Option("debug", VBCStyleBoolean = true)]
index 051f5d98345bbfeff0cc78b15d1625600c3a9977..3ecb6e6a70f51733b9f26391b1d6ae2db7759ed3 100644 (file)
@@ -64,6 +64,7 @@ namespace Mono.GetOptions
                public string paramName = null;
                public bool VBCStyleBoolean;
                public bool SecondLevelHelp;
+               public bool Hidden;
 
                private string ExtractParamName(string shortDescription)
                {
@@ -84,15 +85,9 @@ namespace Mono.GetOptions
                        return shortDescription;
                }
 
-               public string ParamName 
-               {
-                       get 
-                       { 
-                               return paramName;
-                       }
-               }
+               public string ParamName { get { return paramName; } }
                                
-               public static bool Verbose = false;
+               private bool verboseParsing { get { return this.OptionBundle.VerboseParsingOfOptions; } }
 
                private OptionsParsingMode parsingMode { get { return this.OptionBundle.ParsingMode; } } 
 
@@ -192,6 +187,7 @@ namespace Mono.GetOptions
                        this.MaxOccurs = 1;
                        this.VBCStyleBoolean = option.VBCStyleBoolean;
                        this.SecondLevelHelp = option.SecondLevelHelp;
+                       this.Hidden = false; // TODO: check other attributes
                        
                        this.ParameterType = TypeOfMember(memberInfo);
 
@@ -270,22 +266,27 @@ namespace Mono.GetOptions
                        }
                }
 
-               private void Occurred(int howMany)
+               private int HowManyBeforeExceedingMaxOccurs(int howMany)
                {
+                       if (MaxOccurs > 0 && (Occurs + howMany) > MaxOccurs) {
+                               System.Console.Error.WriteLine("Option " + LongForm + " can be used at most " + MaxOccurs + " times. Ignoring extras...");
+                               howMany = MaxOccurs - Occurs;
+                       }
                        Occurs += howMany;
-
-                       if (MaxOccurs > 0 && Occurs > MaxOccurs)
-                               throw new IndexOutOfRangeException("Option " + ShortForm + " can be used at most " + MaxOccurs + " times");
+                       return howMany;
                }
+               
+               private bool AddingOneMoreExceedsMaxOccurs { get { return HowManyBeforeExceedingMaxOccurs(1) < 1; } }
 
                private void DoIt(bool setValue)
                {
                        if (!NeedsParameter)
                        {
-                               Occurred(1);
+                               if (AddingOneMoreExceedsMaxOccurs) 
+                                       return;
 
-                               if (Verbose)
-                                       Console.WriteLine("<" + this.LongForm + "> set to [true]");
+                               if (verboseParsing)
+                                       Console.WriteLine("<{0}> set to [{1}]", this.LongForm, setValue);
 
                                if (MemberInfo is FieldInfo)
                                {
@@ -311,14 +312,16 @@ namespace Mono.GetOptions
 
                        string[] parameterValues = parameterValue.Split(',');
 
-                       Occurred(parameterValues.Length);
+                       int waitingToBeProcessed = HowManyBeforeExceedingMaxOccurs(parameterValues.Length);
 
                        foreach (string parameter in parameterValues)
                        {
-
+                               if (waitingToBeProcessed-- <= 0)
+                                       break;
+                                       
                                object convertedParameter = null;
 
-                               if (Verbose)
+                               if (verboseParsing)
                                        Console.WriteLine("<" + this.LongForm + "> set to [" + parameter + "]");
 
                                if (Values != null && parameter != null) {
index 7e9489143b74e490ff097d21a58fdaa6f5de577a..bf5976e2e51352a2867a0743cf170125a107ff8e 100644 (file)
@@ -452,7 +452,7 @@ namespace Mono.GetOptions
                
                private void ProcessNonOption(string argument)
                {
-                       if (OptionDetails.Verbose)
+                       if (optionBundle.VerboseParsingOfOptions)
                                        Console.WriteLine("argument [" + argument + "]");                                                       
                        if (argumentProcessor == null)
                                arguments.Add(argument);
index c3b4ffa13f65169d9d0ef5bdc889f951d76438c1..f91a637d03e6debbe9ef26d110559ff1ee16d222 100644 (file)
@@ -1,4 +1,10 @@
-
+//
+// Options.cs
+//
+// Author: Rafael Teixeira (rafaelteixeirabr@hotmail.com)
+//
+// (C) 2002 Rafael Teixeira
+//
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 using System;
+using System.Collections;
 
 namespace Mono.GetOptions
 {
+       
        public class Options
        {
                public OptionsParsingMode ParsingMode;
@@ -31,8 +39,6 @@ namespace Mono.GetOptions
 
                private OptionList optionParser;
 
-               public string[] RemainingArguments;
-
                public Options()
                {
                        ParsingMode = OptionsParsingMode.Both;
@@ -40,10 +46,41 @@ namespace Mono.GetOptions
                        EndOptionProcessingWithDoubleDash = true;
                }
 
+               public Options(string[] args) : this()
+               {
+                       InitializeOtherDefaults();
+                       ProcessArgs(args);
+               }
+               
+               public bool RunningOnWindows { get { return 128 != (int)Environment.OSVersion.Platform; } }
+
+               protected virtual void InitializeOtherDefaults() { } // Only subclasses may need to implement something here
+
+               #region non-option arguments
+                               
+               private ArrayList arguments = new ArrayList();
+               public string[] RemainingArguments { get { return (string[])arguments.ToArray(typeof(string)); } }
+
+               [ArgumentProcessor]
+               protected virtual void DefaultArgumentProcessor(string argument)
+               {
+                       arguments.Add(argument);
+               }
+               
+               public string FirstArgument  { get { return (arguments.Count > 0)?(string)arguments[0]:null; } }
+               public string SecondArgument { get { return (arguments.Count > 1)?(string)arguments[1]:null; } }
+               public string ThirdArgument  { get { return (arguments.Count > 2)?(string)arguments[2]:null; } }
+               public string FourthArgument { get { return (arguments.Count > 3)?(string)arguments[3]:null; } }
+               public string FifthArgument  { get { return (arguments.Count > 4)?(string)arguments[4]:null; } }
+               
+               public bool GotNoArguments { get { return arguments.Count == 0; }  }
+               
+               #endregion
+               
                public void ProcessArgs(string[] args)
                {
                        optionParser = new OptionList(this);
-                       RemainingArguments =  optionParser.ProcessArgs(args);
+                       optionParser.ProcessArgs(args);
                }
 
                public void ShowBanner()
@@ -75,10 +112,13 @@ namespace Mono.GetOptions
                        return optionParser.DoUsage();
                }
 
-               [Option("Show verbose parsing of options", "verbosegetoptions", SecondLevelHelp = true)]
+               private bool verboseParsingOfOptions = false;
+               
+               [Option("Show verbose parsing of options", '.', "verbosegetoptions", SecondLevelHelp = true)]
                public bool VerboseParsingOfOptions
                {
-                       set { OptionDetails.Verbose = value;}
+                       set { verboseParsingOfOptions = value; }
+                       get { return verboseParsingOfOptions; }
                }