diff --git a/core/src/main/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImpl.java b/core/src/main/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImpl.java index 175f4cd824e..55d9b28f4a7 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImpl.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImpl.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.util.Objects; /** * @author Julien Viet @@ -100,11 +101,20 @@ private static List adjustTokensForSpecialSymbols(List token return separateLeadingAndTailingSymbol(tokens, PIPE, REDIRECT_APPEND, REDIRECT); } + private static boolean isQuotedTokenValue(String raw, String value) { + return Objects.equals(raw, "\"" + value + "\"") + || Objects.equals(raw, "'" + value + "'"); + } + private static List separateLeadingAndTailingSymbol(List tokens, String... symbols) { List adjustedTokens = new ArrayList<>(); for (CliToken token : tokens) { String value = token.value(); String raw = token.raw(); + if (isQuotedTokenValue(raw, value) || "".equals(value) || "".equals(value)) { + adjustedTokens.add(token); + continue; + } boolean handled = false; for (String symbol : symbols) { if (value.equals(symbol)) { diff --git a/core/src/test/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImplTest.java b/core/src/test/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImplTest.java index 8e347d3adf5..feda84201ff 100644 --- a/core/src/test/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImplTest.java +++ b/core/src/test/java/com/taobao/arthas/core/shell/cli/impl/CliTokenImplTest.java @@ -341,6 +341,38 @@ public void testSeparateRedirect() { } + @Test + public void testSeparateRedirect2() { + String[] expectedTextTokenValue = new String[]{"watch", "demo.MathGame", ""}; + String cmd = "watch demo.MathGame \"\""; + List actualTokens = CliTokenImpl.tokenize(cmd); + assertEqualsIgnoreBlank(expectedTextTokenValue, actualTokens); + Assert.assertEquals(cmd, concatRaw(actualTokens)); + + expectedTextTokenValue = new String[]{"watch", "demo.MathGame", ""}; + cmd = "watch demo.MathGame "; + actualTokens = CliTokenImpl.tokenize(cmd); + assertEqualsIgnoreBlank(expectedTextTokenValue, actualTokens); + Assert.assertEquals(cmd, concatRaw(actualTokens)); + + expectedTextTokenValue = new String[]{"watch", "demo.MathGame", ""}; + cmd = "watch demo.MathGame ''"; + actualTokens = CliTokenImpl.tokenize(cmd); + assertEqualsIgnoreBlank(expectedTextTokenValue, actualTokens); + Assert.assertEquals(cmd, concatRaw(actualTokens)); + + expectedTextTokenValue = new String[]{"watch", "demo.MathGame", "", "#cost> 100"}; + cmd = "watch demo.MathGame '#cost> 100'"; + actualTokens = CliTokenImpl.tokenize(cmd); + assertEqualsIgnoreBlank(expectedTextTokenValue, actualTokens); + Assert.assertEquals(cmd, concatRaw(actualTokens)); + expectedTextTokenValue = new String[]{"watch", "demo.MathGame", "", "> 100"}; + cmd = "watch demo.MathGame '> 100'"; + actualTokens = CliTokenImpl.tokenize(cmd); + assertEqualsIgnoreBlank(expectedTextTokenValue, actualTokens); + Assert.assertEquals(cmd, concatRaw(actualTokens)); + } + @Test public void testSeparateRedirectAppend() { String[] expectedTextTokenValue = new String[]{"jad", "aaa", ">>", "bbb"};