Beispiel #1
0
        public static bool TryParseModifyOptions(
            IEnumerable <string> values,
            string optionName,
            out ModifyOptions modifyOptions,
            out bool aggregateOnly)
        {
            modifyOptions = null;
            aggregateOnly = false;

            var           sortProperty = ValueSortProperty.None;
            List <string> options      = null;

            foreach (string value in values)
            {
                int index = value.IndexOf('=');

                if (index >= 0)
                {
                    string key    = value.Substring(0, index);
                    string value2 = value.Substring(index + 1);

                    if (OptionValues.SortBy.IsKeyOrShortKey(key))
                    {
                        if (!TryParseAsEnum(value2, optionName, out sortProperty, provider: OptionValueProviders.ValueSortPropertyProvider))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        string helpText = OptionValueProviders.ModifyFlagsProvider.GetHelpText();
                        WriteError($"Option '{OptionNames.GetHelpText(optionName)}' has invalid value '{value}'. Allowed values: {helpText}.");
                        return(false);
                    }
                }
                else
                {
                    (options ?? (options = new List <string>())).Add(value);
                }
            }

            var modifyFlags = ModifyFlags.None;

            if (options != null &&
                !TryParseAsEnumFlags(options, optionName, out modifyFlags, provider: OptionValueProviders.ModifyFlagsProvider))
            {
                return(false);
            }

            if ((modifyFlags & ModifyFlags.ExceptIntersect) == ModifyFlags.ExceptIntersect)
            {
                WriteError($"Values '{OptionValues.ModifyFlags_Except.HelpValue}' and '{OptionValues.ModifyFlags_Intersect.HelpValue}' cannot be use both at the same time.");
                return(false);
            }

            var functions = ModifyFunctions.None;

            if ((modifyFlags & ModifyFlags.Distinct) != 0)
            {
                functions |= ModifyFunctions.Distinct;
            }

            if ((modifyFlags & ModifyFlags.Ascending) != 0)
            {
                functions |= ModifyFunctions.Sort;
            }

            if ((modifyFlags & ModifyFlags.Descending) != 0)
            {
                functions |= ModifyFunctions.SortDescending;
            }

            if ((modifyFlags & ModifyFlags.Except) != 0)
            {
                functions |= ModifyFunctions.Except;
            }

            if ((modifyFlags & ModifyFlags.Intersect) != 0)
            {
                functions |= ModifyFunctions.Intersect;
            }

            if ((modifyFlags & ModifyFlags.RemoveEmpty) != 0)
            {
                functions |= ModifyFunctions.RemoveEmpty;
            }

            if ((modifyFlags & ModifyFlags.RemoveWhiteSpace) != 0)
            {
                functions |= ModifyFunctions.RemoveWhiteSpace;
            }

            if ((modifyFlags & ModifyFlags.TrimStart) != 0)
            {
                functions |= ModifyFunctions.TrimStart;
            }

            if ((modifyFlags & ModifyFlags.TrimEnd) != 0)
            {
                functions |= ModifyFunctions.TrimEnd;
            }

            if ((modifyFlags & ModifyFlags.ToLower) != 0)
            {
                functions |= ModifyFunctions.ToLower;
            }

            if ((modifyFlags & ModifyFlags.ToUpper) != 0)
            {
                functions |= ModifyFunctions.ToUpper;
            }

            aggregateOnly = (modifyFlags & ModifyFlags.AggregateOnly) != 0;

            if (modifyFlags != ModifyFlags.None)
            {
                modifyOptions = new ModifyOptions(
                    functions: functions,
                    aggregate: (modifyFlags & ModifyFlags.Aggregate) != 0 || aggregateOnly,
                    ignoreCase: (modifyFlags & ModifyFlags.IgnoreCase) != 0,
                    cultureInvariant: (modifyFlags & ModifyFlags.CultureInvariant) != 0,
                    sortProperty: sortProperty);
            }
            else
            {
                modifyOptions = ModifyOptions.Default;
            }

            return(true);
        }
Beispiel #2
0
        public static bool TryParseModifyOptions(
            IEnumerable <string> values,
            string optionName,
            EnumerableModifier <string>?modifier,
            [NotNullWhen(true)] out ModifyOptions?modifyOptions,
            out bool aggregateOnly)
        {
            modifyOptions = null;
            aggregateOnly = false;

            var           sortProperty = ValueSortProperty.None;
            List <string>?options      = null;

            foreach (string value in values)
            {
                int index = value.IndexOf('=');

                if (index >= 0)
                {
                    string key    = value.Substring(0, index);
                    string value2 = value.Substring(index + 1);

                    if (OptionValues.SortBy.IsKeyOrShortKey(key))
                    {
                        if (!TryParseAsEnum(
                                value2,
                                optionName,
                                out sortProperty,
                                provider: OptionValueProviders.ValueSortPropertyProvider))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        WriteOptionError(value, optionName, OptionValueProviders.ModifyFlagsProvider);
                        return(false);
                    }
                }
                else
                {
                    string value2 = value;

                    if (value2 == "ao")
                    {
                        LogHelpers.WriteObsoleteWarning(
                            $"Value '{value2}' is obsolete. Use value '{OptionValues.ModifyFlags_AggregateOnly.HelpValue}' instead.");

                        value2 = OptionValues.ModifyFlags_AggregateOnly.ShortValue;
                    }

                    (options ??= new List <string>()).Add(value2);
                }
            }

            var modifyFlags = ModifyFlags.None;

            if (options != null &&
                !TryParseAsEnumFlags(
                    options,
                    optionName,
                    out modifyFlags,
                    provider: OptionValueProviders.ModifyFlagsProvider))
            {
                return(false);
            }

            ModifyFlags except_Intersect_Group = modifyFlags & ModifyFlags.Except_Intersect_Group;

            if (except_Intersect_Group != ModifyFlags.None &&
                except_Intersect_Group != ModifyFlags.Except &&
                except_Intersect_Group != ModifyFlags.Intersect &&
                except_Intersect_Group != ModifyFlags.Group)
            {
                WriteError($"Values '{OptionValues.ModifyFlags_Except.HelpValue}', "
                           + $"'{OptionValues.ModifyFlags_Intersect.HelpValue}' and "
                           + $"'{OptionValues.ModifyFlags_Group.HelpValue}' cannot be used at the same time.");

                return(false);
            }

            var functions = ModifyFunctions.None;

            if ((modifyFlags & ModifyFlags.Distinct) != 0)
            {
                functions |= ModifyFunctions.Distinct;
            }

            if ((modifyFlags & ModifyFlags.Ascending) != 0)
            {
                functions |= ModifyFunctions.Sort;
            }

            if ((modifyFlags & ModifyFlags.Descending) != 0)
            {
                functions |= ModifyFunctions.SortDescending;
            }

            if ((modifyFlags & ModifyFlags.Except) != 0)
            {
                functions |= ModifyFunctions.Except;
            }

            if ((modifyFlags & ModifyFlags.Intersect) != 0)
            {
                functions |= ModifyFunctions.Intersect;
            }

            if ((modifyFlags & ModifyFlags.Group) != 0)
            {
                functions |= ModifyFunctions.Group;
            }

            if ((modifyFlags & ModifyFlags.RemoveEmpty) != 0)
            {
                functions |= ModifyFunctions.RemoveEmpty;
            }

            if ((modifyFlags & ModifyFlags.RemoveWhiteSpace) != 0)
            {
                functions |= ModifyFunctions.RemoveWhiteSpace;
            }

            if ((modifyFlags & ModifyFlags.TrimStart) != 0)
            {
                functions |= ModifyFunctions.TrimStart;
            }

            if ((modifyFlags & ModifyFlags.TrimEnd) != 0)
            {
                functions |= ModifyFunctions.TrimEnd;
            }

            if ((modifyFlags & ModifyFlags.ToLower) != 0)
            {
                functions |= ModifyFunctions.ToLower;
            }

            if ((modifyFlags & ModifyFlags.ToUpper) != 0)
            {
                functions |= ModifyFunctions.ToUpper;
            }

            if (sortProperty != ValueSortProperty.None &&
                (functions & ModifyFunctions.Sort) == 0 &&
                (functions & ModifyFunctions.SortDescending) == 0)
            {
                functions |= ModifyFunctions.Sort;
            }

            aggregateOnly = (modifyFlags & ModifyFlags.AggregateOnly) != 0;

            if (modifyFlags != ModifyFlags.None ||
                functions != ModifyFunctions.None ||
                modifier != null)
            {
                modifyOptions = new ModifyOptions(
                    functions: functions,
                    aggregate: (modifyFlags & ModifyFlags.Aggregate) != 0 || aggregateOnly,
                    ignoreCase: (modifyFlags & ModifyFlags.IgnoreCase) != 0,
                    cultureInvariant: (modifyFlags & ModifyFlags.CultureInvariant) != 0,
                    sortProperty: sortProperty,
                    modifier: modifier);
            }
            else
            {
                modifyOptions = ModifyOptions.Default;
            }

            return(true);
        }
Beispiel #3
0
        private void WriteAggregatedValues()
        {
            int           count         = 0;
            ModifyOptions modifyOptions = Options.ModifyOptions;
            List <string> allValues     = ((ListResultStorage)_storage).Values;

            if (_storageIndexes?.Count > 1)
            {
                Debug.Assert((modifyOptions.Functions & ModifyFunctions.ExceptIntersect) != 0, modifyOptions.Functions.ToString());

                if ((modifyOptions.Functions & ModifyFunctions.Except) != 0)
                {
                    if (_storageIndexes.Count > 2)
                    {
                        throw new InvalidOperationException($"'Except' operation cannot be applied on more than two {((ContentFilter != null) ? "files" : "directories")}.");
                    }

                    int index = _storageIndexes[0];

                    allValues = allValues
                                .Take(index)
                                .Except(GetRange(index, allValues.Count))
                                .ToList();
                }
                else if ((modifyOptions.Functions & ModifyFunctions.Intersect) != 0)
                {
                    allValues = Intersect();
                }
            }

            using (IEnumerator <string> en = allValues
                                             .Modify(modifyOptions, filter: ModifyFunctions.Enumerable)
                                             .GetEnumerator())
            {
                if (en.MoveNext())
                {
                    OutputSymbols symbols        = OutputSymbols.Create(Options.HighlightOptions);
                    ConsoleColors colors         = ((Options.HighlightOptions & HighlightOptions.Match) != 0) ? Colors.Match : default;
                    ConsoleColors boundaryColors = (Options.HighlightBoundary) ? Colors.MatchBoundary : default;
                    var           valueWriter    = new ValueWriter(new ContentTextWriter(Verbosity.Minimal), includeEndingIndent: false);

                    ConsoleOut.WriteLineIf(ShouldWriteLine(ConsoleOut.Verbosity));
                    Out?.WriteLineIf(ShouldWriteLine(Out.Verbosity));

                    do
                    {
                        valueWriter.Write(en.Current, symbols, colors, boundaryColors);
                        WriteLine(Verbosity.Minimal);
                        count++;
                    } while (en.MoveNext());

                    if (ShouldLog(Verbosity.Detailed) ||
                        Options.IncludeSummary)
                    {
                        WriteLine(Verbosity.Minimal);
                        WriteCount("Values", count, verbosity: Verbosity.Minimal);
                        WriteLine(Verbosity.Minimal);
                    }
                }
            }

            List <string> Intersect()
            {
                var list = new List <string>(GetRange(0, _storageIndexes[0]));

                for (int i = 1; i < _storageIndexes.Count; i++)
                {
                    IEnumerable <string> second = GetRange(_storageIndexes[i - 1], _storageIndexes[i]);

                    list = list.Intersect(second, modifyOptions.StringComparer).ToList();
                }

                return(list);
            }

            IEnumerable <string> GetRange(int start, int end)
            {
                for (int i = start; i < end; i++)
                {
                    yield return(allValues[i]);
                }
            }

            bool ShouldWriteLine(Verbosity verbosity)
            {
                if (verbosity > Verbosity.Minimal)
                {
                    return(true);
                }

                return(verbosity == Verbosity.Minimal &&
                       (Options.PathDisplayStyle != PathDisplayStyle.Omit || Options.IncludeSummary));
            }
        }