/// <summary> /// Converts the given group results and match /// into an Argument object. /// </summary> /// <param name="Groups">The group descriptors that were used to get the given match.</param> /// <param name="Match">The match to process.</param> /// <returns></returns> private static Argument ConvertMatchToArgument(ArgumentGroupID[] Groups, Match Match) { // Find the argument type that was matched Argument.ArgumentType ArgumentType = Argument.ArgumentType.Unknown; foreach (ArgumentGroupID Group in Groups) { if (Match.Groups[Group.GroupName].Success) { ArgumentType = Group.ArgumentType; } } // Isolate the groups that were matched var TypeGroups = Groups.Where(Group => Group.ArgumentType == ArgumentType); // Create an object to be returned Argument Result = new Argument() { Type = ArgumentType }; // Set all of the properties from the group name foreach (ArgumentGroupID Group in TypeGroups) { ReflectionUtils.SetProperty(Result, Group.PropertyName, Match.Groups[Group.GroupName].Value); } // Return the result return(Result); }
/// <summary> /// Parses the given argument string. /// </summary> /// <param name="ArgumentList">The argument string to be parsed.</param> /// <returns>An array of argument objects</returns> public static Argument[] Parse(string ArgumentList) { // Create the regex object for use in parsing args Regex ArgRegex = new Regex(ArgPattern, RegexOptions.ExplicitCapture); // Get the groups from the regex ArgumentGroupID[] Groups = ArgRegex.GetGroupNames().Skip(1).Select(Name => { // Split by underscores string[] NameParts = Name.Split('_'); // Parse the type for the argument Argument.ArgumentType Type = (Argument.ArgumentType)Enum.Parse(typeof(Argument.ArgumentType), NameParts[0]); // Construct a result return(new ArgumentGroupID() { ArgumentType = Type, PropertyName = NameParts[1] }); }).ToArray(); return(ArgRegex.Matches(ArgumentList).Cast <Match>().Select(m => ConvertMatchToArgument(Groups, m)).ToArray()); }