4 // Author: Rafael Teixeira (rafaelteixeirabr@hotmail.com)
6 // (C) 2002 Rafael Teixeira
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
31 namespace Mono.GetOptions
34 public delegate void ErrorReporter (int num, string msg);
36 [Obsolete ("This class is being obsoleted, there is no replacement, we suggest you use your own option parsing")]
39 public OptionsParsingMode ParsingMode;
40 public bool BreakSingleDashManyLettersIntoManyOptions;
41 public bool EndOptionProcessingWithDoubleDash;
42 public bool DontSplitOnCommas;
43 public ErrorReporter ReportError;
45 private OptionList optionParser;
47 public Options() : this(null) {}
49 public Options(string[] args) : this(args, OptionsParsingMode.Both, false, true, false, null) {}
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) {}
58 public Options(string[] args,
59 OptionsParsingMode parsingMode,
60 bool breakSingleDashManyLettersIntoManyOptions,
61 bool endOptionProcessingWithDoubleDash,
62 bool dontSplitOnCommas,
63 ErrorReporter reportError)
65 ParsingMode = parsingMode;
66 BreakSingleDashManyLettersIntoManyOptions = breakSingleDashManyLettersIntoManyOptions;
67 EndOptionProcessingWithDoubleDash = endOptionProcessingWithDoubleDash;
68 DontSplitOnCommas = dontSplitOnCommas;
69 if (reportError == null)
70 ReportError = new ErrorReporter(DefaultErrorReporter);
72 ReportError = reportError;
73 InitializeOtherDefaults();
78 protected virtual void InitializeOtherDefaults() { } // Only subclasses may need to implement something here
80 public bool RunningOnWindows {
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));
89 #region non-option arguments
91 private ArrayList arguments = new ArrayList();
92 public string[] RemainingArguments;
95 public virtual void DefaultArgumentProcessor(string argument)
97 arguments.Add(argument);
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; } }
106 public bool GotNoArguments { get { return arguments.Count == 0; } }
110 public void ProcessArgs(string[] args)
112 optionParser = new OptionList(this);
113 optionParser.AdditionalBannerInfo = AdditionalBannerInfo;
114 optionParser.ProcessArgs(args);
115 RemainingArguments = (string[])arguments.ToArray(typeof(string));
118 private static void DefaultErrorReporter (int number, string message)
121 Console.WriteLine("Error {0}: {1}", number, message);
123 Console.WriteLine("Error: {0}", message);
126 public virtual string AdditionalBannerInfo { get { return null; } }
128 public void ShowBanner()
130 optionParser.ShowBanner();
133 [Option("Show this help list", '?', "help")]
134 public virtual WhatToDoNext DoHelp()
136 return optionParser.DoHelp();
139 [Option("Show an additional help list", "help2")]
140 public virtual WhatToDoNext DoHelp2()
142 return optionParser.DoHelp2();
145 [Option("Display version and licensing information", 'V', "version")]
146 public virtual WhatToDoNext DoAbout()
148 return optionParser.DoAbout();
151 [Option("Show usage syntax and exit", "usage")]
152 public virtual WhatToDoNext DoUsage()
154 return optionParser.DoUsage();
157 private bool verboseParsingOfOptions = false;
159 [Option("Show verbose parsing of options", '.', "verbosegetoptions", SecondLevelHelp = true)]
160 public bool VerboseParsingOfOptions
162 set { verboseParsingOfOptions = value; }
163 get { return verboseParsingOfOptions; }
166 private bool debuggingOfOptions = false;
168 [Option("Show debugging info while processing options", '~', "debugoptions", SecondLevelHelp = true)]
169 public bool DebuggingOfOptions
172 debuggingOfOptions = 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);
180 get { return debuggingOfOptions; }