コード例 #1
0
        public void Should_Parse_Remaining_Parameters()
        {
            // Given, When
            var(result, remaining) = CommandTreeTokenizer.Tokenize(new[] { "--foo", "--", "--bar", "-qux", "\"lol\"", "w00t" });

            // Then
            result.Count.ShouldBe(8);

            remaining.Count.ShouldBe(4);
            remaining[0].ShouldBe("--bar");
            remaining[1].ShouldBe("-qux");
            remaining[2].ShouldBe("\"lol\"");
            remaining[3].ShouldBe("w00t");

            result[0].TokenKind.ShouldBe(CommandTreeToken.Kind.LongOption);
            result[0].Value.ShouldBe("foo");
            result[1].TokenKind.ShouldBe(CommandTreeToken.Kind.Remaining);
            result[1].Value.ShouldBe("--");
            result[2].TokenKind.ShouldBe(CommandTreeToken.Kind.LongOption);
            result[2].Value.ShouldBe("bar");
            result[3].TokenKind.ShouldBe(CommandTreeToken.Kind.ShortOption);
            result[3].Value.ShouldBe("q");
            result[4].TokenKind.ShouldBe(CommandTreeToken.Kind.ShortOption);
            result[4].Value.ShouldBe("u");
            result[5].TokenKind.ShouldBe(CommandTreeToken.Kind.ShortOption);
            result[5].Value.ShouldBe("x");
            result[6].TokenKind.ShouldBe(CommandTreeToken.Kind.String);
            result[6].Value.ShouldBe("lol");
            result[7].TokenKind.ShouldBe(CommandTreeToken.Kind.String);
            result[7].Value.ShouldBe("w00t");
        }
コード例 #2
0
        public void Should_Parse_Quoted_String_Correctly(params string[] args)
        {
            // Given, When
            var(result, remaining) = CommandTreeTokenizer.Tokenize(args);

            // Then
            result.Count.ShouldBe(1);
            result[0].TokenKind.ShouldBe(CommandTreeToken.Kind.String);
            result[0].Value.ShouldBe("foo");
        }
コード例 #3
0
        public void Should_Parse_Long_Option_Correctly(params string[] args)
        {
            // Given, When
            var(result, remaining) = CommandTreeTokenizer.Tokenize(args);

            // Then
            result.Count.ShouldBe(2);
            result[0].TokenKind.ShouldBe(CommandTreeToken.Kind.LongOption);
            result[0].Value.ShouldBe("foo");
            result[1].TokenKind.ShouldBe(CommandTreeToken.Kind.String);
            result[1].Value.ShouldBe("bar");
        }
コード例 #4
0
        public void Should_Parse_Short_Option_Correctly(params string[] args)
        {
            // Given, When
            var result = CommandTreeTokenizer.Tokenize(args);

            // Then
            result.Tokens.Count.ShouldBe(2);
            result.Tokens[0].TokenKind.ShouldBe(CommandTreeToken.Kind.ShortOption);
            result.Tokens[0].Value.ShouldBe("f");
            result.Tokens[1].TokenKind.ShouldBe(CommandTreeToken.Kind.String);
            result.Tokens[1].Value.ShouldBe("bar");
        }
コード例 #5
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)));
    }