4 // Author: Rafael Teixeira (rafaelteixeirabr@hotmail.com)
6 // (C) 2002 Rafael Teixeira
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 using System.Reflection;
36 namespace Mono.GetOptions
42 public class OptionList
45 private Options optionBundle = null;
46 private OptionsParsingMode parsingMode;
47 private bool breakSingleDashManyLettersIntoManyOptions;
48 private bool endOptionProcessingWithDoubleDash;
49 public ErrorReporter ReportError;
51 private string appExeName;
52 private string appVersion;
54 private string appTitle = "Add a [assembly: AssemblyTitle(\"Here goes the application name\")] to your assembly";
55 private string appCopyright = "Add a [assembly: AssemblyCopyright(\"(c)200n Here goes the copyright holder name\")] to your assembly";
56 private string appDescription = "Add a [assembly: AssemblyDescription(\"Here goes the short description\")] to your assembly";
57 private string appAboutDetails = "Add a [assembly: Mono.About(\"Here goes the short about details\")] to your assembly";
58 private string appUsageComplement = "Add a [assembly: Mono.UsageComplement(\"Here goes the usage clause complement\")] to your assembly";
59 private string appAdditionalInfo = null;
60 private string appReportBugsTo = null;
61 private string[] appAuthors;
63 private ArrayList list = new ArrayList();
64 private ArrayList arguments = new ArrayList();
65 private ArrayList argumentsTail = new ArrayList();
66 private MethodInfo argumentProcessor = null;
68 private bool HasSecondLevelHelp = false;
70 internal bool MaybeAnOption(string arg)
72 return ((parsingMode & OptionsParsingMode.Windows) > 0 && arg[0] == '/') ||
73 ((parsingMode & OptionsParsingMode.Linux) > 0 && arg[0] == '-');
80 return "Usage: " + appExeName + " [options] " + appUsageComplement;
84 public string AboutDetails
88 return appAboutDetails;
92 #region Assembly Attributes
96 private object[] GetAssemblyAttributes(Type type)
98 return entry.GetCustomAttributes(type, false);
101 private string[] GetAssemblyAttributeStrings(Type type)
103 object[] result = GetAssemblyAttributes(type);
105 if ((result == null) || (result.Length == 0))
106 return new string[0];
109 string[] var = new string[result.Length];
111 foreach(object o in result)
112 var[i++] = o.ToString();
117 private void GetAssemblyAttributeValue(Type type, string propertyName, ref string var)
119 object[] result = GetAssemblyAttributes(type);
121 if ((result != null) && (result.Length > 0))
122 var = (string)type.InvokeMember(propertyName, BindingFlags.Public | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance, null, result[0], new object [] {}); ;
125 private void GetAssemblyAttributeValue(Type type, ref string var)
127 object[] result = GetAssemblyAttributes(type);
129 if ((result != null) && (result.Length > 0))
130 var = result[0].ToString();
133 private void ExtractEntryAssemblyInfo(Type optionsType)
135 entry = optionsType.Assembly;
136 if (entry == this.GetType().Assembly) {
137 entry = Assembly.GetEntryAssembly();
140 appExeName = entry.GetName().Name;
141 appVersion = entry.GetName().Version.ToString();
142 GetAssemblyAttributeValue(typeof(AssemblyTitleAttribute), "Title", ref appTitle);
143 GetAssemblyAttributeValue(typeof(AssemblyCopyrightAttribute), "Copyright", ref appCopyright);
144 GetAssemblyAttributeValue(typeof(AssemblyDescriptionAttribute), "Description", ref appDescription);
145 GetAssemblyAttributeValue(typeof(Mono.AboutAttribute), ref appAboutDetails);
146 GetAssemblyAttributeValue(typeof(Mono.UsageComplementAttribute), ref appUsageComplement);
147 GetAssemblyAttributeValue(typeof(Mono.AdditionalInfoAttribute), ref appAdditionalInfo);
148 GetAssemblyAttributeValue(typeof(Mono.ReportBugsToAttribute), ref appReportBugsTo);
149 appAuthors = GetAssemblyAttributeStrings(typeof(AuthorAttribute));
150 if (appAuthors.Length == 0) {
151 appAuthors = new String[1];
152 appAuthors[0] = "Add one or more [assembly: Mono.Author(\"Here goes the author name\")] to your assembly";
160 private void AddArgumentProcessor(MemberInfo memberInfo)
162 if (argumentProcessor != null)
163 throw new NotSupportedException("More than one argument processor method found");
165 if ((memberInfo.MemberType == MemberTypes.Method && memberInfo is MethodInfo))
167 if (((MethodInfo)memberInfo).ReturnType.FullName != typeof(void).FullName)
168 throw new NotSupportedException("Argument processor method must return 'void'");
170 ParameterInfo[] parameters = ((MethodInfo)memberInfo).GetParameters();
171 if ((parameters == null) || (parameters.Length != 1) || (parameters[0].ParameterType.FullName != typeof(string).FullName))
172 throw new NotSupportedException("Argument processor method must have a string parameter");
174 argumentProcessor = (MethodInfo)memberInfo;
177 throw new NotSupportedException("Argument processor marked member isn't a method");
180 public OptionList(Options optionBundle)
182 if (optionBundle == null)
183 throw new ArgumentNullException("optionBundle");
185 Type optionsType = optionBundle.GetType();
186 this.optionBundle = optionBundle;
187 this.parsingMode = optionBundle.ParsingMode ;
188 this.breakSingleDashManyLettersIntoManyOptions = optionBundle.BreakSingleDashManyLettersIntoManyOptions;
189 this.endOptionProcessingWithDoubleDash = optionBundle.EndOptionProcessingWithDoubleDash;
190 this.ReportError = optionBundle.ReportError;
192 ExtractEntryAssemblyInfo(optionsType);
194 foreach(MemberInfo mi in optionsType.GetMembers()) {
195 object[] attribs = mi.GetCustomAttributes(typeof(KillOptionAttribute), true);
196 if (attribs == null || attribs.Length == 0) {
197 attribs = mi.GetCustomAttributes(typeof(OptionAttribute), true);
198 if (attribs != null && attribs.Length > 0) {
199 OptionDetails option = new OptionDetails(mi, (OptionAttribute)attribs[0], optionBundle);
201 HasSecondLevelHelp = HasSecondLevelHelp || option.SecondLevelHelp;
202 } else if (mi.DeclaringType == mi.ReflectedType) { // not inherited
203 attribs = mi.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true);
204 if (attribs != null && attribs.Length > 0)
205 AddArgumentProcessor(mi);
210 if (argumentProcessor == null) // try to find an inherited one
211 foreach(MemberInfo mi in optionsType.GetMembers())
212 if (mi.DeclaringType != mi.ReflectedType) { // inherited
213 object[] attribs = mi.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true);
214 if (attribs != null && attribs.Length > 0)
215 AddArgumentProcessor(mi);
221 #region Prebuilt Options
223 private bool bannerAlreadyShown = false;
225 internal string AdditionalBannerInfo;
227 public void ShowBanner()
229 if (!bannerAlreadyShown) {
230 Console.WriteLine(appTitle + " " + appVersion + " - " + appCopyright);
231 if (AdditionalBannerInfo != null)
232 Console.WriteLine(AdditionalBannerInfo);
234 bannerAlreadyShown = true;
237 private void ShowTitleLines()
240 Console.WriteLine(appDescription);
244 private void ShowAbout()
247 Console.WriteLine(appAboutDetails);
248 StringBuilder sb = new StringBuilder("Authors: ");
250 foreach(string s in appAuthors)
258 Console.WriteLine(sb.ToString());
261 private void ShowHelp(bool showSecondLevelHelp)
264 Console.WriteLine(Usage);
265 Console.WriteLine("Options:");
266 ArrayList lines = new ArrayList(list.Count);
268 foreach (OptionDetails option in list)
269 if (option.SecondLevelHelp == showSecondLevelHelp) {
270 string[] optionLines = option.ToString().Split('\n');
271 foreach(string line in optionLines) {
272 int pos = line.IndexOf('\t');
279 foreach (string line in lines) {
280 string[] parts = line.Split('\t');
281 Console.Write(parts[0].PadRight(tabSize));
282 Console.WriteLine(parts[1]);
283 if (parts.Length > 2) {
284 string spacer = new string(' ', tabSize);
285 for(int i = 2; i < parts.Length; i++) {
286 Console.Write(spacer);
287 Console.WriteLine(parts[i]);
291 if (appAdditionalInfo != null)
292 Console.WriteLine("\n{0}", appAdditionalInfo);
293 if (appReportBugsTo != null)
294 Console.WriteLine("\nPlease report bugs {0} <{1}>", (appReportBugsTo.IndexOf('@')>0)?"to":"at" , appReportBugsTo);
298 private void ShowUsage()
300 Console.WriteLine(Usage);
301 Console.Write("Short Options: ");
302 foreach (OptionDetails option in list)
303 Console.Write(option.ShortForm.Trim());
308 internal WhatToDoNext DoUsage()
311 return WhatToDoNext.AbandonProgram;
314 internal WhatToDoNext DoAbout()
317 return WhatToDoNext.AbandonProgram;
320 internal WhatToDoNext DoHelp()
323 return WhatToDoNext.AbandonProgram;
326 internal WhatToDoNext DoHelp2()
329 return WhatToDoNext.AbandonProgram;
334 #region Arguments Processing
336 public string[] ExpandResponseFiles(string[] args)
338 ArrayList result = new ArrayList();
340 foreach(string arg in args)
342 if (arg.StartsWith("@"))
346 using (StreamReader tr = new StreamReader(arg.Substring(1))) {
348 StringBuilder sb = new StringBuilder ();
350 while ((line = tr.ReadLine()) != null) {
353 for (int i = 0; i < t; i++) {
356 if (c == '"' || c == '\'') {
359 for (i++; i < t; i++) {
366 } else if (c == ' ') {
368 result.Add (sb.ToString());
376 result.Add (sb.ToString());
382 } catch (FileNotFoundException) {
383 ReportError(2011, "Unable to open response file '" + arg.Substring(1) + "'");
385 } catch (Exception exception) {
386 ReportError(2011, "Unable to open response file '" + arg.Substring(1) + "'. " + exception.Message);
394 return (string[])result.ToArray(typeof(string));
397 private static int IndexOfAny(string where, params char[] what)
399 return where.IndexOfAny(what);
402 public string[] NormalizeArgs(string[] args)
404 bool ParsingOptions = true;
405 ArrayList result = new ArrayList();
407 foreach(string arg in ExpandResponseFiles(args)) {
408 if (arg.Length > 0) {
409 if (ParsingOptions) {
410 if (endOptionProcessingWithDoubleDash && (arg == "--")) {
411 ParsingOptions = false;
415 if ((parsingMode & OptionsParsingMode.Linux) > 0 &&
416 arg[0] == '-' && arg.Length > 1 && arg[1] != '-' &&
417 breakSingleDashManyLettersIntoManyOptions) {
418 foreach(char c in arg.Substring(1)) // many single-letter options
419 result.Add("-" + c); // expand into individualized options
423 if (MaybeAnOption(arg)) {
424 int pos = IndexOfAny(arg, ':', '=');
429 result.Add(arg.Substring(0, pos));
430 result.Add(arg.Substring(pos+1));
435 argumentsTail.Add(arg);
439 // if nothing else matches then it get here
444 return (string[])result.ToArray(typeof(string));
447 public string[] ProcessArgs(string[] args)
451 bool OptionWasProcessed;
455 args = NormalizeArgs(args);
458 int argc = args.Length;
459 for (int i = 0; i < argc; i++) {
466 OptionWasProcessed = false;
468 if (arg.Length > 1 && (arg.StartsWith("-") || arg.StartsWith("/"))) {
469 foreach(OptionDetails option in list) {
470 OptionProcessingResult result = option.ProcessArgument(arg, nextArg);
471 if (result != OptionProcessingResult.NotThisOption) {
472 OptionWasProcessed = true;
473 if (result == OptionProcessingResult.OptionConsumedParameter)
480 if (!OptionWasProcessed)
481 ProcessNonOption(arg);
484 foreach(OptionDetails option in list)
485 option.TransferValues();
487 foreach(string argument in argumentsTail)
488 ProcessNonOption(argument);
490 return (string[])arguments.ToArray(typeof(string));
492 } catch (Exception ex) {
493 System.Console.WriteLine(ex.ToString());
494 System.Environment.Exit(1);
500 private void ProcessNonOption(string argument)
502 if (optionBundle.VerboseParsingOfOptions)
503 Console.WriteLine("argument [" + argument + "]");
504 if (argumentProcessor == null)
505 arguments.Add(argument);
507 argumentProcessor.Invoke(optionBundle, new object[] { argument });