SpringShell 除了允許我們自定義命令之外, 還提供了一些內(nèi)置命令, 用于輔助我們操作. 筆者使用的是SpringShell 2.0版本, 內(nèi)置命令只提供了五個(gè):help, clear, stacktrace, script, exit/quite.
1.help命令-查看幫助
1.1 查看所有內(nèi)置命令
help 本身就是內(nèi)置命令, 不跟參數(shù)的help命令會(huì)打印所有內(nèi)置命令.
shell:>help
AVAILABLE COMMANDS
Built-In Commands
clear: Clear the shell screen.
exit, quit: Exit the shell.
help: Display help about available commands.
script: Read and execute commands from a file.
stacktrace: Display the full stacktrace of the last error.
1.2 查看命令詳情
可通過help + 命令方式, 查看命令詳情.
shell:>help add
NAME
add - 計(jì)算兩個(gè)整數(shù)的加法
SYNOPSYS
add [-a] int [-b] int
OPTIONS
-a int
[Mandatory]
-b int
[Mandatory]
ALSO KNOWN AS
sum
2. clear命令-清屏
SpringShell 也提供了類似于linux shell的清屏方式, 輸入clear 或使用Ctrl+L快捷鍵.
shell:>clear
3. stacktrace命令-查看異常堆棧信息
默認(rèn)情況下, 命令執(zhí)行拋出的異常, 只會(huì)輸出異常的內(nèi)容, 不會(huì)輸出異常的堆棧信息. 當(dāng)命令發(fā)生異常時(shí), 我們可以通過stacktrace 來查看異常的堆棧信息. 需要注意的是, stacktrace 永遠(yuǎn)只保存上一次的異常的堆棧信息.
shell:>div 2 0
/ by zero
Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.
shell:>stacktrace
java.lang.ArithmeticException: / by zero
at org.zongf.learn.spring.shell.cmd.CalculatorCommands.div(CalculatorCommands.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:246)
at org.springframework.shell.Shell.evaluate(Shell.java:169)
at org.springframework.shell.Shell.run(Shell.java:134)
at org.springframework.shell.jline.InteractiveShellApplicationRunner.run(InteractiveShellApplicationRunner.java:84)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:794)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at org.zongf.learn.spring.shell.SpringShellApplication.main(SpringShellApplication.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
4. script命令-執(zhí)行腳本
script命令使批量執(zhí)行命令成了可能, 我們可以將一組命令存入文件, 然后批量執(zhí)行. 需要注意的時(shí), 文件名需要使用絕對(duì)路徑.
4.1 創(chuàng)建批量腳本文件
腳本存放在 /tmp/zongf/cmds
add 1 2
add 2 3
add 3 4
div 4 2
div 9 3
4.2 執(zhí)行腳本
shell:>script /tmp/zongf/cmds
3
5
7
2
3
5. exit/quit命令-退出應(yīng)用
exit和quite命令互為別名, 使用哪個(gè)都可以退出應(yīng)用.
shell:>exit