2008-01-18 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Mono.GetOptions / Mono.GetOptions / Options.cs
1 //
2 // Options.cs
3 //
4 // Author: Rafael Teixeira (rafaelteixeirabr@hotmail.com)
5 //
6 // (C) 2002 Rafael Teixeira
7 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections;
30
31 namespace Mono.GetOptions
32 {
33
34         public delegate void ErrorReporter (int num, string msg);
35
36         [Obsolete ("This class is being obsoleted, there is no replacement, we suggest you use your own option parsing")]
37         public class Options
38         {
39                 public OptionsParsingMode ParsingMode;
40                 public bool BreakSingleDashManyLettersIntoManyOptions;
41                 public bool EndOptionProcessingWithDoubleDash;
42                 public bool DontSplitOnCommas;
43                 public ErrorReporter ReportError;
44                 
45                 private OptionList optionParser;
46
47                 public Options() : this(null) {}
48                 
49                 public Options(string[] args) : this(args, OptionsParsingMode.Both, false, true, false, null) {}
50
51                 public Options(string[] args, 
52                                            OptionsParsingMode parsingMode, 
53                                            bool breakSingleDashManyLettersIntoManyOptions, 
54                                            bool endOptionProcessingWithDoubleDash,
55                                            bool dontSplitOnCommas) : 
56                         this(args, OptionsParsingMode.Both, false, true, false, null) {}
57                 
58                 public Options(string[] args, 
59                                            OptionsParsingMode parsingMode, 
60                                            bool breakSingleDashManyLettersIntoManyOptions, 
61                                            bool endOptionProcessingWithDoubleDash,
62                                            bool dontSplitOnCommas,
63                                            ErrorReporter reportError)
64                 {
65                         ParsingMode = parsingMode;
66                         BreakSingleDashManyLettersIntoManyOptions = breakSingleDashManyLettersIntoManyOptions;
67                         EndOptionProcessingWithDoubleDash = endOptionProcessingWithDoubleDash;
68                         DontSplitOnCommas = dontSplitOnCommas;
69                         if (reportError == null)
70                                 ReportError = new ErrorReporter(DefaultErrorReporter);
71                         else
72                                 ReportError = reportError;
73                         InitializeOtherDefaults();
74                         if (args != null)
75                                 ProcessArgs(args);
76                 }
77                 
78                 protected virtual void InitializeOtherDefaults() { } // Only subclasses may need to implement something here
79
80                 public bool RunningOnWindows {
81                         get {
82                                 // check for non-Unix platforms - see FAQ for more details
83                                 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
84                                 int platform = (int) Environment.OSVersion.Platform;
85                                 return ((platform != 4) && (platform != 128));
86                         }
87                 }
88
89                 #region non-option arguments
90                                 
91                 private ArrayList arguments = new ArrayList();
92                 public string[] RemainingArguments;
93
94                 [ArgumentProcessor]
95                 public virtual void DefaultArgumentProcessor(string argument)
96                 {
97                         arguments.Add(argument);
98                 }
99                 
100                 public string FirstArgument  { get { return (arguments.Count > 0)?(string)arguments[0]:null; } }
101                 public string SecondArgument { get { return (arguments.Count > 1)?(string)arguments[1]:null; } }
102                 public string ThirdArgument  { get { return (arguments.Count > 2)?(string)arguments[2]:null; } }
103                 public string FourthArgument { get { return (arguments.Count > 3)?(string)arguments[3]:null; } }
104                 public string FifthArgument  { get { return (arguments.Count > 4)?(string)arguments[4]:null; } }
105                 
106                 public bool GotNoArguments { get { return arguments.Count == 0; }  }
107                 
108                 #endregion
109                 
110                 public void ProcessArgs(string[] args)
111                 {
112                         optionParser = new OptionList(this);
113                         optionParser.AdditionalBannerInfo = AdditionalBannerInfo;
114                         optionParser.ProcessArgs(args);
115                         RemainingArguments = (string[])arguments.ToArray(typeof(string));
116                 }
117
118                 private static void DefaultErrorReporter (int number, string message)
119                 {
120                         if (number > 0)
121                                 Console.WriteLine("Error {0}: {1}", number, message);
122                         else
123                                 Console.WriteLine("Error: {0}", message);                               
124                 }
125                 
126                 public virtual string AdditionalBannerInfo { get { return null; } }
127                 
128                 public void ShowBanner()
129                 {
130                         optionParser.ShowBanner();
131                 }
132
133                 [Option("Show this help list", '?', "help")]
134                 public virtual WhatToDoNext DoHelp()
135                 {
136                         return optionParser.DoHelp();
137                 }
138
139                 [Option("Show an additional help list", "help2")]
140                 public virtual WhatToDoNext DoHelp2()
141                 {
142                         return optionParser.DoHelp2();
143                 }
144
145                 [Option("Display version and licensing information", 'V', "version")]
146                 public virtual WhatToDoNext DoAbout()
147                 {
148                         return optionParser.DoAbout();
149                 }
150
151                 [Option("Show usage syntax and exit", "usage")]
152                 public virtual WhatToDoNext DoUsage()
153                 {
154                         return optionParser.DoUsage();
155                 }
156
157                 private bool verboseParsingOfOptions = false;
158                 
159                 [Option("Show verbose parsing of options", '.', "verbosegetoptions", SecondLevelHelp = true)]
160                 public bool VerboseParsingOfOptions
161                 {
162                         set { verboseParsingOfOptions = value; }
163                         get { return verboseParsingOfOptions; }
164                 }
165
166                 private bool debuggingOfOptions = false;
167                 
168                 [Option("Show debugging info while processing options", '~', "debugoptions", SecondLevelHelp = true)]
169                 public bool DebuggingOfOptions
170                 {
171                         set { 
172                                 debuggingOfOptions = value; 
173                                 if (value) {
174                                         Console.WriteLine("ParsingMode = {0}", ParsingMode);
175                                         Console.WriteLine("BreakSingleDashManyLettersIntoManyOptions = {0}", BreakSingleDashManyLettersIntoManyOptions);
176                                         Console.WriteLine("EndOptionProcessingWithDoubleDash = {0}", EndOptionProcessingWithDoubleDash);
177                                         Console.WriteLine("DontSplitOnCommas = {0}", DontSplitOnCommas);
178                                 }
179                         }
180                         get { return debuggingOfOptions; }
181                 }
182
183
184         }
185         
186 }