我有这个自定义命令:
public final class ArgsCommand extends Command {
private String foobar;
private String baz;
public ArgsCommand() {
super("args", "sets additional application arguments");
}
public String getFoobar() {
return foobar;
}
public String getBaz() {
return baz;
}
@Override
public void configure(Subparser subparser) {
subparser.addArgument("-foobar")
.dest("foobar")
.type(String.class)
.required(true)
.help("the foobar value for the application");
subparser.addArgument("-baz")
.dest("baz")
.type(String.class)
.required(true)
.help("the baz value for the application");
}
@Override
public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception {
foobar = namespace.getString("foobar");
baz = namespace.getString("baz");
if (foobar == null || foobar.length() == 0
|| baz == null || baz.length() == 0) {
throw new IllegalArgumentException(
"One or more required command-line arguments is missing!");
}
// do something with the additional arguments
}
}
...在应用程序中设置如下:
@Override
public void initialize(final Bootstrap<FoobarBazConfiguration> bootstrap) {
bootstrap.addCommand(new ArgsCommand());
}
...并从命令行执行如下:
java -jar <jar file> args -foobar asdf -baz 123
我想这样做(无法正常工作):
java -jar <jar file> server <yml file> args -foobar asdf -baz 123
我现在需要一个自定义命令参数来传递和处理.yml文件吗?
答案 0 :(得分:0)
好的,采取不同的方法,这比我想象的容易 -
1) - 将自定义键/值对添加到.yml配置文件中:
foobar: asdf
baz: 123
2) - 在FoobarBazConfiguration.java中注册自定义键:
import io.dropwizard.Configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
public final class FoobarBazConfiguration extends Configuration {
@NotEmpty
private String foobar;
@NotEmpty
private String baz;
@JsonProperty("foobar")
public String getFoobar() {
return foobar;
}
@JsonProperty("foobar")
public void setFoobar(String foobar) {
this.foobar = foobar;
}
@JsonProperty("baz")
public String getBaz() {
return baz;
}
@JsonProperty("baz")
public void setBaz(String baz) {
this.baz = baz;
}
}
3) - 从FoobarBazApplication.java中的配置对象中检索值:
System.out.println(configuration.getFoobar());
System.out.println(configuration.getBaz());
4) - 按照惯例从命令行运行应用程序:
java -jar <jar file> server <yml file>
我仍然不知道如何使用Command
的子类server <.yml file>
。
答案 1 :(得分:0)
如果要在.yml文件中使用嵌套键/值对,可以使用Map<String, String>
:
1) - 将自定义键/值对添加到.yml配置文件中:
args:
foobar: asdf
baz: 123
2) - 在FoobarBazConfiguration.java中注册地图:
import io.dropwizard.Configuration;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
public final class FoobarBazConfiguration extends Configuration {
@NotEmpty
private Map<String, String> args;
@JsonProperty("args")
public Map<String, String> getArgs() {
return args;
}
@JsonProperty("args")
public void setArgs(Map<String, String> args) {
this.args = args;
}
@JsonIgnore
public String getFoobar() {
return args.get("foobar");
}
@JsonIgnore
public String getBaz() {
return args.get("baz");
}
}
3) - 从FoobarBazApplication.java中的配置对象中检索值:
System.out.println(configuration.getFoobar());
System.out.println(configuration.getBaz());