コード例 #1
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);
        }
コード例 #2
0
        public bool TryParse(RenameCommandOptions options)
        {
            var baseOptions = (DeleteOrRenameCommandOptions)options;

            if (!TryParse(baseOptions))
            {
                return(false);
            }

            options = (RenameCommandOptions)baseOptions;

            if (!TryParseHighlightOptions(
                    Highlight,
                    out HighlightOptions highlightOptions,
                    defaultValue: HighlightOptions.Replacement,
                    provider: OptionValueProviders.RenameHighlightOptionsProvider))
            {
                return(false);
            }

            if (!FilterParser.TryParse(
                    Name,
                    OptionNames.Name,
                    OptionValueProviders.PatternOptionsWithoutGroupAndNegativeProvider,
                    out Filter? nameFilter,
                    out FileNamePart namePart,
                    namePartProvider: OptionValueProviders.NamePartKindProvider_WithoutFullName))
            {
                return(false);
            }

            if (!FilterParser.TryParse(
                    Content,
                    OptionNames.Content,
                    OptionValueProviders.PatternOptionsWithoutPartProvider,
                    out Filter? contentFilter,
                    allowNull: true))
            {
                return(false);
            }

            if (!TryParseReplacement(Replacement, out string?replacement, out MatchEvaluator? matchEvaluator))
            {
                return(false);
            }

            if (matchEvaluator == null &&
                Evaluator != null)
            {
                LogHelpers.WriteObsoleteWarning(
                    $"Option '{OptionNames.GetHelpText(OptionNames.Evaluator)}' is obsolete. "
                    + $"Use option '{OptionNames.GetHelpText(OptionNames.Replacement)}' instead.");

                if (!DelegateFactory.TryCreateFromAssembly(Evaluator, typeof(string), typeof(Match), out matchEvaluator))
                {
                    return(false);
                }
            }

            if (!TryParseReplaceOptions(
                    Modify,
                    OptionNames.Modify,
                    replacement,
                    matchEvaluator,
                    out ReplaceOptions? replaceOptions))
            {
                return(false);
            }

            if (!TryParseAsEnum(
                    Conflict,
                    OptionNames.Conflict,
                    out ConflictResolution conflictResolution,
                    defaultValue: ConflictResolution.Ask,
                    provider: OptionValueProviders.ConflictResolutionProvider_WithoutSuffix))
            {
                return(false);
            }

            if (!TryParseDisplay(
                    values: Display,
                    optionName: OptionNames.Display,
                    contentDisplayStyle: out ContentDisplayStyle? contentDisplayStyle,
                    pathDisplayStyle: out PathDisplayStyle? pathDisplayStyle,
                    lineDisplayOptions: out LineDisplayOptions lineDisplayOptions,
                    lineContext: out LineContext lineContext,
                    displayParts: out DisplayParts displayParts,
                    fileProperties: out ImmutableArray <FileProperty> fileProperties,
                    indent: out string?indent,
                    separator: out string?separator,
                    noAlign: out bool noAlign,
                    contentDisplayStyleProvider: OptionValueProviders.ContentDisplayStyleProvider,
                    pathDisplayStyleProvider: OptionValueProviders.PathDisplayStyleProvider_Rename))
            {
                return(false);
            }

            if (pathDisplayStyle == PathDisplayStyle.Relative &&
                options.Paths.Length > 1 &&
                options.SortOptions != null)
            {
                pathDisplayStyle = PathDisplayStyle.Full;
            }

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle ?? ContentDisplayStyle.Omit,
                pathDisplayStyle: pathDisplayStyle ?? PathDisplayStyle.Full,
                lineOptions: lineDisplayOptions,
                lineContext: lineContext,
                displayParts: displayParts,
                fileProperties: fileProperties,
                indent: indent,
                separator: separator,
                alignColumns: !noAlign);

            options.HighlightOptions   = highlightOptions;
            options.SearchTarget       = GetSearchTarget();
            options.ReplaceOptions     = replaceOptions;
            options.AskMode            = (Ask) ? AskMode.File : AskMode.None;
            options.DryRun             = DryRun;
            options.NameFilter         = nameFilter;
            options.NamePart           = namePart;
            options.ContentFilter      = contentFilter;
            options.MaxMatchingFiles   = MaxCount;
            options.ConflictResolution = conflictResolution;
            options.Interactive        = Interactive;

            return(true);
        }