/// <summary> /// Adds parsed option to a ParseResult object. This method is called by Parser. /// </summary> /// <param name="parsedOption">ParsedOption object to be added to a ParseResult object.</param> internal void addOption(ParsedOption parsedOption) { ParsedOptions.Add(parsedOption); foreach (var name in parsedOption.Names) { parsedOptions.Add(name, parsedOption); } }
/// <summary> /// Provided a reference to a Match object, which is resulted from matching an argument against a grouped option regex, /// a reference to a ParseResult object and a reference to the corresponding ProgramSettings object, tries to parse a /// group of options, except for the last option. Furthermore, the name of the last option and its string parameter value /// (if it is provided as part of the same argument) are assigned to out-parameters of the method. (The method /// tryParseSingleOption is to be then used for the last option, since that method handles the possibility of a /// parameter value provided in a separate argument.) /// </summary> /// <param name="groupedOptionsMatch">A regex Match object generated by matching an argument string against a Regex object for grouped options.</param> /// <param name="result">ParseResult object. If legal options are detected, they are added to the object via its addOption method.</param> /// <param name="settings">Corresponding ProgramSettings object, used for checking if detected options exist.</param> /// <param name="message">String to which a potential error message is assigned. Otherwise, becomes an empty string.</param> /// <param name="lastOptionName">String to which name of the last option within the evaluated group of options is assigned. Otherwise if parsing fails, becomes an empty string.</param> /// <param name="parameterString">String to which string parameter value of the last option is assigned. Otherwise if parsing fails, becomes an empty string.</param> /// <returns>Returns a Boolean value: were grouped options parsed successfully?</returns> internal static bool tryParseGroupedOptions(ref Match groupedOptionsMatch, ref ParseResult result, ref ProgramSettings settings, out string message, out string lastOptionName, out string parameterString) { char[] groupedOptionNames = groupedOptionsMatch.Groups["optionName"].ToString().ToCharArray(); for (int i = 0; i < (groupedOptionNames.Length - 1); ++i) { // try adding all options but the last (these do not have any parameters specified) Option groupedOption; string groupedOptionName = groupedOptionNames[i].ToString(); if (!settings.OptionsDictionary.TryGetValue(groupedOptionName, out groupedOption)) // get option name { // if option name not recognised, error message = String.Format(Properties.Resources.Parser_tryParseGroupedOptions_InvalidOptionName, groupedOptionName); lastOptionName = ""; parameterString = ""; return(false); } else { // if recognised if (groupedOption.Parameter == null || !groupedOption.Parameter.IsMandatory) { // if no parameter needed, add it to results ParsedOption parsedOption = new ParsedOption(groupedOption.Names, value: null); result.addOption(parsedOption); } else { // otherwise if parameter needed, error message = String.Format(Properties.Resources.Parser_tryParseGroupedOptions_NoParameterValue, groupedOptionName); lastOptionName = ""; parameterString = ""; return(false); } } } message = ""; lastOptionName = groupedOptionNames[groupedOptionNames.Length - 1].ToString(); parameterString = groupedOptionsMatch.Groups["parameterString"].ToString(); return(true); }
/// <summary> /// Provided a reference to a Regex object for matching an argument string (possibly with parameter value), an option name string, /// a parameter value string, reference to a string array of arguments, an iterator (index), a reference to a ParseResult /// object and a reference to a corresponding ProgramSettings object, tries to parse a single option, with a possible parameter value. /// If successful, adds the option to the given ParseResult object using its addOption method. /// </summary> /// <param name="parameterRegex">Regex for matching a parameter value string.</param> /// <param name="optionName">Name of the option which should be parsed.</param> /// <param name="parameterString">Relevant parameter string value.</param> /// <param name="args">String array of arguments.</param> /// <param name="argsIterator">Index of element in the arguments array which is being evaluated.</param> /// <param name="result">ParseResult object. If option is legal, it is added, possibly with a corresponding parameter value, to the result via its addOption method.</param> /// <param name="settings">Corresponding ProgramSettings object, used for checking whether the parsed option and its potential parameter value are legal.</param> /// <param name="message">String to which a potential error message is assigned. Otherwise, becomes an empty string.</param> /// <returns>Returns a Boolean value: was the option (and, potentially, its parameter value) parsed successfully?</returns> internal static bool tryParseSingleOption(ref Regex parameterRegex, string optionName, string parameterString, ref string[] args, int argsIterator, ref ParseResult result, ref ProgramSettings settings, out string message) { Option option; object parameterValue = null; if (!settings.OptionsDictionary.TryGetValue(optionName, out option)) // get option name { // if option name not recognised, error message = String.Format(Properties.Resources.Parser_tryParseSingleOption_InvalidOptionName, optionName); return(false); } if (parameterString == "" && option.Parameter != null) { // if no parameter value was provided yet and option accepts parameter if ((argsIterator + 1) < args.Length) { // look for separate parameter value in the following position var parameterMatch = parameterRegex.Match(args[argsIterator + 1]); if (parameterMatch.Success) { // if separate parameter value found parameterString = parameterMatch.Groups["parameterString"].ToString(); if (!option.Parameter.TryParse(parameterString, out parameterValue)) // get parameter value { // if it is invalid, error message = String.Format(Properties.Resources.Parser_tryParseSingleOption_InvalidParameterValue, optionName); return(false); } } // otherwise OK else { // else if value not found in the following position if (option.Parameter.IsMandatory) { // if it is mandatory, error message = String.Format(Properties.Resources.Parser_tryParseSingleOption_NoParameterValue, optionName); return(false); } parameterValue = null; } // otherwise OK } } else { // else if parameter value was provided, OR was not provided BUT option does not accept parameter... if (parameterString != "" && option.Parameter == null) { // if option does not accept parameter and parameter is provided, error message = String.Format(Properties.Resources.Parser_tryParseSingleOption_TooManyParameterValue, optionName); return(false); } // else if (parameterString != "") { if (!option.Parameter.TryParse(parameterString, out parameterValue)) // get parameter value { // if parameter value is invalid, error message = String.Format(Properties.Resources.Parser_tryParseSingleOption_InvalidParameterValue, optionName); return(false); } } else { // otherwise OK parameterValue = null; } } ParsedOption parsedOption = new ParsedOption(option.Names, parameterValue); result.addOption(parsedOption); message = ""; return(true); }