private void ShowUsage() { Console.WriteLine(this.Usage); Console.Write("Short Options: "); IEnumerator enumerator1 = this.list.GetEnumerator(); Label_0021: try { if (enumerator1.MoveNext()) { OptionDetails details1 = (OptionDetails)enumerator1.Current; Console.Write(details1.ShortForm.Trim()); goto Label_0021; } } finally { IDisposable disposable1 = enumerator1 as IDisposable; if (disposable1 != null) { disposable1.Dispose(); } } Console.WriteLine(); }
public static void LinkAlternatesInsideList(ArrayList list) { Hashtable baseForms = new Hashtable(list.Count); foreach (OptionDetails option in list) { if (option.LongForm != null && option.LongForm.Trim().Length > 0) { string[] parts = option.LongForm.Split(':'); if (parts.Length < 2) { baseForms.Add(option.LongForm, option); } else { OptionDetails baseForm = (OptionDetails)baseForms[parts[0]]; if (baseForm != null) { // simple linked list option.NextAlternate = baseForm.NextAlternate; baseForm.NextAlternate = option; } } } } }
public OptionList(Options optionBundle) { if (optionBundle == null) { throw new ArgumentNullException("optionBundle"); } Type optionsType = optionBundle.GetType(); this.optionBundle = optionBundle; this.parsingMode = optionBundle.ParsingMode; this.breakSingleDashManyLettersIntoManyOptions = optionBundle.BreakSingleDashManyLettersIntoManyOptions; this.endOptionProcessingWithDoubleDash = optionBundle.EndOptionProcessingWithDoubleDash; this.ReportError = optionBundle.ReportError; ExtractEntryAssemblyInfo(optionsType); foreach (MemberInfo mi in optionsType.GetMembers()) { object[] attribs = mi.GetCustomAttributes(typeof(KillOptionAttribute), true); if (attribs == null || attribs.Length == 0) { attribs = mi.GetCustomAttributes(typeof(OptionAttribute), true); if (attribs != null && attribs.Length > 0) { OptionDetails option = new OptionDetails(mi, (OptionAttribute)attribs[0], optionBundle); list.Add(option); HasSecondLevelHelp = HasSecondLevelHelp || option.SecondLevelHelp; } else if (mi.DeclaringType == mi.ReflectedType) // not inherited { attribs = mi.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true); if (attribs != null && attribs.Length > 0) { AddArgumentProcessor(mi); } } } } if (argumentProcessor == null) // try to find an inherited one { foreach (MemberInfo mi in optionsType.GetMembers()) { if (mi.DeclaringType != mi.ReflectedType) // inherited { object[] attribs = mi.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true); if (attribs != null && attribs.Length > 0) { AddArgumentProcessor(mi); } } } } }
private bool IsAlternate(string compoundArg) { OptionDetails next = NextAlternate; while (next != null) { if (next.IsThisOption(compoundArg)) { return(true); } next = next.NextAlternate; } return(false); }
private void Initialize(Options optionBundle) { if (optionBundle == null) { throw new ArgumentNullException("optionBundle"); } this.entry = Assembly.GetEntryAssembly(); this.appExeName = this.entry.GetName().Name; this.appVersion = this.entry.GetName().Version.ToString(); this.optionBundle = optionBundle; this.parsingMode = optionBundle.ParsingMode; this.breakSingleDashManyLettersIntoManyOptions = optionBundle.BreakSingleDashManyLettersIntoManyOptions; this.endOptionProcessingWithDoubleDash = optionBundle.EndOptionProcessingWithDoubleDash; this.GetAssemblyAttributeValue(typeof(AssemblyTitleAttribute), "Title", ref this.appTitle); this.GetAssemblyAttributeValue(typeof(AssemblyCopyrightAttribute), "Copyright", ref this.appCopyright); this.GetAssemblyAttributeValue(typeof(AssemblyDescriptionAttribute), "Description", ref this.appDescription); this.GetAssemblyAttributeValue(typeof(AboutAttribute), ref this.appAboutDetails); this.GetAssemblyAttributeValue(typeof(UsageComplementAttribute), ref this.appUsageComplement); this.appAuthors = this.GetAssemblyAttributeStrings(typeof(AuthorAttribute)); if (this.appAuthors.Length == 0) { this.appAuthors = new string[] { "Add one or more [assembly: Mono.GetOptions.Author(\"Here goes the author name\")] to your assembly" }; } MemberInfo[] infoArray1 = optionBundle.GetType().GetMembers(); for (int num1 = 0; num1 < infoArray1.Length; num1++) { MemberInfo info1 = infoArray1[num1]; object[] objArray1 = info1.GetCustomAttributes(typeof(OptionAttribute), true); if ((objArray1 != null) && (objArray1.Length > 0)) { OptionDetails details1 = new OptionDetails(info1, (OptionAttribute)objArray1[0], optionBundle); this.list.Add(details1); this.HasSecondLevelHelp = this.HasSecondLevelHelp || details1.SecondLevelHelp; } else { objArray1 = info1.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true); if ((objArray1 != null) && (objArray1.Length > 0)) { this.AddArgumentProcessor(info1); } } } }
public OptionList(Options optionBundle) { if (optionBundle == null) throw new ArgumentNullException("optionBundle"); Type optionsType = optionBundle.GetType(); this.optionBundle = optionBundle; parsingMode = optionBundle.ParsingMode; breakSingleDashManyLettersIntoManyOptions = optionBundle.BreakSingleDashManyLettersIntoManyOptions; endOptionProcessingWithDoubleDash = optionBundle.EndOptionProcessingWithDoubleDash; ReportError = optionBundle.ReportError; ExtractEntryAssemblyInfo(optionsType); foreach(MemberInfo mi in optionsType.GetMembers()) { object[] attribs = mi.GetCustomAttributes(typeof(KillOptionAttribute), true); if (attribs == null || attribs.Length == 0) { attribs = mi.GetCustomAttributes(typeof(OptionAttribute), true); if (attribs != null && attribs.Length > 0) { OptionDetails option = new OptionDetails(mi, (OptionAttribute) attribs[0], optionBundle); list.Add(option); HasSecondLevelHelp = HasSecondLevelHelp || option.SecondLevelHelp; } else if (mi.DeclaringType == mi.ReflectedType) { // not inherited attribs = mi.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true); if (attribs != null && attribs.Length > 0) AddArgumentProcessor(mi); } } } if (argumentProcessor == null) // try to find an inherited one foreach(MemberInfo mi in optionsType.GetMembers()) if (mi.DeclaringType != mi.ReflectedType) { // inherited object[] attribs = mi.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true); if (attribs != null && attribs.Length > 0) AddArgumentProcessor(mi); } }
private void ShowHelp(bool showSecondLevelHelp) { this.ShowTitleLines(); Console.WriteLine(this.Usage); Console.WriteLine("Options:"); ArrayList list1 = new ArrayList(this.list.Count); int num1 = 0; IEnumerator enumerator1 = this.list.GetEnumerator(); Label_003B: try { if (enumerator1.MoveNext()) { OptionDetails details1 = (OptionDetails)enumerator1.Current; if (details1.SecondLevelHelp == showSecondLevelHelp) { char[] chArray1 = new char[] { '\n' }; string[] textArray1 = details1.ToString().Split(chArray1); string[] textArray3 = textArray1; for (int num4 = 0; num4 < textArray3.Length; num4++) { string text1 = textArray3[num4]; int num2 = text1.IndexOf('\t'); if (num2 > num1) { num1 = num2; } list1.Add(text1); } } goto Label_003B; } } finally { IDisposable disposable1 = enumerator1 as IDisposable; if (disposable1 != null) { disposable1.Dispose(); } } num1 += 2; IEnumerator enumerator2 = list1.GetEnumerator(); Label_00E6: try { if (!enumerator2.MoveNext()) { return; } string text2 = (string)enumerator2.Current; char[] chArray2 = new char[] { '\t' }; string[] textArray2 = text2.Split(chArray2); Console.Write(textArray2[0].PadRight(num1)); Console.WriteLine(textArray2[1]); if (textArray2.Length > 2) { string text3 = new string(' ', num1); for (int num3 = 2; num3 < textArray2.Length; num3++) { Console.Write(text3); Console.WriteLine(textArray2[num3]); } } goto Label_00E6; } finally { IDisposable disposable2 = enumerator2 as IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } }
public string[] ProcessArgs(string[] args) { this.list.Sort(); args = this.NormalizeArgs(args); try { int num1 = args.Length; for (int num2 = 0; num2 < num1; num2++) { string text1; string text2 = args[num2]; if ((num2 + 1) < num1) { text1 = args[num2 + 1]; } else { text1 = null; } bool flag1 = false; if ((text2.Length <= 1) || (!text2.StartsWith("-", StringComparison.Ordinal) && !text2.StartsWith("/", StringComparison.Ordinal))) { goto Label_00DA; } IEnumerator enumerator1 = this.list.GetEnumerator(); Label_0078: try { if (enumerator1.MoveNext()) { OptionDetails details1 = (OptionDetails)enumerator1.Current; OptionProcessingResult result1 = details1.ProcessArgument(text2, text1); if (result1 == OptionProcessingResult.NotThisOption) { goto Label_0078; } flag1 = true; if (result1 == OptionProcessingResult.OptionConsumedParameter) { num2++; } } } finally { IDisposable disposable1 = enumerator1 as IDisposable; if (disposable1 != null) { disposable1.Dispose(); } } Label_00DA: if (!flag1) { this.ProcessNonOption(text2); } } IEnumerator enumerator2 = this.list.GetEnumerator(); Label_0102: try { if (enumerator2.MoveNext()) { OptionDetails details2 = (OptionDetails)enumerator2.Current; details2.TransferValues(); goto Label_0102; } } finally { IDisposable disposable2 = enumerator2 as IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } IEnumerator enumerator3 = this.argumentsTail.GetEnumerator(); Label_0151: try { if (enumerator3.MoveNext()) { string text3 = (string)enumerator3.Current; this.ProcessNonOption(text3); goto Label_0151; } } finally { IDisposable disposable3 = enumerator3 as IDisposable; if (disposable3 != null) { disposable3.Dispose(); } } return((string[])this.arguments.ToArray(typeof(string))); } catch (Exception exception1) { Console.WriteLine(exception1.ToString()); Environment.Exit(1); } return(null); }
public OptionDetails(System.Reflection.MemberInfo memberInfo, OptionAttribute option, Options optionBundle) { this.paramName = null; this.optionHelp = null; this.ShortForm = ("" + option.ShortForm).Trim(); if (option.LongForm == null) { this.LongForm = string.Empty; } else { this.LongForm = (option.LongForm != string.Empty) ? option.LongForm : memberInfo.Name; } this.AlternateForm = option.AlternateForm; this.ShortDescription = this.ExtractParamName(option.ShortDescription); this.Occurs = 0; this.OptionBundle = optionBundle; this.BooleanOption = false; this.MemberInfo = memberInfo; this.NeedsParameter = false; this.Values = null; this.MaxOccurs = 1; this.VBCStyleBoolean = option.VBCStyleBoolean; this.SecondLevelHelp = option.SecondLevelHelp; this.ParameterType = OptionDetails.TypeOfMember(memberInfo); if (this.ParameterType != null) { if (this.ParameterType.FullName != "System.Boolean") { if (this.LongForm.IndexOf(':') >= 0) { throw new InvalidOperationException("Options with an embedded colon (':') in their visible name must be boolean!!! [" + this.MemberInfo.ToString() + " isn't]"); } this.NeedsParameter = true; if (option.MaxOccurs == 1) { return; } if (this.ParameterType.IsArray) { this.Values = new ArrayList(); this.MaxOccurs = option.MaxOccurs; return; } if ((this.MemberInfo is MethodInfo) || (this.MemberInfo is PropertyInfo)) { this.MaxOccurs = option.MaxOccurs; return; } object[] objArray1 = new object[] { "MaxOccurs set to non default value (", option.MaxOccurs, ") for a [", this.MemberInfo.ToString(), "] option" }; throw new InvalidOperationException(string.Concat(objArray1)); } this.BooleanOption = true; if (option.MaxOccurs != 1) { if ((this.MemberInfo is MethodInfo) || (this.MemberInfo is PropertyInfo)) { this.MaxOccurs = option.MaxOccurs; } else { object[] objArray2 = new object[] { "MaxOccurs set to non default value (", option.MaxOccurs, ") for a [", this.MemberInfo.ToString(), "] option" }; throw new InvalidOperationException(string.Concat(objArray2)); } } } }
private void Initialize(Options optionBundle) { if (optionBundle == null) { throw new ArgumentNullException("optionBundle"); } this.entry = Assembly.GetEntryAssembly(); this.appExeName = this.entry.GetName().Name; this.appVersion = this.entry.GetName().Version.ToString(); this.optionBundle = optionBundle; this.parsingMode = optionBundle.ParsingMode; this.breakSingleDashManyLettersIntoManyOptions = optionBundle.BreakSingleDashManyLettersIntoManyOptions; this.endOptionProcessingWithDoubleDash = optionBundle.EndOptionProcessingWithDoubleDash; this.GetAssemblyAttributeValue(typeof(AssemblyTitleAttribute), "Title", ref this.appTitle); this.GetAssemblyAttributeValue(typeof(AssemblyCopyrightAttribute), "Copyright", ref this.appCopyright); this.GetAssemblyAttributeValue(typeof(AssemblyDescriptionAttribute), "Description", ref this.appDescription); this.GetAssemblyAttributeValue(typeof(AboutAttribute), ref this.appAboutDetails); this.GetAssemblyAttributeValue(typeof(UsageComplementAttribute), ref this.appUsageComplement); this.appAuthors = this.GetAssemblyAttributeStrings(typeof(AuthorAttribute)); if (this.appAuthors.Length == 0) { this.appAuthors = new string[] { "Add one or more [assembly: Mono.GetOptions.Author(\"Here goes the author name\")] to your assembly" } ; } MemberInfo[] infoArray1 = optionBundle.GetType().GetMembers(); for (int num1 = 0; num1 < infoArray1.Length; num1++) { MemberInfo info1 = infoArray1[num1]; object[] objArray1 = info1.GetCustomAttributes(typeof(OptionAttribute), true); if ((objArray1 != null) && (objArray1.Length > 0)) { OptionDetails details1 = new OptionDetails(info1, (OptionAttribute) objArray1[0], optionBundle); this.list.Add(details1); this.HasSecondLevelHelp = this.HasSecondLevelHelp || details1.SecondLevelHelp; } else { objArray1 = info1.GetCustomAttributes(typeof(ArgumentProcessorAttribute), true); if ((objArray1 != null) && (objArray1.Length > 0)) { this.AddArgumentProcessor(info1); } } } }
public string[] ProcessArgs(string[] args) { string arg; string nextArg; bool OptionWasProcessed; list.Sort(); OptionDetails.LinkAlternatesInsideList(list); args = NormalizeArgs(args); try { int argc = args.Length; for (int i = 0; i < argc; i++) { arg = args[i]; if (i + 1 < argc) { nextArg = args[i + 1]; } else { nextArg = null; } OptionWasProcessed = false; if (arg.Length > 1 && (arg.StartsWith("-") || arg.StartsWith("/"))) { foreach (OptionDetails option in list) { OptionProcessingResult result = option.ProcessArgument(arg, nextArg); if (result != OptionProcessingResult.NotThisOption) { OptionWasProcessed = true; if (result == OptionProcessingResult.OptionConsumedParameter) { i++; } break; } } } if (!OptionWasProcessed) { ProcessNonOption(arg); } } foreach (OptionDetails option in list) { option.TransferValues(); } foreach (string argument in argumentsTail) { ProcessNonOption(argument); } return((string[])arguments.ToArray(typeof(string))); } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); //System.Environment.Exit(1); throw; } }