gpt4 book ai didi

java - 在方法之前包含 spring shell 的参数

转载 作者:行者123 更新时间:2023-12-02 05:37:43 25 4
gpt4 key购买 nike

我正在开发一个 Spring Shell 项目。该工具是一个用于操作数据库中数据的命令行工具。有一些命令,例如添加用户(将记录添加到数据库中的表中)。为了执行任何命令,该工具的用户必须连接到数据库。我希望能够在一行中运行这一切。我的工具的用户应该能够编写如下命令。

--数据库连接字符串 xyz --用户名 abc --密码 mno 添加用户 --用户名 bob --角色 AA_ADMIN --公司 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 命令中的消息参数应该是运行 hw complex 或 hw enum 的先决条件所需的参数。例如,我想运行的命令是。

--message hw complex --message abc --name1 def --name2 ghi --time 7:98 --地点:西雅图

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

最佳答案

这里有两个选择:

  • 要么为每个需要它们的命令设置这 3 个附加参数(数据库、用户名、密码)参数(请注意,在您的特定示例中,您需要重命名其中一个用户名参数[连接到数据库的参数,或代表要添加的用户的参数],因为显然不能有两个同名的参数。
  • 使用 @CliAvailabilityIndi​​cator 方法,类似于示例中描述的方法,其中第一个命令(可能名为 useconnect)首先测试与 3 个给定参数的连接并将它们存储在某处,以便任何进一步的“真实”命令(例如添加用户)可以使用这些值。

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

最后,请注意,您永远无法获得与问题开头所描述的类似的内容,因为命令名称必须位于开头,并且不能包含 --(选项执行)

关于java - 在方法之前包含 spring shell 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24814174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com