0

我正在开发一个 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

有谁知道如何做到这一点?如果不可能做到这一点,我想听听这个或任何可能的替代想法。

4

1 回答 1

2

您在这里有 2 个选项:

  • database, username, password或者为需要它们的每个命令username制作这 3 个附加参数(添加] 因为你显然不能有 2 个同名的参数)。
  • 使用@CliAvailabilityIndicator类似于示例中描述的方法,其中第一个命令(可能命名为useor connect)首先测试与 3 个给定参数的连接并将它们存储在某处,以便任何进一步的“真实”命令(例如add user)可以使用那些价值观。

另请注意,您实际上可以使用两者的组合(使用解决方案 2 提供默认值,解决方案 1 可能会根据具体情况覆盖该默认值)。

最后,请注意,您将永远无法拥有像您在问题开头所描述的内容,因为命令名称必须在开头并且它们不能包含--选项

于 2014-07-18T15:00:04.937 回答