Exemplo n.º 1
0
        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))));
            }
        }
        public ParseResultVisitor(
            Parser parser,
            TokenizeResult tokenizeResult,
            IReadOnlyCollection <Token> unparsedTokens,
            IReadOnlyCollection <Token> unmatchedTokens,
            IReadOnlyCollection <ParseError> parseErrors,
            string?rawInput)
        {
            _parser         = parser;
            _tokenizeResult = tokenizeResult;
            _rawInput       = rawInput;

            _unparsedTokens = new List <string>();
            if (unparsedTokens?.Count > 0)
            {
                _unparsedTokens.AddRange(unparsedTokens.Select(t => t.Value));
            }

            _unmatchedTokens = new List <string>();
            if (unmatchedTokens?.Count > 0)
            {
                _unmatchedTokens.AddRange(unmatchedTokens.Select(t => t.Value));
            }

            _errors = new List <ParseError>();
            _errors.AddRange(_tokenizeResult.Errors.Select(t => new ParseError(t.Message)));
            _errors.AddRange(parseErrors);
        }
Exemplo n.º 3
0
 public ParseOperation(
     TokenizeResult tokenizeResult,
     CommandLineConfiguration configuration)
 {
     _tokenizeResult = tokenizeResult;
     _configuration  = configuration;
 }
Exemplo n.º 4
0
        public ParseResultVisitor(
            Parser parser,
            TokenizeResult tokenizeResult,
            List <Token> unparsedTokens,
            List <Token> unmatchedTokens,
            IReadOnlyCollection <ParseError> parseErrors,
            string?rawInput
            )
        {
            _parser          = parser;
            _tokenizeResult  = tokenizeResult;
            _unparsedTokens  = unparsedTokens;
            _unmatchedTokens = unmatchedTokens;
            _rawInput        = rawInput;

            _errors = new List <ParseError>(_tokenizeResult.Errors.Count + parseErrors.Count);

            for (var i = 0; i < _tokenizeResult.Errors.Count; i++)
            {
                var error = _tokenizeResult.Errors[i];
                _errors.Add(new ParseError(error.Message));
            }

            _errors.AddRange(parseErrors);
        }
Exemplo n.º 5
0
        internal ParseResult(
            Parser parser,
            RootCommandResult rootCommandResult,
            CommandResult commandResult,
            IDirectiveCollection directives,
            TokenizeResult tokenizeResult,
            IReadOnlyList <Token>?unparsedTokens,
            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>();
            }

            _unparsedTokens = unparsedTokens ?? 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));
                    }
                }
            }
        }
Exemplo n.º 6
0
        internal ParseResult(
            Parser parser,
            RootCommandResult rootCommandResult,
            CommandResult commandResult,
            IDirectiveCollection directives,
            TokenizeResult tokenizeResult,
            IReadOnlyList <Token> unparsedTokens,
            IReadOnlyList <Token> unmatchedTokens,
            List <ParseError>?errors = null,
            string?rawInput          = 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>();
            }

            _unparsedTokens  = unparsedTokens;
            _unmatchedTokens = unmatchedTokens;

            RawInput = rawInput;

            _errors = errors ?? (parser.Configuration.RootCommand.TreatUnmatchedTokensAsErrors
                                     ? new List <ParseError>(unmatchedTokens.Count)
                                     : new List <ParseError>());

            if (parser.Configuration.RootCommand.TreatUnmatchedTokensAsErrors)
            {
                for (var i = 0; i < unmatchedTokens.Count; i++)
                {
                    var token = unmatchedTokens[i];
                    _errors.Add(new ParseError(parser.Configuration.ValidationMessages.UnrecognizedCommandOrArgument(token.Value)));
                }
            }
        }
Exemplo n.º 7
0
        public ParseResultVisitor(
            Parser parser,
            TokenizeResult tokenizeResult,
            IReadOnlyCollection <Token> unparsedTokens,
            IReadOnlyCollection <Token> unmatchedTokens,
            IReadOnlyCollection <ParseError> parseErrors,
            string?rawInput)
        {
            _parser         = parser;
            _tokenizeResult = tokenizeResult;
            _rawInput       = rawInput;

            var unparsedTokensCount = unparsedTokens?.Count ?? 0;

            _unparsedTokens = unparsedTokensCount == 0 ? new List <string>() : new List <string>(unparsedTokensCount);
            if (unparsedTokensCount > 0)
            {
                foreach (var unparsedToken in unparsedTokens !)
                {
                    _unparsedTokens.Add(unparsedToken.Value);
                }
            }

            var unmatchedTokensCount = unmatchedTokens?.Count ?? 0;

            _unmatchedTokens = unmatchedTokensCount == 0 ? new List <string>() : new List <string>(unmatchedTokensCount);
            if (unmatchedTokensCount > 0)
            {
                foreach (var unmatchedToken in unmatchedTokens !)
                {
                    _unmatchedTokens.Add(unmatchedToken.Value);
                }
            }

            _errors = new List <ParseError>(_tokenizeResult.Errors.Count + parseErrors.Count);

            foreach (var error in _tokenizeResult.Errors)
            {
                _errors.Add(new ParseError(error.Message));
            }

            _errors.AddRange(parseErrors);
        }