Пример #1
0
        internal ParseResult(
            IReadOnlyCollection <string> tokens,
            AppliedOptionSet appliedOptions,
            bool isProgressive,
            ParserConfiguration configuration,
            IReadOnlyCollection <string> unparsedTokens  = null,
            IReadOnlyCollection <string> unmatchedTokens = null,
            IReadOnlyCollection <OptionError> errors     = null)
        {
            Tokens = tokens ??
                     throw new ArgumentNullException(nameof(tokens));
            AppliedOptions = appliedOptions ??
                             throw new ArgumentNullException(nameof(appliedOptions));
            this.configuration = configuration ??
                                 throw new ArgumentNullException(nameof(configuration));

            IsProgressive   = isProgressive;
            UnparsedTokens  = unparsedTokens;
            UnmatchedTokens = unmatchedTokens;

            if (errors != null)
            {
                this.errors.AddRange(errors);
            }

            CheckForErrors();
        }
Пример #2
0
 internal static Command Command(this AppliedOptionSet options) =>
 options.FlattenBreadthFirst()
 .Select(a => a.Option)
 .OfType <Command>()
 .LastOrDefault();
Пример #3
0
        internal ParseResult Parse(
            IReadOnlyCollection <string> rawArgs,
            bool isProgressive)
        {
            var unparsedTokens = new Queue <Token>(
                NormalizeRootCommand(rawArgs)
                .Lex(configuration));
            var rootAppliedOptions = new AppliedOptionSet();
            var allAppliedOptions  = new List <AppliedOption>();
            var errors             = new List <OptionError>();
            var unmatchedTokens    = new List <string>();

            while (unparsedTokens.Any())
            {
                var token = unparsedTokens.Dequeue();

                if (token.Value == "--")
                {
                    // stop parsing further tokens
                    break;
                }

                if (token.Type != TokenType.Argument)
                {
                    var definedOption =
                        DefinedOptions.SingleOrDefault(o => o.HasAlias(token.Value));

                    if (definedOption != null)
                    {
                        var appliedOption = allAppliedOptions
                                            .LastOrDefault(o => o.HasAlias(token.Value));

                        if (appliedOption == null)
                        {
                            appliedOption = new AppliedOption(definedOption, token.Value);
                            rootAppliedOptions.Add(appliedOption);
                        }
                        else
                        {
                            appliedOption.OptionWasRespecified();
                        }

                        allAppliedOptions.Add(appliedOption);

                        continue;
                    }
                }

                var added = false;

                foreach (var appliedOption in Enumerable.Reverse(allAppliedOptions))
                {
                    var option = appliedOption.TryTakeToken(token);

                    if (option != null)
                    {
                        allAppliedOptions.Add(option);
                        added = true;
                        break;
                    }
                }

                if (!added)
                {
                    unmatchedTokens.Add(token.Value);
                }
            }

            if (rootAppliedOptions.Command()?.TreatUnmatchedTokensAsErrors == true)
            {
                errors.AddRange(
                    unmatchedTokens.Select(UnrecognizedArg));
            }

            return(new ParseResult(
                       rawArgs,
                       rootAppliedOptions,
                       isProgressive,
                       unparsedTokens.Select(t => t.Value).ToArray(),
                       unmatchedTokens,
                       errors));
        }