/// <summary> /// Convert the usage to Text /// </summary> /// <remarks>if <paramref name="maxColumns"/> is -1, the width of the console will be used /// on windows machines, and the text will not be wrapped on non-windows machines</remarks> /// <param name="writer">TextWriter to write the output to</param> /// <param name="optStyle">The style to use when printing possible option names</param> /// <param name="includeDefaultValues">True to include the default option values in the output</param> /// <param name="maxColumns">Wrap text at the given column (attempts to wrap at '-' or ' ')</param> public void ToText(TextWriter writer, OptStyle optStyle, bool includeDefaultValues, int maxColumns) { XsltArgumentList argList = new XsltArgumentList(); XslTransform trans = new XslTransform(); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "CommandLine.OptParse.Xslt.Text.xslt")) { trans.Load(new XmlTextReader(stream), null, Assembly.GetExecutingAssembly().Evidence); } argList.AddParam("shortOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "-" : "/"); argList.AddParam("longOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "--" : "/"); argList.AddParam("includeDefaultValues", string.Empty, includeDefaultValues); argList.AddParam("newline", string.Empty, Environment.NewLine); argList.AddExtensionObject("extObj", new TextTransformHelper(maxColumns)); if (maxColumns <= 0) { maxColumns = -1; } argList.AddParam("maxColumns", string.Empty, maxColumns); trans.Transform(this.Usage, argList, writer, null); }
private Regex BuildRegEx(OptStyle optStyle, UnixShortOption unixShortOption) { if (optStyle == OptStyle.Unix) { return(new Regex(@"^ (?: --(?<longname>[^\s=]+) (?<longvalue>=.+)? | -(?<shortname>\S) (?<shortvalue>.+)? )$" , RegexOptions.IgnorePatternWhitespace)); } else { return(new Regex(@"^/(?<longname>[^\s:]+)(?<longvalue>:.+)?$")); } }
public NameValueCollection GetOptionDescriptions(OptStyle style) { NameValueCollection coll = new NameValueCollection(); OptionDefinition[] opts = _resultsHandler.GetOptions(); StringBuilder key = new StringBuilder(); foreach (OptionDefinition opt in opts) { key.Length = 0; if (opt.ShortNames != null && opt.ShortNames.Length > 0) { foreach (char optName in opt.ShortNames) { if (key.Length > 0) { key.Append((style == OptStyle.Unix) ? ", -" : ", /"); } else { key.Append((style == OptStyle.Unix) ? "-" : "/"); } key.Append(optName); } if (opt.LongNames != null && opt.LongNames.Length > 0) { key.Append(", "); } } if (opt.LongNames != null && opt.LongNames.Length > 0) { key.Append((style == OptStyle.Unix) ? "--" : "/"); key.Append(string.Join((style == OptStyle.Unix) ? ", --" : ", /", opt.LongNames)); } coll.Add(key.ToString(), opt.Description); } return(coll); }
public void PrintUsage(OptStyle optStyle, IProgUsageInfo usageInfo, TextWriter writer, int columns) { foreach (string header in usageInfo.Headers) { writer.WriteLine(header); if (usageInfo.IsOptionHeader(header)) { NameValueCollection coll = GetOptionDescriptions(optStyle); for (int i = 0; i < coll.Count; i++) { PrintWithIndent(4, coll.GetKey(i), columns, writer); PrintWithIndent(8, coll[i], columns, writer); } } else { PrintWithIndent(4, usageInfo.GetContents(header), columns, writer); } } }
/// <summary> /// Convert the usage to HTML /// </summary> /// <param name="writer">TextWriter to write the output to</param> /// <param name="optStyle">The style to use when printing possible option names</param> /// <param name="cssStyleSheet">Stylesheet URI to apply to the HTML content</param> /// <param name="includeDefaultValues">True to include the default option values in the output</param> /// <param name="title">Title to use for the HTML page</param> public void ToHtml(TextWriter writer, OptStyle optStyle, Uri cssStyleSheet, bool includeDefaultValues, string title) { XsltArgumentList argList = new XsltArgumentList(); XslTransform trans = new XslTransform(); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "CommandLine.OptParse.Xslt.Html.xslt")) { trans.Load(new XmlTextReader(stream), null, Assembly.GetExecutingAssembly().Evidence); } argList.AddParam("shortOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "-" : "/"); argList.AddParam("longOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "--" : "/"); argList.AddParam("includeDefaultValues", string.Empty, includeDefaultValues); if (cssStyleSheet != null) { argList.AddParam("cssStyleSheet", string.Empty, cssStyleSheet.AbsoluteUri); } argList.AddParam("title", string.Empty, title); trans.Transform(this.Usage, argList, writer, null); }
/// <summary> /// Parse the arguments for options using the given settings /// </summary> /// <param name="optStyle">What type of options to parse</param> /// <param name="unixShortOption">How to parse unix short options (ignored /// if not unix style parsing)</param> /// <param name="dupOptHandleType">How to handle un-expected duplicate option /// definitions</param> /// <param name="unknownOptHandleType">How to handle options that were not /// defined</param> /// <param name="caseSesitive">If the parsing of options /// should consider the case of the options</param> /// <param name="args">The command-line arguments to parse</param> /// <returns>Arguments from the command-line that were not options</returns> public string[] Parse(OptStyle optStyle, UnixShortOption unixShortOption, DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType, bool caseSesitive, string[] args) { ArrayList foundDefinitions = new ArrayList(); Group grp; Match m; Regex re; StringCollection arguments; bool isShort; bool unknown; string name; string value; string[] results; if (args == null || args.Length == 0) { if (this.SearchEnvironment) { ParseEnvironment(foundDefinitions, caseSesitive); } return(new string[0]); } arguments = new StringCollection(); re = BuildRegEx(optStyle, unixShortOption); for (int i = 0; i < args.Length; i++) { m = re.Match(args[i]); if (m.Success) { // see if there is a value grp = m.Groups["shortname"]; if (grp != null && grp.Value != string.Empty) { isShort = true; name = grp.Value; value = m.Groups["shortvalue"].Value; } else { isShort = false; name = m.Groups["longname"].Value; value = m.Groups["longvalue"].Value; // remove the '=' or ':' at the beginning of the value if (value != null && value.Length > 0) { value = value.Substring(1); } } if (optStyle == OptStyle.Unix) { if (isShort == false) { ParseLongOption(args, name, value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); } // see if the value is just concatinated short options else if (unixShortOption == UnixShortOption.CollapseShort) { // name is the first option ParseShortOption(args, name[0], null, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); // parse the rest of the options that lie in the 'value' variable if (value != null) { char[] unknownOptions; ParseShortOptions(args, value.ToLower().ToCharArray(), caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknownOptions); // don't do anything with unknown concatinated short options, // they should not be considered options or arguments } } else { // name is the first option ParseShortOption(args, name[0], value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); } } else { if (name.Length == 1) { ParseShortOption(args, name[0], value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); } else { ParseLongOption(args, name, value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); } } // consider unknown options to be arguments if (unknown) { arguments.Add(args[i]); } } else { arguments.Add(args[i]); } } // parse the environment if (this.SearchEnvironment) { ParseEnvironment(foundDefinitions, caseSesitive); } results = new string[arguments.Count]; arguments.CopyTo(results, 0); return(results); }
public void PrintUsage(OptStyle optStyle, IProgUsageInfo usageInfo, int columns) { PrintUsage(optStyle, usageInfo, Console.Out, columns); }
/// <summary> /// Convert the usage to Text /// </summary> /// <remarks>if <paramref name="maxColumns"/> is -1, the width of the console will be used /// on windows machines, and the text will not be wrapped on non-windows machines</remarks> /// <param name="writer">TextWriter to write the output to</param> /// <param name="optStyle">The style to use when printing possible option names</param> /// <param name="includeDefaultValues">True to include the default option values in the output</param> /// <param name="maxColumns">Wrap text at the given column (attempts to wrap at '-' or ' ')</param> public void ToText(TextWriter writer, OptStyle optStyle, bool includeDefaultValues, int maxColumns) { XsltArgumentList argList = new XsltArgumentList(); XslTransform trans = new XslTransform(); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "CommandLine.OptParse.Xslt.Text.xslt")) { trans.Load(new XmlTextReader(stream), null, Assembly.GetExecutingAssembly().Evidence); } argList.AddParam("shortOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "-" : "/"); argList.AddParam("longOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "--" : "/"); argList.AddParam("includeDefaultValues", string.Empty, includeDefaultValues); argList.AddParam("newline", string.Empty, Environment.NewLine); argList.AddExtensionObject("extObj", new TextTransformHelper(maxColumns)); if (maxColumns <= 0) maxColumns = -1; argList.AddParam("maxColumns", string.Empty, maxColumns); trans.Transform(this.Usage, argList, writer, null); }
/// <summary> /// Convert the usage to Text /// </summary> /// <param name="writer">TextWriter to write the output to</param> /// <param name="optStyle">The style to use when printing possible option names</param> /// <param name="includeDefaultValues">True to include the default option values in the output</param> public void ToText(TextWriter writer, OptStyle optStyle, bool includeDefaultValues) { ToText(writer, optStyle, includeDefaultValues, -1); }
/// <summary> /// Convert the usage to HTML /// </summary> /// <param name="writer">TextWriter to write the output to</param> /// <param name="optStyle">The style to use when printing possible option names</param> /// <param name="cssStyleSheet">Stylesheet URI to apply to the HTML content</param> /// <param name="includeDefaultValues">True to include the default option values in the output</param> /// <param name="title">Title to use for the HTML page</param> public void ToHtml(TextWriter writer, OptStyle optStyle, Uri cssStyleSheet, bool includeDefaultValues, string title) { XsltArgumentList argList = new XsltArgumentList(); XslTransform trans = new XslTransform(); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "CommandLine.OptParse.Xslt.Html.xslt")) { trans.Load(new XmlTextReader(stream), null, Assembly.GetExecutingAssembly().Evidence); } argList.AddParam("shortOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "-" : "/"); argList.AddParam("longOptPrefix", string.Empty, (optStyle == OptStyle.Unix) ? "--" : "/"); argList.AddParam("includeDefaultValues", string.Empty, includeDefaultValues); if (cssStyleSheet != null) argList.AddParam("cssStyleSheet", string.Empty, cssStyleSheet.AbsoluteUri); argList.AddParam("title", string.Empty, title); trans.Transform(this.Usage, argList, writer, null); }
private Regex BuildRegEx(OptStyle optStyle, UnixShortOption unixShortOption) { if (optStyle == OptStyle.Unix) return new Regex(@"^ (?: --(?<longname>[^\s=]+) (?<longvalue>=.+)? | -(?<shortname>\S) (?<shortvalue>.+)? )$", RegexOptions.IgnorePatternWhitespace); else return new Regex(@"^/(?<longname>[^\s:]+)(?<longvalue>:.+)?$"); }
/// <summary> /// Parse the arguments for options using the given settings /// </summary> /// <param name="optStyle">What type of options to parse</param> /// <param name="unixShortOption">How to parse unix short options (ignored /// if not unix style parsing)</param> /// <param name="dupOptHandleType">How to handle un-expected duplicate option /// definitions</param> /// <param name="unknownOptHandleType">How to handle options that were not /// defined</param> /// <param name="caseSesitive">If the parsing of options /// should consider the case of the options</param> /// <param name="args">The command-line arguments to parse</param> /// <returns>Arguments from the command-line that were not options</returns> public string[] Parse(OptStyle optStyle, UnixShortOption unixShortOption, DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType, bool caseSesitive, string[] args) { ArrayList foundDefinitions = new ArrayList(); Group grp; Match m; Regex re; StringCollection arguments; bool isShort; bool unknown; string name; string value; string[] results; if (args == null || args.Length == 0) { if (this.SearchEnvironment) ParseEnvironment(foundDefinitions, caseSesitive); return new string[0]; } arguments = new StringCollection(); re = BuildRegEx(optStyle, unixShortOption); for (int i = 0; i < args.Length; i++) { m = re.Match(args[i]); if (m.Success) { // see if there is a value grp = m.Groups["shortname"]; if (grp != null && grp.Value != string.Empty) { isShort = true; name = grp.Value; value = m.Groups["shortvalue"].Value; } else { isShort = false; name = m.Groups["longname"].Value; value = m.Groups["longvalue"].Value; // remove the '=' or ':' at the beginning of the value if (value != null && value.Length > 0) value = value.Substring(1); } if (optStyle == OptStyle.Unix) { if (isShort == false) ParseLongOption(args, name, value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); // see if the value is just concatinated short options else if (unixShortOption == UnixShortOption.CollapseShort) { // name is the first option ParseShortOption(args, name[0], null, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); // parse the rest of the options that lie in the 'value' variable if (value != null) { char[] unknownOptions; ParseShortOptions(args, value.ToLower().ToCharArray(), caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknownOptions); // don't do anything with unknown concatinated short options, // they should not be considered options or arguments } } else // name is the first option ParseShortOption(args, name[0], value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); } else { if (name.Length == 1) ParseShortOption(args, name[0], value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); else ParseLongOption(args, name, value, caseSesitive, ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions, out unknown); } // consider unknown options to be arguments if (unknown) arguments.Add(args[i]); } else arguments.Add(args[i]); } // parse the environment if (this.SearchEnvironment) ParseEnvironment(foundDefinitions, caseSesitive); results = new string[arguments.Count]; arguments.CopyTo(results, 0); return results; }
public void PrintUsage(OptStyle optStyle, IProgUsageInfo usageInfo, TextWriter writer, int columns) { foreach (string header in usageInfo.Headers) { writer.WriteLine(header); if (usageInfo.IsOptionHeader(header)) { NameValueCollection coll = GetOptionDescriptions(optStyle); for (int i = 0; i < coll.Count; i++) { PrintWithIndent(4, coll.GetKey(i), columns, writer); PrintWithIndent(8, coll[i], columns, writer); } } else PrintWithIndent(4, usageInfo.GetContents(header), columns, writer); } }
public NameValueCollection GetOptionDescriptions(OptStyle style) { NameValueCollection coll = new NameValueCollection(); OptionDefinition[] opts = _resultsHandler.GetOptions(); StringBuilder key = new StringBuilder(); foreach (OptionDefinition opt in opts) { key.Length = 0; if (opt.ShortNames != null && opt.ShortNames.Length > 0) { foreach (char optName in opt.ShortNames) { if (key.Length > 0) key.Append((style == OptStyle.Unix) ? ", -" : ", /"); else key.Append((style == OptStyle.Unix) ? "-" : "/"); key.Append(optName); } if (opt.LongNames != null && opt.LongNames.Length > 0) key.Append(", "); } if (opt.LongNames != null && opt.LongNames.Length > 0) { key.Append((style == OptStyle.Unix) ? "--" : "/"); key.Append(string.Join((style == OptStyle.Unix) ? ", --" : ", /", opt.LongNames)); } coll.Add(key.ToString(), opt.Description); } return coll; }