コード例 #1
0
 public NameFilter(
     Filter name,
     FileNamePart part = FileNamePart.Name)
 {
     Name = name ?? throw new ArgumentNullException(nameof(name));
     Part = part;
 }
コード例 #2
0
        public static FileNameSpan FromDirectory(string path, FileNamePart part)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            switch (part)
            {
            case FileNamePart.Name:
            case FileNamePart.NameWithoutExtension:
            {
                int index = GetFileNameIndex(path);

                return(new FileNameSpan(path, index, path.Length - index, part));
            }

            case FileNamePart.FullName:
            {
                return(new FileNameSpan(path, 0, path.Length, part));
            }

            //case NamePartKind.Extension:
            default:
            {
                throw new ArgumentException("", nameof(part));
            }
            }
        }
コード例 #3
0
        public static FileNameSpan FromFile(string path, FileNamePart part)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            switch (part)
            {
            case FileNamePart.Name:
            {
                int index = GetFileNameIndex(path);

                return(new FileNameSpan(path, index, path.Length - index, part));
            }

            case FileNamePart.FullName:
            {
                return(new FileNameSpan(path, 0, path.Length, part));
            }

            case FileNamePart.NameWithoutExtension:
            {
                int index = GetFileNameIndex(path);

                int dotIndex = GetExtensionIndex(path);

                return(new FileNameSpan(path, index, dotIndex - index, part));
            }

            case FileNamePart.Extension:
            {
                int dotIndex = GetExtensionIndex(path);

                if (dotIndex >= path.Length - 1)
                {
                    return(new FileNameSpan(path, path.Length, 0, part));
                }

                return(new FileNameSpan(path, dotIndex + 1, path.Length - dotIndex - 1, part));
            }

            default:
            {
                throw new ArgumentException("", nameof(part));
            }
            }
        }
コード例 #4
0
        internal FileNameSpan(string path, int start, int length, FileNamePart part)
        {
            Path = path ?? throw new ArgumentNullException(nameof(path));

            if (start < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(start), start, null);
            }

            if (start + length > path.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(length), length, null);
            }

            Start  = start;
            Length = length;
            Part   = part;
        }
コード例 #5
0
 public FileSystemFilter(
     Filter?name                     = null,
     FileNamePart part               = FileNamePart.Name,
     Filter?extension                = null,
     Filter?content                  = null,
     FilePropertyFilter?properties   = null,
     FileAttributes attributes       = default,
     FileAttributes attributesToSkip = default,
     FileEmptyOption emptyOption     = FileEmptyOption.None)
 {
     Name             = name;
     Part             = part;
     Extension        = extension;
     Content          = content;
     Properties       = properties;
     Attributes       = attributes;
     AttributesToSkip = attributesToSkip;
     EmptyOption      = emptyOption;
 }
コード例 #6
0
 public static bool TryParse(
     IEnumerable <string> values,
     string optionName,
     OptionValueProvider provider,
     out Filter?filter,
     bool allowNull = false,
     OptionValueProvider?namePartProvider  = null,
     FileNamePart defaultNamePart          = FileNamePart.Name,
     PatternOptions includedPatternOptions = PatternOptions.None)
 {
     return(TryParse(
                values: values,
                optionName: optionName,
                provider: provider,
                filter: out filter,
                namePart: out _,
                allowNull: allowNull,
                namePartProvider: namePartProvider,
                defaultNamePart: defaultNamePart,
                includedPatternOptions: includedPatternOptions));
 }
コード例 #7
0
        public static bool TryParse(
            IEnumerable <string> values,
            string optionName,
            OptionValueProvider provider,
            out Filter?filter,
            out FileNamePart namePart,
            bool allowNull = false,
            OptionValueProvider?namePartProvider  = null,
            FileNamePart defaultNamePart          = FileNamePart.Name,
            PatternOptions includedPatternOptions = PatternOptions.None)
        {
            filter   = null;
            namePart = defaultNamePart;

            string?pattern = values.FirstOrDefault();

            if (pattern == null)
            {
                if (allowNull)
                {
                    return(true);
                }
                else
                {
                    throw new InvalidOperationException($"Option '{OptionNames.GetHelpText(optionName)}' is required.");
                }
            }

            TimeSpan            matchTimeout = Regex.InfiniteMatchTimeout;
            string?             groupName    = null;
            string?             separator    = null;
            Func <string, bool>?predicate    = null;

            List <string>?options = null;

            foreach (string option in values.Skip(1))
            {
                int index = option.IndexOfAny(_equalsOrLessThanOrGreaterThanChars);

                if (index != -1)
                {
                    string key = option.Substring(0, index);

                    if (!provider.ContainsKeyOrShortKey(key))
                    {
                        WriteOptionError(option, optionName, provider);
                        return(false);
                    }

                    string value = option.Substring(index + 1);

                    if (OptionValues.Group.IsKeyOrShortKey(key))
                    {
                        groupName = value;
                        continue;
                    }
                    else if (OptionValues.ListSeparator.IsKeyOrShortKey(key))
                    {
                        separator = value;
                        continue;
                    }
                    else if (OptionValues.Part.IsKeyOrShortKey(key))
                    {
                        if (!TryParseAsEnum(
                                value,
                                out namePart,
                                provider: namePartProvider ?? OptionValueProviders.NamePartKindProvider))
                        {
                            WriteOptionValueError(
                                value,
                                OptionValues.Part,
                                namePartProvider ?? OptionValueProviders.NamePartKindProvider);
                            return(false);
                        }

                        continue;
                    }
                    else if (OptionValues.Timeout.IsKeyOrShortKey(key))
                    {
                        if (!TryParseMatchTimeout(value, out matchTimeout))
                        {
                            WriteOptionValueError(value, OptionValues.Timeout);
                            return(false);
                        }

                        continue;
                    }
                }

                if (Expression.TryParse(option, out Expression? expression))
                {
                    if (OptionValues.Length.IsKeyOrShortKey(expression.Identifier) &&
                        provider.ContainsKeyOrShortKey(expression.Identifier))
                    {
                        try
                        {
                            predicate = PredicateHelpers.GetLengthPredicate(expression);
                            continue;
                        }
                        catch (ArgumentException)
                        {
                            WriteOptionValueError(
                                expression.Value,
                                OptionValues.Length,
                                HelpProvider.GetExpressionsText("  ", includeDate: false));
                            return(false);
                        }
                    }
                    else
                    {
                        WriteOptionError(option, optionName, provider);
                        return(false);
                    }
                }

                (options ??= new List <string>()).Add(option);
            }

            if (!TryParseRegexOptions(
                    options,
                    optionName,
                    out RegexOptions regexOptions,
                    out PatternOptions patternOptions,
                    includedPatternOptions,
                    provider))
            {
                return(false);
            }

            switch (patternOptions & (PatternOptions.WholeWord | PatternOptions.WholeLine))
            {
            case PatternOptions.None:
            case PatternOptions.WholeWord:
            case PatternOptions.WholeLine:
            {
                break;
            }

            default:
            {
                string helpValue = OptionValueProviders.PatternOptionsProvider
                                   .GetValue(nameof(PatternOptions.WholeWord)).HelpValue;

                string helpValue2 = OptionValueProviders.PatternOptionsProvider
                                    .GetValue(nameof(PatternOptions.WholeLine)).HelpValue;

                WriteError($"Values '{helpValue}' and '{helpValue2}' cannot be combined.");

                return(false);
            }
            }

            if ((patternOptions & PatternOptions.FromFile) != 0 &&
                !FileSystemHelpers.TryReadAllText(pattern, out pattern, ex => WriteError(ex)))
            {
                return(false);
            }

            pattern = BuildPattern(pattern, patternOptions, separator);

            if (pattern.Length == 0)
            {
                throw new InvalidOperationException(
                          $"Option '{OptionNames.GetHelpText(optionName)}' is invalid: pattern cannot be empty.");
            }

            Regex?regex = null;

            try
            {
                regex = new Regex(pattern, regexOptions, matchTimeout);
            }
            catch (ArgumentException ex)
            {
                WriteError(ex, $"Could not parse regular expression: {ex.Message}");
                return(false);
            }

            int groupIndex = -1;

            if (groupName != null)
            {
                groupIndex = regex.GroupNumberFromName(groupName);
                if (groupIndex == -1)
                {
                    string message = $"Group '{groupName}' does not exist.";

                    string[] groupNames = regex.GetGroupNames();

                    if (groupNames.Length > 1)
                    {
                        message += " Existing group names: "
                                   + $"{TextHelpers.Join(", ", " and ", groupNames.Where(f => f != "0"))}.";
                    }

                    WriteError(message);
                    return(false);
                }
            }

            filter = new Filter(
                regex,
                isNegative: (patternOptions & PatternOptions.Negative) != 0,
                groupNumber: groupIndex,
                predicate: predicate);

            return(true);
        }