Beispiel #1
0
        public static bool TryParseInput(
            IEnumerable <string> values,
            out string input)
        {
            if (!values.Any())
            {
                throw new InvalidOperationException("Input is missing.");
            }

            input = values.First();

            if (!TryParseAsEnumFlags(
                    values.Skip(1),
                    OptionNames.Input,
                    out InputOptions options,
                    InputOptions.None,
                    OptionValueProviders.InputOptionsProvider))
            {
                return(false);
            }

            if ((options & InputOptions.Escape) != 0)
            {
                input = RegexEscape.ConvertCharacterEscapes(input);
            }

            return(true);
        }
Beispiel #2
0
        public static bool TryParseReplacement(
            IEnumerable <string> values,
            out string replacement)
        {
            if (!values.Any())
            {
                replacement = null;
                return(true);
            }

            replacement = values.First();

            if (!TryParseAsEnumFlags(values.Skip(1), OptionNames.Replacement, out ReplacementOptions options, ReplacementOptions.None, OptionValueProviders.ReplacementOptionsProvider))
            {
                return(false);
            }

            if ((options & ReplacementOptions.FromFile) != 0 &&
                !FileSystemHelpers.TryReadAllText(replacement, out replacement))
            {
                return(false);
            }

            if ((options & ReplacementOptions.Literal) != 0)
            {
                replacement = RegexEscape.EscapeSubstitution(replacement);
            }

            if ((options & ReplacementOptions.CharacterEscapes) != 0)
            {
                replacement = RegexEscape.ConvertCharacterEscapes(replacement);
            }

            return(true);
        }
Beispiel #3
0
        public static bool TryParseReplacement(
            IEnumerable <string> values,
            out string?replacement,
            out MatchEvaluator?matchEvaluator)
        {
            replacement    = null;
            matchEvaluator = null;

            if (!values.Any())
            {
                return(true);
            }

            string value = values.First();

            if (!TryParseAsEnumFlags(
                    values.Skip(1),
                    OptionNames.Replacement,
                    out ReplacementOptions options,
                    ReplacementOptions.None,
                    OptionValueProviders.ReplacementOptionsProvider))
            {
                return(false);
            }

            if ((options & ReplacementOptions.FromFile) != 0 &&
                !FileSystemHelpers.TryReadAllText(value, out value !, ex => WriteError(ex)))
            {
                return(false);
            }

            if ((options & ReplacementOptions.CSharp) != 0)
            {
                if ((options & ReplacementOptions.FromFile) != 0)
                {
                    return(DelegateFactory.TryCreateFromSourceText(value, typeof(string), typeof(Match), out matchEvaluator));
                }
                else
                {
                    return(DelegateFactory.TryCreateFromExpression(
                               value,
                               "EvaluatorClass",
                               "EvaluatorMethod",
                               "string",
                               typeof(string),
                               "Match",
                               typeof(Match),
                               "match",
                               out matchEvaluator));
                }
            }
            else if ((options & ReplacementOptions.FromDll) != 0)
            {
                return(DelegateFactory.TryCreateFromAssembly(value, typeof(string), typeof(Match), out matchEvaluator));
            }
            else
            {
                replacement = value;

                if ((options & ReplacementOptions.Literal) != 0)
                {
                    replacement = RegexEscape.EscapeSubstitution(replacement);
                }

                if ((options & ReplacementOptions.Escape) != 0)
                {
                    replacement = RegexEscape.ConvertCharacterEscapes(replacement);
                }
            }

            return(true);
        }
Beispiel #4
0
        public static bool TryParseDisplay(
            IEnumerable <string> values,
            string optionName,
            out ContentDisplayStyle?contentDisplayStyle,
            out PathDisplayStyle?pathDisplayStyle,
            out LineDisplayOptions lineDisplayOptions,
            out LineContext lineContext,
            out DisplayParts displayParts,
            out ImmutableArray <FileProperty> fileProperties,
            out string?indent,
            out string?separator,
            out bool noAlign,
            OptionValueProvider?contentDisplayStyleProvider = null,
            OptionValueProvider?pathDisplayStyleProvider    = null)
        {
            contentDisplayStyle = null;
            pathDisplayStyle    = null;
            lineDisplayOptions  = LineDisplayOptions.None;
            lineContext         = default;
            displayParts        = DisplayParts.None;
            fileProperties      = ImmutableArray <FileProperty> .Empty;
            indent    = null;
            separator = null;
            noAlign   = false;

            ImmutableArray <FileProperty> .Builder?builder = 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 (key == "t")
                    {
                        LogHelpers.WriteObsoleteWarning($"Value '{key}' is obsolete. "
                                                        + $"Use value '{OptionValues.Display_Context.HelpValue}' instead.");

                        key = OptionValues.Display_Context.ShortKey;
                    }
                    else if (key == "ta")
                    {
                        LogHelpers.WriteObsoleteWarning($"Value '{key}' is obsolete. "
                                                        + $"Use value '{OptionValues.Display_ContextAfter.HelpValue}' instead.");

                        key = OptionValues.Display_ContextAfter.ShortKey;
                    }
                    else if (key == "tb")
                    {
                        LogHelpers.WriteObsoleteWarning($"Value '{key}' is obsolete. "
                                                        + $"Use value '{OptionValues.Display_ContextBefore.HelpValue}' instead.");

                        key = OptionValues.Display_ContextBefore.ShortKey;
                    }

                    if (OptionValues.Display_Content.IsKeyOrShortKey(key))
                    {
                        if (!TryParseAsEnum(
                                value2,
                                optionName,
                                out ContentDisplayStyle contentDisplayStyle2,
                                provider: contentDisplayStyleProvider))
                        {
                            return(false);
                        }

                        contentDisplayStyle = contentDisplayStyle2;
                    }
                    else if (OptionValues.Display_Path.IsKeyOrShortKey(key))
                    {
                        if (!TryParseAsEnum(
                                value2,
                                optionName,
                                out PathDisplayStyle pathDisplayStyle2,
                                provider: pathDisplayStyleProvider))
                        {
                            return(false);
                        }

                        pathDisplayStyle = pathDisplayStyle2;
                    }
                    else if (OptionValues.Display_Indent.IsKeyOrShortKey(key))
                    {
                        indent = value2;
                    }
                    else if (OptionValues.Display_Separator.IsKeyOrShortKey(key))
                    {
                        separator = RegexEscape.ConvertCharacterEscapes(value2);
                    }
                    else if (OptionValues.Display_Context.IsKeyOrShortKey(key))
                    {
                        if (!TryParseCount(value2, OptionNames.Display, out int count, value))
                        {
                            return(false);
                        }

                        lineContext = new LineContext(count);
                    }
                    else if (OptionValues.Display_ContextBefore.IsKeyOrShortKey(key))
                    {
                        if (!TryParseCount(value2, OptionNames.Display, out int before, value))
                        {
                            return(false);
                        }

                        lineContext = lineContext.WithBefore(before);
                    }
                    else if (OptionValues.Display_ContextAfter.IsKeyOrShortKey(key))
                    {
                        if (!TryParseCount(value2, OptionNames.Display, out int after, value))
                        {
                            return(false);
                        }

                        lineContext = lineContext.WithAfter(after);
                    }
                    else
                    {
                        WriteOptionError(value, optionName, OptionValueProviders.DisplayProvider);
                        return(false);
                    }
                }
                else if (OptionValues.Display_Summary.IsValueOrShortValue(value))
                {
                    displayParts |= DisplayParts.Summary;
                }
                else if (OptionValues.Display_Count.IsValueOrShortValue(value))
                {
                    displayParts |= DisplayParts.Count;
                }
                else if (OptionValues.Display_CreationTime.IsValueOrShortValue(value))
                {
                    (builder ??= ImmutableArray.CreateBuilder <FileProperty>()).Add(FileProperty.CreationTime);
                }
                else if (OptionValues.Display_ModifiedTime.IsValueOrShortValue(value))
                {
                    (builder ??= ImmutableArray.CreateBuilder <FileProperty>()).Add(FileProperty.ModifiedTime);
                }
                else if (OptionValues.Display_Size.IsValueOrShortValue(value))
                {
                    (builder ??= ImmutableArray.CreateBuilder <FileProperty>()).Add(FileProperty.Size);
                }
                else if (OptionValues.Display_LineNumber.IsValueOrShortValue(value))
                {
                    lineDisplayOptions |= LineDisplayOptions.IncludeLineNumber;
                }
                else if (OptionValues.Display_TrimLine.IsValueOrShortValue(value))
                {
                    lineDisplayOptions |= LineDisplayOptions.TrimLine;
                }
                else if (OptionValues.Display_TrimLine.IsValueOrShortValue(value))
                {
                    lineDisplayOptions |= LineDisplayOptions.TrimLine;
                }
                else if (OptionValues.Display_TrimLine.IsValueOrShortValue(value))
                {
                    lineDisplayOptions |= LineDisplayOptions.TrimLine;
                }
                else if (OptionValues.Display_NoAlign.IsValueOrShortValue(value))
                {
                    noAlign = true;
                }
                else
                {
                    WriteOptionError(value, optionName, OptionValueProviders.DisplayProvider);
                    return(false);
                }
            }

            if (builder != null)
            {
                fileProperties = builder.ToImmutableArray();
            }

            return(true);
        }