Exemplo n.º 1
0
        public bool TryParse(MatchCommandOptions options)
        {
            var baseOptions = (RegexCommandOptions)options;

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

            options = (MatchCommandOptions)baseOptions;

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

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

            options.Filter           = filter !;
            options.HighlightOptions = highlightOptions;
            options.MaxCount         = MaxCount;

            return(true);
        }
Exemplo n.º 2
0
        internal static bool TryParseProperties(string ask, IEnumerable <string> name, CommonFindCommandOptions 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, out FileNamePart namePart, allowNull: true))
            {
                return(false);
            }

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

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

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

            options = (CommonCopyCommandOptions)baseOptions;

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

            options.NameFilter = nameFilter;
            options.NamePart   = namePart;

            return(true);
        }
Exemplo n.º 4
0
        public bool TryParse(ListPatternsCommandOptions options)
        {
            char?value = null;

            if (Value != null)
            {
                if (!TryParseChar(Value, out char ch))
                {
                    return(false);
                }

                value = ch;
            }

            if (!TryParseAsEnumFlags(
                    Options,
                    OptionNames.Options,
                    out RegexOptions regexOptions,
                    provider: OptionValueProviders.RegexOptionsProvider))
            {
                return(false);
            }

            if (!TryParseAsEnumValues(
                    Section,
                    OptionNames.Section,
                    out ImmutableArray <SyntaxSection> sections,
                    provider: OptionValueProviders.SyntaxSectionProvider))
            {
                return(false);
            }

            if (!FilterParser.TryParse(
                    Filter,
                    OptionNames.Filter,
                    OptionValueProviders.PatternOptions_List_Provider,
                    out Filter? filter,
                    includedPatternOptions: PatternOptions.IgnoreCase,
                    allowNull: true))
            {
                return(false);
            }

            options.Filter       = filter;
            options.Value        = value;
            options.RegexOptions = regexOptions;
            options.InCharGroup  = CharGroup;
            options.Sections     = sections;

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

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

            options = (CommonCopyCommandOptions)baseOptions;

            if (!FilterParser.TryParse(
                    Name,
                    OptionNames.Name,
                    OptionValueProviders.PatternOptionsProvider,
                    out Filter? nameFilter,
                    out FileNamePart namePart,
                    allowNull: true))
            {
                return(false);
            }
#if DEBUG
            if (!TryParseAsEnumFlags(
                    IgnoredAttributes,
                    OptionNames.IgnoredAttributes,
                    out FileSystemAttributes noCompareAttributes,
                    provider: OptionValueProviders.FileSystemAttributesToSkipProvider))
            {
                return(false);
            }

            TimeSpan allowedTimeDiff = TimeSpan.Zero;

            if (AllowedTimeDiff != null &&
                !TimeSpan.TryParse(AllowedTimeDiff, CultureInfo.InvariantCulture, out allowedTimeDiff))
            {
                Logger.WriteError($"Option '{OptionNames.GetHelpText(OptionNames.AllowedTimeDiff)}' "
                                  + $"has invalid value '{AllowedTimeDiff}'.");

                return(false);
            }

            options.AllowedTimeDiff     = allowedTimeDiff;
            options.NoCompareAttributes = GetFileAttributes(noCompareAttributes);
#endif
            options.NameFilter = nameFilter;
            options.NamePart   = namePart;

            return(true);
        }
Exemplo n.º 6
0
        public bool TryParse(SpellcheckCommandOptions options)
        {
            var baseOptions = (CommonReplaceCommandOptions)options;

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

            options = (SpellcheckCommandOptions)baseOptions;

            Regex?wordRegex = null;

            if (!TryEnsureFullPath(Words, out ImmutableArray <string> wordListPaths))
            {
                return(false);
            }
#if DEBUG
            if (Word.Any())
            {
                if (!FilterParser.TryParse(
                        Word,
                        OptionNames.Word,
                        OptionValueProviders.PatternOptions_Word_Provider,
                        out Filter? wordFilter))
                {
                    return(false);
                }

                wordRegex = wordFilter !.Regex;
            }
#endif
            WordListLoaderResult result = WordListLoader.Load(
                wordListPaths,
                minWordLength: MinWordLength,
                (CaseSensitive) ? WordListLoadOptions.None : WordListLoadOptions.IgnoreCase);

            var data = new SpellingData(result.List, result.CaseSensitiveList, result.FixList);

            var spellcheckerOptions = new SpellcheckerOptions(
                SplitMode.CaseAndHyphen,
                MinWordLength);

            var spellchecker = new Spellchecker(data, wordRegex, spellcheckerOptions);

            options.Replacer = new SpellcheckState(spellchecker, data);

            return(true);
        }
Exemplo n.º 7
0
        public bool TryParse(HelpCommandOptions options)
        {
            if (!FilterParser.TryParse(
                    Filter,
                    OptionNames.Filter,
                    OptionValueProviders.PatternOptionsWithoutPartAndNegativeProvider,
                    out Filter? filter,
                    allowNull: true,
                    includedPatternOptions: PatternOptions.IgnoreCase))
            {
                return(false);
            }

            options.Command = Command;
            options.Filter  = filter;
            options.Manual  = Manual;

            return(true);
        }
Exemplo n.º 8
0
        public bool TryParse(HelpCommandOptions options)
        {
            if (!FilterParser.TryParse(
                    Filter,
                    OptionNames.Filter,
                    OptionValueProviders.PatternOptions_List_Provider,
                    out Filter? filter,
                    includedPatternOptions: PatternOptions.IgnoreCase,
                    allowNull: true))
            {
                return(false);
            }

            options.Command = Command;
            options.Filter  = filter;
            options.Manual  = Manual;
            options.Online  = Online;

            return(true);
        }
Exemplo n.º 9
0
        public bool TryParse(SplitCommandOptions options)
        {
            var baseOptions = (RegexCommandOptions)options;

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

            options = (SplitCommandOptions)baseOptions;

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

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

            options.Filter           = filter !;
            options.HighlightOptions = highlightOptions;
            options.OmitGroups       = NoGroups;
            options.MaxCount         = MaxCount;

            return(true);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public bool TryParse(RenameCommandOptions options)
        {
            var baseOptions = (DeleteOrRenameCommandOptions)options;

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

            options = (RenameCommandOptions)baseOptions;

            if (!TryParseAsEnumFlags(Highlight, OptionNames.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))
            {
                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 (!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? _,
                    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,
                    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.None,
                pathDisplayStyle: pathDisplayStyle ?? PathDisplayStyle.Full,
                lineOptions: lineDisplayOptions,
                lineContext: lineContext,
                displayParts: displayParts,
                fileProperties: fileProperties,
                indent: indent,
                separator: separator);

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

            return(true);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
        public bool TryParse(DeleteCommandOptions options)
        {
            var baseOptions = (CommonFindCommandOptions)options;

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

            options = (DeleteCommandOptions)baseOptions;

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

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

            if (nameFilter == null &&
                options.Paths.Length == 1 &&
                options.Paths[0].Origin == PathOrigin.CurrentDirectory)
            {
                Logger.WriteError($"Option '{OptionNames.GetHelpText(OptionNames.Name)}' is required when no path is specified (i.e. current directory is used).");
                return(false);
            }

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

            if (!TryParseDisplay(
                    values: Display,
                    optionName: OptionNames.Display,
                    contentDisplayStyle: out 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;
            }

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

            options.Ask              = Ask;
            options.DryRun           = DryRun;
            options.HighlightOptions = highlightOptions;
            options.SearchTarget     = GetSearchTarget();
            options.NameFilter       = nameFilter;
            options.ContentFilter    = contentFilter;
            options.ContentOnly      = ContentOnly;
            options.IncludingBom     = IncludingBom;
            options.MaxMatchingFiles = MaxCount;

            return(true);
        }
        public bool TryParse(FileSystemCommandOptions options)
        {
            var baseOptions = (CommonRegexCommandOptions)options;

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

            options = (FileSystemCommandOptions)baseOptions;

            if (!TryParsePaths(out ImmutableArray <PathInfo> paths))
            {
                return(false);
            }

            if (!TryParseAsEnumFlags(Attributes, OptionNames.Attributes, out FileSystemAttributes attributes, provider: OptionValueProviders.FileSystemAttributesProvider))
            {
                return(false);
            }

            if (!TryParseAsEnumFlags(AttributesToSkip, OptionNames.AttributesToSkip, out FileSystemAttributes attributesToSkip, provider: OptionValueProviders.FileSystemAttributesToSkipProvider))
            {
                return(false);
            }

            if (!TryParseEncoding(Encoding, out Encoding defaultEncoding, EncodingHelpers.UTF8NoBom))
            {
                return(false);
            }

            if (!TryParseSortOptions(Sort, OptionNames.Sort, out SortOptions sortOptions))
            {
                return(false);
            }

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

            if (!FilterParser.TryParse(
                    Extension,
                    OptionNames.Extension,
                    OptionValueProviders.ExtensionOptionsProvider,
                    out Filter extensionFilter,
                    allowNull: true,
                    defaultNamePart: NamePartKind.Extension,
                    includedPatternOptions: PatternOptions.List | PatternOptions.Equals | PatternOptions.IgnoreCase))
            {
                return(false);
            }

            if (!TryParseFileProperties(
                    FileProperties,
                    OptionNames.Properties,
                    out FilePropertyFilter filePropertyFilter))
            {
                return(false);
            }

            if ((attributes & FileSystemAttributes.Empty) != 0)
            {
                if ((attributesToSkip & FileSystemAttributes.Empty) != 0)
                {
                    Logger.WriteError($"Value '{OptionValueProviders.FileSystemAttributesProvider.GetValue(nameof(FileSystemAttributes.Empty)).HelpValue}' cannot be specified both for '{OptionNames.GetHelpText(OptionNames.Attributes)}' and '{OptionNames.GetHelpText(OptionNames.AttributesToSkip)}'.");
                    return(false);
                }

                options.Empty = true;
            }
            else if ((attributesToSkip & FileSystemAttributes.Empty) != 0)
            {
                options.Empty = false;
            }

            options.Paths                 = paths;
            options.DirectoryFilter       = directoryFilter;
            options.ExtensionFilter       = extensionFilter;
            options.Attributes            = GetFileAttributes(attributes);
            options.AttributesToSkip      = GetFileAttributes(attributesToSkip);
            options.RecurseSubdirectories = !NoRecurse;
            options.Progress              = Progress;
            options.DefaultEncoding       = defaultEncoding;
            options.SortOptions           = sortOptions;
            options.FilePropertyFilter    = filePropertyFilter;

            FileSystemAttributes = attributes;

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

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

            options = (CommonFindCommandOptions)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,
                    lineContext: out LineContext lineContext,
                    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.Value.ToString()).HelpValue}' when option '{OptionNames.GetHelpText(OptionNames.Ask)}' has value '{OptionValueProviders.AskModeProvider.GetValue(nameof(AskMode.Value)).HelpValue}'.");
                return(false);
            }

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

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

            options.HighlightOptions = highlightOptions;
            options.SearchTarget     = GetSearchTarget();
            options.ContentFilter    = contentFilter;
            options.MaxMatchesInFile = maxMatchesInFile;
            options.MaxMatchingFiles = maxMatchingFiles;
            options.MaxTotalMatches  = 0;

            return(true);
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        public bool TryParse(FileSystemCommandOptions options)
        {
            var baseOptions = (CommonRegexCommandOptions)options;

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

            options = (FileSystemCommandOptions)baseOptions;

            if (!TryParsePaths(out ImmutableArray <PathInfo> paths))
            {
                return(false);
            }

            if (!TryParseAsEnumFlags(Attributes, OptionNames.Attributes, out FileSystemAttributes attributes, provider: OptionValueProviders.FileSystemAttributesProvider))
            {
                return(false);
            }

            if (!TryParseAsEnumFlags(AttributesToSkip, OptionNames.AttributesToSkip, out FileSystemAttributes attributesToSkip, provider: OptionValueProviders.FileSystemAttributesToSkipProvider))
            {
                return(false);
            }

            if (!TryParseEncoding(Encoding, out Encoding defaultEncoding, Text.EncodingHelpers.UTF8NoBom))
            {
                return(false);
            }

            if (!TryParseSortOptions(Sort, OptionNames.Sort, out SortOptions? sortOptions))
            {
                return(false);
            }

            if (!FilterParser.TryParse(IncludeDirectory, OptionNames.IncludeDirectory, OptionValueProviders.PatternOptionsProvider, out Filter? directoryFilter, out FileNamePart directoryNamePart, allowNull: true, namePartProvider: OptionValueProviders.NamePartKindProvider_WithoutExtension))
            {
                return(false);
            }

            if (!FilterParser.TryParse(
                    Extension,
                    OptionNames.Extension,
                    OptionValueProviders.ExtensionOptionsProvider,
                    out Filter? extensionFilter,
                    allowNull: true,
                    defaultNamePart: FileNamePart.Extension,
                    includedPatternOptions: PatternOptions.List | PatternOptions.Equals | PatternOptions.IgnoreCase))
            {
                return(false);
            }

            if (!TryParseFileProperties(
                    FileProperties,
                    OptionNames.Properties,
                    out FilterPredicate <DateTime>?creationTimePredicate,
                    out FilterPredicate <DateTime>?modifiedTimePredicate,
                    out FilterPredicate <long>?sizePredicate))
            {
                return(false);
            }

            if ((attributes & FileSystemAttributes.Empty) != 0)
            {
                if ((attributesToSkip & FileSystemAttributes.Empty) != 0)
                {
                    Logger.WriteError($"Value '{OptionValueProviders.FileSystemAttributesProvider.GetValue(nameof(FileSystemAttributes.Empty)).HelpValue}' cannot be specified both for '{OptionNames.GetHelpText(OptionNames.Attributes)}' and '{OptionNames.GetHelpText(OptionNames.AttributesToSkip)}'.");
                    return(false);
                }

                options.EmptyOption = FileEmptyOption.Empty;
            }
            else if ((attributesToSkip & FileSystemAttributes.Empty) != 0)
            {
                options.EmptyOption = FileEmptyOption.NonEmpty;
            }

            options.Paths                 = paths;
            options.DirectoryFilter       = directoryFilter;
            options.DirectoryNamePart     = directoryNamePart;
            options.ExtensionFilter       = extensionFilter;
            options.Attributes            = GetFileAttributes(attributes);
            options.AttributesToSkip      = GetFileAttributes(attributesToSkip);
            options.RecurseSubdirectories = !NoRecurse;
            options.Progress              = Progress;
            options.DefaultEncoding       = defaultEncoding;
            options.SortOptions           = sortOptions;
            options.CreationTimePredicate = creationTimePredicate;
            options.ModifiedTimePredicate = modifiedTimePredicate;
            options.SizePredicate         = sizePredicate;

            if (creationTimePredicate != null ||
                modifiedTimePredicate != null ||
                sizePredicate != null)
            {
                options.FilePropertyFilter = new FilePropertyFilter(
                    creationTimePredicate: creationTimePredicate?.Predicate,
                    modifiedTimePredicate: modifiedTimePredicate?.Predicate,
                    sizePredicate: sizePredicate?.Predicate);
            }

            FileSystemAttributes = attributes;

            return(true);
        }