Inheritance: Orang.CommandLine.CommonFindCommandOptions
Esempio n. 1
0
 internal static void WriteReplaceCommand(ReplaceCommandOptions options)
 {
     WriteOption("ask", options.AskMode);
     WriteOption("attributes", options.Attributes);
     WriteOption("attributes to skip", options.AttributesToSkip);
     WriteFilter("content filter", options.ContentFilter);
     WriteEncoding("default encoding", options.DefaultEncoding);
     WriteFilter("directory filter", options.DirectoryFilter, options.DirectoryNamePart);
     WriteDisplayFormat("display", options.Format);
     WriteOption("dry run", options.DryRun);
     WriteOption("empty", options.EmptyOption);
     WriteEvaluator("evaluator", options.ReplaceOptions.MatchEvaluator);
     WriteFilter("extension filter", options.ExtensionFilter);
     WriteFilePropertyFilter(
         "file properties",
         options.SizePredicate,
         options.CreationTimePredicate,
         options.ModifiedTimePredicate);
     WriteOption("highlight options", options.HighlightOptions);
     WriteInput(options.Input);
     WriteOption("interactive", options.Interactive);
     WriteOption("max matching files", options.MaxMatchingFiles);
     WriteOption("max matches in file", options.MaxMatchesInFile);
     WriteReplaceModify("modify", options.ReplaceOptions);
     WriteFilter("name filter", options.NameFilter, options.NamePart);
     WritePaths("paths", options.Paths);
     WriteOption("progress", options.Progress);
     WriteOption("recurse subdirectories", options.RecurseSubdirectories);
     WriteOption("replacement", options.ReplaceOptions.Replacement);
     WriteOption("search target", options.SearchTarget);
     WriteSortOptions("sort", options.SortOptions);
 }
Esempio n. 2
0
        private static int Replace(ReplaceCommandLineOptions commandLineOptions)
        {
            var options = new ReplaceCommandOptions();

            if (!commandLineOptions.TryParse(options))
            {
                return(2);
            }

            return(Execute(new ReplaceCommand(options)));
        }
        public bool TryParse(ReplaceCommandOptions options)
        {
            var baseOptions = (CommonReplaceCommandOptions)options;

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

            options = (ReplaceCommandOptions)baseOptions;

            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);
            }

            options.Replacer = replaceOptions;
#if DEBUG // --find
            if (Find)
            {
                options.Replacer         = ReplaceOptions.Empty;
                options.HighlightOptions = HighlightOptions.Match;
                options.DryRun           = true;
            }
#endif
            return(true);
        }
Esempio n. 4
0
        public bool TryParse(ReplaceCommandOptions options)
        {
            var baseOptions = (FileSystemCommandOptions)options;

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

            options = (ReplaceCommandOptions)baseOptions;

            if (!TryParseProperties(Ask, Name, options))
            {
                return(false);
            }

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

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

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

            if (!DelegateFactory.TryCreateMatchEvaluator(Evaluator, out MatchEvaluator? matchEvaluator))
            {
                return(false);
            }

            if (replacement != null && matchEvaluator != null)
            {
                WriteError($"Options '{OptionNames.GetHelpText(OptionNames.Replacement)}' and '{OptionNames.GetHelpText(OptionNames.Evaluator)}' cannot be set both at the same time.");
                return(false);
            }

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

            if (!TryParseMaxCount(MaxCount, out int maxMatchingFiles, out int maxMatchesInFile))
            {
                return(false);
            }

            ContentDisplayStyle contentDisplayStyle;
            PathDisplayStyle    pathDisplayStyle;

            if (!TryParseDisplay(
                    values: Display,
                    optionName: OptionNames.Display,
                    contentDisplayStyle: out ContentDisplayStyle? contentDisplayStyle2,
                    pathDisplayStyle: out PathDisplayStyle? pathDisplayStyle2,
                    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,
                    contentDisplayStyleProvider: OptionValueProviders.ContentDisplayStyleProvider_WithoutUnmatchedLines,
                    pathDisplayStyleProvider: OptionValueProviders.PathDisplayStyleProvider))
            {
                return(false);
            }

            if (contentDisplayStyle2 != null)
            {
                if (options.AskMode == AskMode.Value &&
                    contentDisplayStyle2 == ContentDisplayStyle.AllLines)
                {
                    WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Display)}' cannot have value '{OptionValueProviders.ContentDisplayStyleProvider.GetValue(nameof(ContentDisplayStyle.AllLines)).HelpValue}' when option '{OptionNames.GetHelpText(OptionNames.Ask)}' has value '{OptionValueProviders.AskModeProvider.GetValue(nameof(AskMode.Value)).HelpValue}'.");
                    return(false);
                }

                contentDisplayStyle = contentDisplayStyle2.Value;
            }
            else if (Input != null)
            {
                contentDisplayStyle = ContentDisplayStyle.AllLines;
            }
            else
            {
                contentDisplayStyle = ContentDisplayStyle.Line;
            }

            pathDisplayStyle = pathDisplayStyle2 ?? PathDisplayStyle.Full;

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

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle,
                pathDisplayStyle: pathDisplayStyle,
                lineOptions: lineDisplayOptions,
                lineContext: lineContext,
                displayParts: displayParts,
                fileProperties: fileProperties,
                indent: indent,
                separator: separator);

            options.HighlightOptions = highlightOptions;
            options.ContentFilter    = contentFilter;
            options.ReplaceOptions   = replaceOptions;
            options.Input            = Input;
            options.DryRun           = DryRun;
            options.MaxMatchesInFile = maxMatchesInFile;
            options.MaxMatchingFiles = maxMatchingFiles;
            options.MaxTotalMatches  = 0;

            return(true);
        }
Esempio n. 5
0
        public bool TryParse(ReplaceCommandOptions options)
        {
            if (!TryParseAsEnum(
                    Pipe,
                    OptionNames.Pipe,
                    out PipeMode pipeMode,
                    PipeMode.None,
                    OptionValueProviders.PipeMode))
            {
                return(false);
            }

            if (pipeMode == PipeMode.None)
            {
                if (Console.IsInputRedirected)
                {
                    PipeMode = PipeMode.Text;
                }
            }
            else
            {
                if (!Console.IsInputRedirected)
                {
                    WriteError("Redirected/piped input is required "
                               + $"when option '{OptionNames.GetHelpText(OptionNames.Pipe)}' is specified.");

                    return(false);
                }

                PipeMode = pipeMode;
            }

            if (!TryParseProperties(Ask, Name, options))
            {
                return(false);
            }

            var baseOptions = (FileSystemCommandOptions)options;

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

            options = (ReplaceCommandOptions)baseOptions;

            if (!FilterParser.TryParse(
                    Content,
                    OptionNames.Content,
                    OptionValueProviders.PatternOptionsWithoutGroupAndPartAndNegativeProvider,
                    out Filter? contentFilter))
            {
                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 (!TryParseMaxCount(MaxCount, out int maxMatchingFiles, out int maxMatchesInFile))
            {
                return(false);
            }

            string?input = null;

            if (Input.Any() &&
                !TryParseInput(Input, out input))
            {
                return(false);
            }

            if (pipeMode != PipeMode.Paths &&
                Console.IsInputRedirected)
            {
                if (input != null)
                {
                    WriteError("Cannot use both redirected/piped input and "
                               + $"option '{OptionNames.GetHelpText(OptionNames.Input)}'.");

                    return(false);
                }

                if (contentFilter == null)
                {
                    WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Content)}' is required "
                               + "when redirected/piped input is used as a text to be searched.");

                    return(false);
                }

                input = ConsoleHelpers.ReadRedirectedInput();
            }

            ContentDisplayStyle contentDisplayStyle;
            PathDisplayStyle    pathDisplayStyle;

            if (!TryParseDisplay(
                    values: Display,
                    optionName: OptionNames.Display,
                    contentDisplayStyle: out ContentDisplayStyle? contentDisplayStyle2,
                    pathDisplayStyle: out PathDisplayStyle? pathDisplayStyle2,
                    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_WithoutUnmatchedLines,
                    pathDisplayStyleProvider: OptionValueProviders.PathDisplayStyleProvider))
            {
                return(false);
            }

            if (contentDisplayStyle2 != null)
            {
                if (options.AskMode == AskMode.Value &&
                    contentDisplayStyle2 == ContentDisplayStyle.AllLines)
                {
                    string helpValue = OptionValueProviders.ContentDisplayStyleProvider
                                       .GetValue(nameof(ContentDisplayStyle.AllLines))
                                       .HelpValue;

                    string helpValue2 = OptionValueProviders.AskModeProvider.GetValue(nameof(AskMode.Value)).HelpValue;

                    WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Display)}' cannot have value "
                               + $"'{helpValue}' when option '{OptionNames.GetHelpText(OptionNames.Ask)}' has value '{helpValue2}'.");

                    return(false);
                }

                contentDisplayStyle = contentDisplayStyle2.Value;
            }
            else if (Input.Any())
            {
                contentDisplayStyle = ContentDisplayStyle.AllLines;
            }
            else
            {
                contentDisplayStyle = ContentDisplayStyle.Line;
            }

            pathDisplayStyle = pathDisplayStyle2 ?? PathDisplayStyle.Full;

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

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

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

            options.HighlightOptions = highlightOptions;
            options.ContentFilter    = contentFilter;
            options.ReplaceOptions   = replaceOptions;
            options.Input            = input;
            options.DryRun           = DryRun;
            options.MaxMatchesInFile = maxMatchesInFile;
            options.MaxMatchingFiles = maxMatchingFiles;
            options.MaxTotalMatches  = 0;
            options.Interactive      = Interactive;
#if DEBUG // --find
            if (Find)
            {
                options.ReplaceOptions   = ReplaceOptions.Empty;
                options.HighlightOptions = HighlightOptions.Match;
                options.DryRun           = true;
            }
#endif
            return(true);
        }