/// <summary> /// Parses a collection of command-line parameters. /// </summary> /// <param name="definition">The definition that the command-line arguments must correspond to.</param> /// <param name="args">The arguments to parse.</param> /// <param name="result">The </param> public void Parse(CommandLineDefinition definition, IEnumerable <string> args, CommandLineParseResult result) { var enumerator = args.GetEnumerator(); while (enumerator.MoveNext()) { var arg = enumerator.Current; if (string.IsNullOrEmpty(arg)) { continue; } var argName = ParseArgumentName(arg); var argObj = FindArgument(definition, argName); var argValue = ParseArgumentValue(argObj, enumerator); if (result.Arguments.ContainsKey(argValue)) { result.Arguments[argObj.LongName] = argValue; } else { result.Arguments.Add(argObj.LongName, argValue); } } EnsureRequiredArgumentsPresent(definition, result); }
/// <summary> /// Parses a collection of command-line parameters. /// </summary> /// <param name="definition">The definition that the command-line arguments must correspond to.</param> /// <param name="args">The arguments to parse.</param> /// <returns>The parsed argument values.</returns> public CommandLineParseResult Parse(CommandLineDefinition definition, IEnumerable <string> args) { var result = new CommandLineParseResult(); Parse(definition, args, result); return(result); }
private static CommandLineArgument FindArgument(CommandLineDefinition definition, string argName) { var argObj = definition.FindArgument(argName); if (argObj == null) { throw new UnrecognisedCommandLineArgumentException(); } return(argObj); }
/// <summary> /// Generates the usage information for a given command-line argument set. /// </summary> /// <returns>The usage information for the given command-line argument set.</returns> public string GenerateHelpText(CommandLineDefinition definition, string applicationName, string applicationDescription) { var helpText = new StringBuilder(); helpText.AppendLine(applicationDescription); helpText.AppendLine(); helpText.Append(applicationName); foreach (var arg in (from a in definition where a.Required orderby a.ShortName select a)) { helpText.AppendFormat(" -{0}", arg.ShortName); if (arg.RequiresValue) { helpText.Append(" \"value\""); } } foreach (var arg in (from a in definition where !a.Required orderby a.ShortName select a)) { helpText.Append(" ["); helpText.AppendFormat("-{0}", arg.ShortName); if (arg.RequiresValue) { helpText.Append(" \"value\""); } helpText.Append("]"); } helpText.AppendLine(); helpText.AppendLine(); var longestShortName = (from arg in definition select arg.ShortName.Length).Max() * -1; var longestLongName = (from arg in definition select arg.LongName.Length).Max() * -1; var formatString = string.Format(" {{0}} -{{1,{0}}} --{{2,{1}}} {{3}}{{4}}", longestShortName, longestLongName); foreach (var arg in definition.OrderByDescending(a => a.Required).ThenBy(a => a.ShortName)) { helpText.AppendFormat(formatString, arg.Required ? "R" : " ", arg.ShortName, arg.LongName, arg.Description, Environment.NewLine); } return(helpText.ToString()); }
/// <summary> /// Parses a collection of command-line parameters. /// </summary> /// <param name="definition">The definition that the command-line arguments must correspond to.</param> /// <param name="args">The arguments to parse.</param> /// <returns>The parsed argument values.</returns> public object Parse(CommandLineDefinition definition, string args) { return(Parse(definition, Split(args))); }