diff --git a/core/src/main/java/com/taobao/arthas/core/command/basic1000/GrepCommand.java b/core/src/main/java/com/taobao/arthas/core/command/basic1000/GrepCommand.java index c2929d7ae..15dbfd3b1 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/basic1000/GrepCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/basic1000/GrepCommand.java @@ -10,8 +10,6 @@ import com.taobao.middleware.cli.annotations.Name; import com.taobao.middleware.cli.annotations.Option; import com.taobao.middleware.cli.annotations.Summary; -import one.profiler.AsyncProfiler; - /** * @see com.taobao.arthas.core.shell.command.internal.GrepHandler */ @@ -93,9 +91,9 @@ public class GrepCommand extends AnnotatedCommand { this.showLineNumber = showLineNumber; } - @Option(longName = "trim-end", flag = true) + @Option(longName = "trim-end", flag = false) @DefaultValue("true") - @Description("Remove whitespaces at the end of the line") + @Description("Remove whitespaces at the end of the line, default value true") public void setTrimEnd(boolean trimEnd) { this.trimEnd = trimEnd; } @@ -170,11 +168,7 @@ public class GrepCommand extends AnnotatedCommand { @Override public void process(CommandProcess process) { - process.write("The grep command only for pipes. See 'grep --help'").write("\n"); - - String libPath = "/private/tmp/async-profiler-1.6-macos-x64/build/libasyncProfiler.so"; - AsyncProfiler instance = AsyncProfiler.getInstance(libPath); - process.write(instance.toString()); + process.write("The grep command only for pipes. See 'grep --help'\n"); process.end(); } } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/command/internal/GrepHandler.java b/core/src/main/java/com/taobao/arthas/core/shell/command/internal/GrepHandler.java index 4cd7d251b..b070551eb 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/command/internal/GrepHandler.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/command/internal/GrepHandler.java @@ -46,12 +46,16 @@ public class GrepHandler extends StdoutHandler { */ private final Integer maxCount; + private static CLI cli = null; + public static StdoutHandler inject(List tokens) { List args = StdoutHandler.parseArgs(tokens, NAME); GrepCommand grepCommand = new GrepCommand(); - CLI cli = CLIConfigurator.define(GrepCommand.class); - CommandLine commandLine = cli.parse(args); + if (cli == null) { + cli = CLIConfigurator.define(GrepCommand.class); + } + CommandLine commandLine = cli.parse(args, true); try { CLIConfigurator.inject(commandLine, grepCommand); diff --git a/core/src/test/java/com/taobao/arthas/core/command/basic1000/GrepCommandTest.java b/core/src/test/java/com/taobao/arthas/core/command/basic1000/GrepCommandTest.java new file mode 100644 index 000000000..59bd23855 --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/command/basic1000/GrepCommandTest.java @@ -0,0 +1,69 @@ +package com.taobao.arthas.core.command.basic1000; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.taobao.middleware.cli.CLI; +import com.taobao.middleware.cli.CommandLine; +import com.taobao.middleware.cli.annotations.CLIConfigurator; + +/** + * + * @author hengyunabc 2019-10-31 + * + */ +public class GrepCommandTest { + + private static CLI cli = null; + + @Before + public void before() { + cli = CLIConfigurator.define(GrepCommand.class); + } + + @Test + public void test() { + List args = Arrays.asList("-v", "ppp"); + GrepCommand grepCommand = new GrepCommand(); + CommandLine commandLine = cli.parse(args, true); + + try { + CLIConfigurator.inject(commandLine, grepCommand); + } catch (Throwable e) { + throw new RuntimeException(e); + } + Assert.assertTrue(grepCommand.isInvertMatch()); + } + + @Test + public void test2() { + List args = Arrays.asList("--before-context=6", "ppp"); + GrepCommand grepCommand = new GrepCommand(); + CommandLine commandLine = cli.parse(args, true); + + try { + CLIConfigurator.inject(commandLine, grepCommand); + } catch (Throwable e) { + throw new RuntimeException(e); + } + Assert.assertEquals(6, grepCommand.getBeforeLines()); + } + + @Test + public void test3() { + List args = Arrays.asList("--trim-end=false", "ppp"); + GrepCommand grepCommand = new GrepCommand(); + CommandLine commandLine = cli.parse(args, true); + + try { + CLIConfigurator.inject(commandLine, grepCommand); + } catch (Throwable e) { + throw new RuntimeException(e); + } + Assert.assertFalse(grepCommand.isTrimEnd()); + } +}