/// <summary> /// Determines if the specified call-chain is present. /// </summary> /// <param name="callChain">The call-chain to check.</param> /// <param name="options">The flags which dictate what determines an argument as found.</param> /// <param name="comparer">The comparer to use when determining if a match was found.</param> /// <param name="argumentsToParse"> /// The collection to search. /// Specifying null will default to <see cref="Arguments"/>. /// </param> /// <returns> /// Returns true if the apecified argument was found using the supplied flags; otherwise, returns false. /// </returns> /// <exception cref="System.ArgumentException"> /// Throws when an invalid flag combination is given. /// </exception> public static bool Found(this IEnumerable <ICommandLineArgument> callChain, CmdLineSearchOptions options, StringComparison?comparer = null, IEnumerable <string> argumentsToParse = null) { var workingArg = callChain.Last(); var parentCallChain = callChain.Take(callChain.Count() - 1); return(workingArg.Found(options, comparer, parentCallChain.GetSegment(comparer, argumentsToParse))); }
/// <summary> /// Determines if the specified grouping is present. /// </summary> /// <param name="grouping">The grouping to check.</param> /// <param name="options">The flags which dictate what determines an argument as found.</param> /// <param name="comparer">The comparer to use when determining if a match was found.</param> /// <param name="argumentsToParse"> /// The collection to search. /// Specifying null will default to <see cref="Arguments"/>. /// </param> /// <returns> /// Returns true if the apecified argument was found using the supplied flags; otherwise, returns false. /// </returns> /// <exception cref="System.ArgumentException"> /// Throws when an invalid flag combination is given. /// </exception> public static bool Found(this ICommandLineGrouping grouping, CmdLineSearchOptions options, StringComparison?comparer = null, IEnumerable <string> argumentsToParse = null) => grouping.ParentCallChain.Concat(grouping.Children).Found(options, comparer, argumentsToParse);
/// <summary> /// Determines if the specified argument is present. /// </summary> /// <param name="rootArgument">The <see cref="ICommandLineArgument"/> to check.</param> /// <param name="options">The flags which dictate what determines an argument as found.</param> /// <param name="comparer">The comparer to use when determining if a match was found.</param> /// <param name="argumentsToParse"> /// The collection to search. /// Specifying null will default to <see cref="Arguments"/>. /// </param> /// <returns> /// Returns true if the apecified argument was found using the supplied flags; otherwise, returns false. /// </returns> /// <exception cref="System.ArgumentException"> /// Throws when an invalid flag combination is given. /// </exception> public static bool Found(this ICommandLineArgument rootArgument, CmdLineSearchOptions options, StringComparison?comparer = null, IEnumerable <string> argumentsToParse = null) { if (options.HasFlag(CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithoutChildren)) { throw new ArgumentException($"Invalid flag combination. {options}", nameof(options)); } if (options.HasFlag(CmdLineSearchOptions.WithSiblings | CmdLineSearchOptions.WithoutSiblings)) { throw new ArgumentException($"Invalid flag combination. {options}", nameof(options)); } if (options.HasFlag(CmdLineSearchOptions.WithParams | CmdLineSearchOptions.WithoutParams)) { throw new ArgumentException($"Invalid flag combination. {options}", nameof(options)); } var segment = rootArgument.GetSegment(comparer, argumentsToParse); if (options == CmdLineSearchOptions.None) { return(segment.Any()); } if (!segment.Any()) { return(false); } if (options == CmdLineSearchOptions.WithChildren) { return(segment.Count() > 1); } if (options == CmdLineSearchOptions.WithoutChildren) { return(segment.Count() == 1); } if (options == CmdLineSearchOptions.WithParams) { return(segment.Any() && rootArgument.GetParams().Any()); } if (options == CmdLineSearchOptions.WithoutParams) { return(segment.Any() && !rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithParams)) { return(segment.Count() > 1 && rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithoutParams)) { return(segment.Count() > 1 && !rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithParams)) { return(segment.Count() == 1 && rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithoutParams)) { return(segment.Count() == 1 && !rootArgument.GetParams().Any()); } var hasSiblings = (argumentsToParse ?? Arguments).Any(str => str.StartsWith(rootArgument.Specification.Delimiter) && !rootArgument.Command.Equals(str, (comparer ?? Comparer))); if (options == CmdLineSearchOptions.WithSiblings) { return(segment.Any() && hasSiblings); } if (options == CmdLineSearchOptions.WithoutSiblings) { return(segment.Any() && !hasSiblings); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithoutSiblings)) { return(segment.Count() == 1 && !hasSiblings); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithoutSiblings)) { return(segment.Count() > 1 && !hasSiblings); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithSiblings)) { return(segment.Count() == 1 && hasSiblings); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithSiblings)) { return(segment.Count() > 1 && hasSiblings); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithoutSiblings | CmdLineSearchOptions.WithoutParams)) { return(segment.Count() == 1 && !hasSiblings && !rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithoutSiblings | CmdLineSearchOptions.WithoutParams)) { return(segment.Count() > 1 && !hasSiblings && !rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithSiblings | CmdLineSearchOptions.WithoutParams)) { return(segment.Count() == 1 && hasSiblings && !rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithSiblings | CmdLineSearchOptions.WithoutParams)) { return(segment.Count() > 1 && hasSiblings && !rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithoutSiblings | CmdLineSearchOptions.WithParams)) { return(segment.Count() == 1 && !hasSiblings && rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithoutSiblings | CmdLineSearchOptions.WithParams)) { return(segment.Count() > 1 && !hasSiblings && rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithoutChildren | CmdLineSearchOptions.WithSiblings | CmdLineSearchOptions.WithParams)) { return(segment.Count() == 1 && hasSiblings && rootArgument.GetParams().Any()); } if (options == (CmdLineSearchOptions.WithChildren | CmdLineSearchOptions.WithSiblings | CmdLineSearchOptions.WithParams)) { return(segment.Count() > 1 && hasSiblings && rootArgument.GetParams().Any()); } throw new ArgumentException("Could not find a search match pattern using the supplied search options.", nameof(options)); }