Exemplo n.º 1
0
        public CommandTreeParserResult Parse(IEnumerable <string> args)
        {
            var context = new CommandTreeParserContext(args, _parsingMode);

            var tokenizerResult = CommandTreeTokenizer.Tokenize(context.Arguments);
            var tokens          = tokenizerResult.Tokens;
            var rawRemaining    = tokenizerResult.Remaining;

            var result = default(CommandTree);

            if (tokens.Count > 0)
            {
                // Not a command?
                var token = tokens.Current;
                if (token == null)
                {
                    // Should not happen, but the compiler isn't
                    // smart enough to realize this...
                    throw new CommandRuntimeException("Could not get current token.");
                }

                if (token.TokenKind != CommandTreeToken.Kind.String)
                {
                    // Got a default command?
                    if (_configuration.DefaultCommand != null)
                    {
                        result = ParseCommandParameters(context, _configuration.DefaultCommand, null, tokens);
                        return(new CommandTreeParserResult(
                                   result, new RemainingArguments(context.GetRemainingArguments(), rawRemaining)));
                    }

                    // Show help?
                    if (_help?.IsMatch(token.Value) == true)
                    {
                        return(new CommandTreeParserResult(
                                   null, new RemainingArguments(context.GetRemainingArguments(), rawRemaining)));
                    }

                    // Unexpected option.
                    throw CommandParseException.UnexpectedOption(context.Arguments, token);
                }

                // Does the token value match a command?
                var command = _configuration.FindCommand(token.Value, CaseSensitivity);
                if (command == null)
                {
                    if (_configuration.DefaultCommand != null)
                    {
                        result = ParseCommandParameters(context, _configuration.DefaultCommand, null, tokens);
                        return(new CommandTreeParserResult(
                                   result, new RemainingArguments(context.GetRemainingArguments(), rawRemaining)));
                    }
                }

                // Parse the command.
                result = ParseCommand(context, _configuration, null, tokens);
            }
            else
            {
                // Is there a default command?
                if (_configuration.DefaultCommand != null)
                {
                    result = ParseCommandParameters(context, _configuration.DefaultCommand, null, tokens);
                }
            }

            return(new CommandTreeParserResult(
                       result, new RemainingArguments(context.GetRemainingArguments(), rawRemaining)));
        }