internal ParseResult(
            Parser parser,
            RootCommandResult rootCommandResult,
            CommandResult commandResult,
            IDirectiveCollection directives,
            TokenizeResult tokenizeResult,
            IReadOnlyCollection <string> unparsedTokens,
            IReadOnlyCollection <string> unmatchedTokens,
            List <ParseError> errors = null,
            string rawInput          = null)
        {
            Parser             = parser;
            _rootCommandResult = rootCommandResult;
            CommandResult      = commandResult;
            Directives         = directives;

            // skip the root command
            Tokens = tokenizeResult.Tokens.Skip(1).ToArray();

            UnparsedTokens  = unparsedTokens;
            UnmatchedTokens = unmatchedTokens;

            RawInput = rawInput;

            _errors = errors ?? new List <ParseError>();

            if (parser.Configuration.RootCommand.TreatUnmatchedTokensAsErrors)
            {
                _errors.AddRange(
                    unmatchedTokens.Select(token =>
                                           new ParseError(parser.Configuration.ValidationMessages.UnrecognizedCommandOrArgument(token))));
            }
        }
        private void AddImplicitOptionsAndCheckForErrors()
        {
            foreach (var result in RootCommandResult.AllSymbolResults().ToArray())
            {
                if (result is CommandResult command)
                {
                    foreach (var symbol in command.Command.Children)
                    {
                        if (symbol.Argument.HasDefaultValue &&
                            command.Children[symbol.Name] == null)
                        {
                            switch (symbol)
                            {
                            case IOption option:
                                command.AddImplicitOption(option);
                                break;
                            }
                        }
                    }

                    if (!command.IsArgumentLimitReached &&
                        command.Command.Argument.HasDefaultValue)
                    {
                        var defaultValue = command.Command.Argument.GetDefaultValue();

                        if (defaultValue is string stringArg)
                        {
                            command.TryTakeToken(new Token(stringArg, TokenType.Argument));
                        }
                        else
                        {
                            command.UseDefaultValue = true;
                        }
                    }
                }

                var error = result.Validate();

                if (error != null)
                {
                    _errors.Add(error);
                }
            }

            if (CommandResult.Command is Command cmd &&
                cmd.Handler == null &&
                cmd.Children.OfType <ICommand>().Any())
            {
                _errors.Insert(0,
                               new ParseError(
                                   CommandResult.ValidationMessages.RequiredCommandWasNotProvided(),
                                   CommandResult));
            }
        }
        internal ParseResult(
            Parser parser,
            RootCommandResult rootCommandResult,
            CommandResult commandResult,
            DirectiveCollection directives,
            TokenizeResult tokenizeResult,
            IReadOnlyList <Token>?unmatchedTokens,
            List <ParseError>?errors,
            string?commandLineText = null)
        {
            Parser             = parser;
            _rootCommandResult = rootCommandResult;
            CommandResult      = commandResult;
            Directives         = directives;

            // skip the root command when populating Tokens property
            if (tokenizeResult.Tokens.Count > 1)
            {
                var tokens = new Token[tokenizeResult.Tokens.Count - 1];
                for (var i = 0; i < tokenizeResult.Tokens.Count - 1; i++)
                {
                    var token = tokenizeResult.Tokens[i + 1];
                    tokens[i] = token;
                }

                Tokens = tokens;
            }
            else
            {
                Tokens = Array.Empty <Token>();
            }

            _errors         = errors ?? new List <ParseError>();
            CommandLineText = commandLineText;

            if (unmatchedTokens is null)
            {
                _unmatchedTokens = Array.Empty <Token>();
            }
            else
            {
                _unmatchedTokens = unmatchedTokens;

                if (parser.Configuration.RootCommand.TreatUnmatchedTokensAsErrors)
                {
                    for (var i = 0; i < _unmatchedTokens.Count; i++)
                    {
                        var token = _unmatchedTokens[i];
                        _errors.Add(new ParseError(parser.Configuration.LocalizationResources.UnrecognizedCommandOrArgument(token.Value), rootCommandResult));
                    }
                }
            }
        }
Exemple #4
0
 public SymbolResult FindResultFor(ISymbol symbol) =>
 RootCommandResult.AllSymbolResults()
 .FirstOrDefault(s => s.Symbol == symbol);