Exemplo n.º 1
0
 internal static void WriteFindCommand(FindCommandOptions 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("empty", options.EmptyOption);
     WriteFilter("extension filter", options.ExtensionFilter);
     WriteFilePropertyFilter(
         "file properties",
         options.SizePredicate,
         options.CreationTimePredicate,
         options.ModifiedTimePredicate);
     WriteOption("highlight options", options.HighlightOptions);
     WriteInput(options.Input);
     WriteOption("max matching files", options.MaxMatchingFiles);
     WriteOption("max matches in file", options.MaxMatchesInFile);
     WriteModify("modify", options.ModifyOptions);
     WriteFilter("name filter", options.NameFilter, options.NamePart);
     WritePaths("paths", options.Paths);
     WriteOption("progress", options.Progress);
     WriteOption("recurse subdirectories", options.RecurseSubdirectories);
     WriteOption("search target", options.SearchTarget);
     WriteOption("split", options.Split);
     WriteSortOptions("sort", options.SortOptions);
 }
Exemplo n.º 2
0
        private static int Find(FindCommandLineOptions commandLineOptions)
        {
            var options = new FindCommandOptions();

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

            return(Execute(new FindCommand <FindCommandOptions>(options)));
        }
Exemplo n.º 3
0
        public bool TryParse(FindCommandOptions options)
        {
            var baseOptions = (CommonFindCommandOptions)options;

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

            options = (FindCommandOptions)baseOptions;

            if (!TryParseModifyOptions(Modify, OptionNames.Modify, out ModifyOptions? modifyOptions, out bool aggregateOnly))
            {
                return(false);
            }

            OutputDisplayFormat format = options.Format;
            ContentDisplayStyle contentDisplayStyle = format.ContentDisplayStyle;
            PathDisplayStyle    pathDisplayStyle    = format.PathDisplayStyle;

            if (modifyOptions.HasAnyFunction &&
                contentDisplayStyle == ContentDisplayStyle.ValueDetail)
            {
                contentDisplayStyle = ContentDisplayStyle.Value;
            }

            if (aggregateOnly)
            {
                ConsoleOut.Verbosity = Orang.Verbosity.Minimal;
                pathDisplayStyle     = PathDisplayStyle.Omit;
            }

            options.ModifyOptions = modifyOptions;

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle,
                pathDisplayStyle: pathDisplayStyle,
                lineOptions: format.LineOptions,
                lineContext: format.LineContext,
                displayParts: format.DisplayParts,
                fileProperties: format.FileProperties,
                indent: format.Indent,
                separator: format.Separator,
                includeBaseDirectory: format.IncludeBaseDirectory);

            return(true);
        }
Exemplo n.º 4
0
 public ModifyManager(FindCommandOptions options)
 {
     Options = options;
 }
Exemplo n.º 5
0
        internal static bool TryParseProperties(string ask, IEnumerable <string> name, FindCommandOptions options)
        {
            if (!TryParseAsEnum(ask, OptionNames.Ask, out AskMode askMode, defaultValue: AskMode.None, OptionValueProviders.AskModeProvider))
            {
                return(false);
            }

            if (askMode == AskMode.Value &&
                ConsoleOut.Verbosity < Verbosity.Normal)
            {
                WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Ask)}' cannot have value '{OptionValueProviders.AskModeProvider.GetValue(nameof(AskMode.Value)).HelpValue}' when '{OptionNames.GetHelpText(OptionNames.Verbosity)}' is set to '{OptionValueProviders.VerbosityProvider.GetValue(ConsoleOut.Verbosity.ToString()).HelpValue}'.");
                return(false);
            }

            if (!FilterParser.TryParse(name, OptionNames.Name, OptionValueProviders.PatternOptionsProvider, out Filter nameFilter, allowNull: true))
            {
                return(false);
            }

            options.AskMode    = askMode;
            options.NameFilter = nameFilter;

            return(true);
        }
Exemplo n.º 6
0
        public bool TryParse(FindCommandOptions 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;
            }

            var baseOptions = (CommonFindCommandOptions)options;

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

            options = (FindCommandOptions)baseOptions;

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

            string?input = null;

            if (pipeMode != PipeMode.Paths &&
                Console.IsInputRedirected)
            {
                if (options.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();
            }

            EnumerableModifier <string>?modifier = null;

#if DEBUG // --modifier
            if (Modifier.Any() &&
                !TryParseModifier(Modifier, OptionNames.Modifier, out modifier))
            {
                return(false);
            }
#endif
            if (!TryParseModifyOptions(
                    Modify,
                    OptionNames.Modify,
                    modifier,
                    out ModifyOptions? modifyOptions,
                    out bool aggregateOnly))
            {
                return(false);
            }

            OutputDisplayFormat format = options.Format;
            ContentDisplayStyle contentDisplayStyle = format.ContentDisplayStyle;
            PathDisplayStyle    pathDisplayStyle    = format.PathDisplayStyle;

            if (modifyOptions.HasAnyFunction &&
                contentDisplayStyle == ContentDisplayStyle.ValueDetail)
            {
                contentDisplayStyle = ContentDisplayStyle.Value;
            }

            options.Input         = input;
            options.ModifyOptions = modifyOptions;
            options.AggregateOnly = aggregateOnly;
            options.Split         = Split;

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle,
                pathDisplayStyle: pathDisplayStyle,
                lineOptions: format.LineOptions,
                lineContext: format.LineContext,
                displayParts: format.DisplayParts,
                fileProperties: format.FileProperties,
                indent: format.Indent,
                separator: format.Separator,
                alignColumns: format.AlignColumns,
                includeBaseDirectory: format.IncludeBaseDirectory);

            return(true);
        }
Exemplo n.º 7
0
        public bool TryParse(FindCommandOptions options)
        {
            var baseOptions = (CommonFindCommandOptions)options;

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

            options = (FindCommandOptions)baseOptions;

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

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

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

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

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

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

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

            int maxMatchesInFile;

            if (contentFilter != null)
            {
                maxMatchesInFile = maxCount;
            }
            else
            {
                maxMatchesInFile = 0;
                maxMatches       = 0;
                maxMatchingFiles = (maxCount > 0) ? maxCount : maxMatchingFiles;
            }

            if (!TryParseModifyOptions(Modify, OptionNames.Modify, out ModifyOptions modifyOptions, out bool aggregateOnly))
            {
                return(false);
            }

            if (modifyOptions.HasAnyFunction &&
                contentDisplayStyle == ContentDisplayStyle.ValueDetail)
            {
                contentDisplayStyle = ContentDisplayStyle.Value;
            }

            if (aggregateOnly)
            {
                ConsoleOut.Verbosity = Orang.Verbosity.Minimal;
                pathDisplayStyle     = PathDisplayStyle.Omit;
            }

            options.Format = new OutputDisplayFormat(
                contentDisplayStyle: contentDisplayStyle ?? ContentDisplayStyle.Line,
                pathDisplayStyle: pathDisplayStyle ?? PathDisplayStyle.Full,
                lineOptions: lineDisplayOptions,
                displayParts: displayParts,
                fileProperties: fileProperties,
                indent: indent,
                separator: separator);

            options.ModifyOptions    = modifyOptions;
            options.HighlightOptions = highlightOptions;
            options.SearchTarget     = GetSearchTarget();
            options.ContentFilter    = contentFilter;
            options.MaxMatchesInFile = maxMatchesInFile;
            options.MaxMatches       = maxMatches;
            options.MaxMatchingFiles = maxMatchingFiles;

            return(true);
        }