Example #1
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);
        }
        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);
        }