Ejemplo n.º 1
0
        private AppliedOption TryTakeOptionOrCommand(Token token)
        {
            var childOption = appliedOptions
                              .SingleOrDefault(o =>
                                               o.Option.DefinedOptions
                                               .Any(oo => oo.RawAliases.Contains(token.Value)));

            if (childOption != null)
            {
                return(childOption.TryTakeToken(token));
            }

            if (token.Type == TokenType.Command &&
                appliedOptions.Any(o => o.Option.IsCommand && !o.HasAlias(token.Value)))
            {
                // if a subcommand has already been applied, don't accept this one
                return(null);
            }

            var applied =
                appliedOptions.SingleOrDefault(o => o.Option.HasRawAlias(token.Value));

            if (applied != null)
            {
                applied.OptionWasRespecified();
                return(applied);
            }

            applied =
                Option.DefinedOptions
                .Where(o => o.RawAliases.Contains(token.Value))
                .Select(o => new AppliedOption(o, token.Value))
                .SingleOrDefault();

            if (applied != null)
            {
                appliedOptions.Add(applied);
            }

            return(applied);
        }
Ejemplo n.º 2
0
        private AppliedOption TryTakeOptionOrCommand(Token token)
        {
            var childOption = appliedOptions
                              .SingleOrDefault(o =>
                                               o.Option.DefinedOptions
                                               .Any(oo => oo.RawAliases.Contains(token.Value)));

            if (childOption != null)
            {
                return(childOption.TryTakeToken(token));
            }

            if (token.Type == TokenType.Command &&
                appliedOptions.Any(o => o.Option.IsCommand && !o.HasAlias(token.Value)))
            {
                return(null);
            }

            var applied =
                appliedOptions.SingleOrDefault(o => o.HasAlias(token.Value));

            if (applied != null)
            {
                return(applied);
            }

            applied =
                Option.DefinedOptions
                .Where(o => o.RawAliases.Contains(token.Value))
                .Select(o => new AppliedOption(o, token.Value))
                .SingleOrDefault();

            if (applied != null)
            {
                appliedOptions.Add(applied);
            }

            return(applied);
        }
Ejemplo n.º 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));
        }