我正在开发一个 Spring Shell 项目。该工具是一个命令行工具,用于操作数据库中的数据。有一些命令,如添加用户(将记录添加到数据库中的表中)。为了执行任何命令,该工具的用户必须连接到数据库。我希望能够在一行中运行这一切。我的工具的用户应该能够编写如下命令。
--database 连接字符串 xyz --username abc --password mno add user --username bob --role AA_ADMIN --company Microsoft
这里需要三个参数数据库连接字符串,用户名和密码才能运行添加用户命令。
下面我包含了一些来自 spring shell 参考文档的示例代码
package commands;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@Component
public class UserManipulation implements CommandMarker {
private boolean simpleCommandExecuted = false;
@CliAvailabilityIndicator({"hw simple"})
public boolean isSimpleAvailable() {
//always available
return true;
}
@CliAvailabilityIndicator({"hw complex", "hw enum"})
public boolean isComplexAvailable() {
if (simpleCommandExecuted) {
return true;
} else {
return false;
}
}
@CliCommand(value = "hw simple", help = "Print a simple hello world message")
public String simple(
@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
@CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello", specifiedDefaultValue="At work") final String location) {
simpleCommandExecuted = true;
return "Message = [" + message + "] Location = [" + location + "]";
}
@CliCommand(value = "hw complex", help = "Print a complex hello world message")
public String hello(
@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
@CliOption(key = { "name1"}, mandatory = true, help = "Say hello to the first name") final String name1,
@CliOption(key = { "name2" }, mandatory = true, help = "Say hello to a second name") final String name2,
@CliOption(key = { "time" }, mandatory = false, specifiedDefaultValue="now", help = "When you are saying hello") final String time,
@CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello") final String location) {
return "Hello " + name1 + " and " + name2 + ". Your special message is " + message + ". time=[" + time + "] location=[" + location + "]";
}
@CliCommand(value = "hw enum", help = "Print a simple hello world message from an enumerated value")
public String eenum(
@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final MessageType message){
return "Hello. Your special enumerated message is " + message;
}
enum MessageType {
Type1("type1"),
Type2("type2"),
Type3("type3");
private String type;
private MessageType(String type){
this.type = type;
}
public String getType(){
return type;
}
}
}
所以目前,hw simple 是在运行 hw complex 或 hw enum 命令之前需要执行的命令。我不希望 hw simple 成为命令,而是 hw simple 命令中的 message 参数应该是运行 hw complex 或 hw enum 的先决条件所必需的参数。例如,我想运行的命令是。
--message hw complex --message abc --name1 def --name2 ghi --time 7:98 --location: Seattle
有谁知道如何做到这一点?如果不可能做到这一点,我想听听这个或任何可能的替代想法。