Exemple #1
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);
        }
        public bool TryParse(RegexCommandOptions options)
        {
            var baseOptions = (CommonRegexCommandOptions)options;

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

            options = (RegexCommandOptions)baseOptions;

            string input = Input.FirstOrDefault();

            if (!string.IsNullOrEmpty(Path))
            {
                if (input != null)
                {
                    WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Input)}' and "
                               + $"argument '{ArgumentMetaNames.Path}' cannot be set both at the same time.");

                    return(false);
                }

                try
                {
                    input = File.ReadAllText(Path);
                }
                catch (Exception ex) when(ex is ArgumentException ||
                                          ex is IOException ||
                                          ex is UnauthorizedAccessException)
                {
                    WriteError(ex);
                    return(false);
                }
            }
            else if (string.IsNullOrEmpty(input))
            {
                string?redirectedInput = ConsoleHelpers.ReadRedirectedInput();

                if (redirectedInput == null)
                {
                    WriteError("Input is missing.");
                    return(false);
                }

                input = redirectedInput;
            }
            else if (!TryParseInput(Input, out input))
            {
                return(false);
            }

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

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

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

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

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

            options.ModifyOptions = modifyOptions;
            options.Input         = input;

            return(true);
        }