public virtual void Print(TextWriter writer, SwitchDescriptor[] switches) { using (var tw = new CommandTextWriter(writer)) { this.Print(tw, switches); } }
public static object Parse(SwitchDescriptor descriptor, string arg) { if (descriptor.SwitchType.IsArray == true || typeof(System.Collections.IList).IsAssignableFrom(descriptor.SwitchType) == true) { return ParseArray(descriptor, arg); } else if (descriptor.SwitchType == typeof(bool)) { return ParseBoolean(descriptor, arg); } else { return ParseDefault(descriptor, arg); } }
private static object ParseDefault(SwitchDescriptor descriptor, string arg) { var converter = descriptor.Converter; if (converter.CanConvertFrom(typeof(string)) == false) throw new NotSupportedException(string.Format(Resources.CannotConvert_Format, arg)); try { return converter.ConvertFrom(arg); } catch (Exception e) { throw new ArgumentException(Resources.InvalidArgumentType, descriptor.Name, e); } }
private static object ParseBoolean(SwitchDescriptor descriptor, string arg) { if (descriptor.SwitchType == typeof(bool) && descriptor.ArgSeparator == null) { return true; } return ParseDefault(descriptor, arg); }
private static object ParseArray(SwitchDescriptor descriptor, string arg) { System.Collections.IList list; if (descriptor.SwitchType.IsArray == true) { list = new System.Collections.ArrayList() as System.Collections.IList; } else { list = TypeDescriptor.CreateInstance(null, descriptor.SwitchType, null, null) as System.Collections.IList; } var itemType = GetItemType(descriptor.SwitchType); if (itemType == null) throw new NotSupportedException(); var segments = arg.Split(new char[] { CommandSettings.ItemSperator, }); try { var converter = TypeDescriptor.GetConverter(itemType); foreach (var item in segments) { var s = item.Trim(); if (s.Length == 0) continue; var element = converter.ConvertFromString(s); list.Add(element); } if (descriptor.SwitchType.IsArray == true) { var array = Array.CreateInstance(itemType, list.Count); list.CopyTo(array, 0); list = array as System.Collections.IList; } else { } } catch (Exception e) { throw new ArgumentException(Resources.InvalidArgumentType, descriptor.Name, e); } return list; }
protected virtual bool IsSwitchVisible(SwitchDescriptor descriptor) { var attr = descriptor.Attributes.FirstOrDefault(item => item is BrowsableAttribute) as BrowsableAttribute; if (attr == null) return true; return attr.Browsable; }
private void ParseRequired(SwitchDescriptor switchDescriptor, ref string arguments) { var normalPattern = @"^((""[^""]*"")|(\S+))"; var match = Regex.Match(arguments, normalPattern); if (match.Success == true) { this.args.Add(switchDescriptor, match.Value); arguments = arguments.Substring(match.Length).Trim(); return; } throw new Exception(); }
private string GetString(SwitchDescriptor descriptor) { if (descriptor.Required == true) { var text = descriptor.Name; if (descriptor.DefaultValue == DBNull.Value) return string.Format("<{0}>", text); return string.Format("<{0}='{1}'>", text, descriptor.DefaultValue ?? "null"); } else { var patternItems = new string[] { descriptor.ShortNamePattern, descriptor.NamePattern, }; var patternText = string.Join(" | ", patternItems.Where(i => i != string.Empty)); return string.Format("[{0}]", patternText); } }
private void PrintSummary(CommandTextWriter writer, SwitchDescriptor[] switches) { if (this.Summary == string.Empty) return; writer.WriteLine(Resources.Summary); writer.Indent++; writer.WriteLine(this.Summary); writer.Indent--; writer.WriteLine(); }
private void PrintUsage(CommandTextWriter writer, SwitchDescriptor[] switches) { var query = from item in switches orderby item.Required descending select this.GetString(item); var maxWidth = writer.Width - (writer.TabString.Length * writer.Indent); var line = this.Name; writer.WriteLine(Resources.Usage); writer.Indent++; foreach (var item in query) { if (line != string.Empty) line += " "; if (line.Length + item.Length >= maxWidth) { writer.WriteLine(line); line = string.Empty.PadLeft(this.Name.Length + 1); } line += item; } writer.WriteLine(line); writer.Indent--; writer.WriteLine(); }
private void PrintRequirements(CommandTextWriter writer, SwitchDescriptor[] switches) { var items = switches.Where(i => i.Required == true).ToArray(); if (items.Any() == false) return; writer.WriteLine(Resources.Requirements); writer.Indent++; foreach (var item in items) { this.PrintRequirement(writer, item); } writer.Indent--; writer.WriteLine(); }
private void PrintRequirement(CommandTextWriter writer, SwitchDescriptor descriptor) { writer.WriteLine(descriptor.Name); if (descriptor.Description != string.Empty) { writer.Indent++; writer.WriteMultiline(descriptor.Description); writer.Indent--; } writer.WriteLine(); }
private void PrintOption(CommandTextWriter writer, SwitchDescriptor descriptor) { if (descriptor.ShortNamePattern != string.Empty) writer.WriteLine(descriptor.ShortNamePattern); if (descriptor.NamePattern != string.Empty) writer.WriteLine(descriptor.NamePattern); writer.Indent++; writer.WriteMultiline(descriptor.Description); writer.Indent--; writer.WriteLine(); }
private void PrintDescription(CommandTextWriter writer, SwitchDescriptor[] switches) { if (this.Description == string.Empty) return; writer.WriteLine(Resources.Description); writer.Indent++; writer.WriteMultiline(this.Description); writer.Indent--; writer.WriteLine(); }
private void Print(CommandTextWriter writer, SwitchDescriptor[] switches) { this.PrintSummary(writer, switches); this.PrintUsage(writer, switches); this.PrintDescription(writer, switches); this.PrintRequirements(writer, switches); this.PrintOptions(writer, switches); }