在Java中解析命令行参数的好方法是什么?
当前回答
如果您已经在使用Spring Boot,那么参数解析将是现成的。
如果你想在启动后运行一些东西,实现ApplicationRunner接口:
@SpringBootApplication
public class Application implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(ApplicationArguments args) {
args.containsOption("my-flag-option"); // test if --my-flag-option was set
args.getOptionValues("my-option"); // returns values of --my-option=value1 --my-option=value2
args.getOptionNames(); // returns a list of all available options
// do something with your args
}
}
您的run方法将在上下文成功启动后被调用。
如果你需要在启动你的应用程序上下文之前访问参数,你可以简单地手动解析应用程序参数:
@SpringBootApplication
public class Application implements ApplicationRunner {
public static void main(String[] args) {
ApplicationArguments arguments = new DefaultApplicationArguments(args);
// do whatever you like with your arguments
// see above ...
SpringApplication.run(Application.class, args);
}
}
最后,如果您需要访问bean中的参数,只需注入ApplicationArguments:
@Component
public class MyBean {
@Autowired
private ApplicationArguments arguments;
// ...
}
其他回答
Argparse4j是我发现的最好的。它模仿Python的argparse库,非常方便和强大。
最近有人向我推荐了基于注释的args4j。我真的很喜欢!
看看Spring Shell
Spring Shell’s features include A simple, annotation driven, programming model to contribute custom commands Use of Spring Boot auto-configuration functionality as the basis for a command plugin strategy Tab completion, colorization, and script execution Customization of command prompt, shell history file name, handling of results and errors Dynamic enablement of commands based on domain specific criteria Integration with the bean validation API Already built-in commands, such as clear screen, gorgeous help, exit ASCII art Tables, with formatting, alignment, fancy borders, etc.
正如前面提到的一个评论(https://github.com/pcj/google-options)是一个很好的开始。
我想补充的一点是:
1)如果您遇到一些解析器反射错误,请尝试使用更新版本的guava。在我的例子中:
maven_jar(
name = "com_google_guava_guava",
artifact = "com.google.guava:guava:19.0",
server = "maven2_server",
)
maven_jar(
name = "com_github_pcj_google_options",
artifact = "com.github.pcj:google-options:jar:1.0.0",
server = "maven2_server",
)
maven_server(
name = "maven2_server",
url = "http://central.maven.org/maven2/",
)
2)当运行命令行时:
bazel run path/to/your:project -- --var1 something --var2 something -v something
3)当你需要使用帮助时,只需输入:
bazel run path/to/your:project -- --help
我知道这里的大多数人会找到一千万个理由来解释他们为什么不喜欢我的方式,但没关系。我喜欢让事情变得简单,所以我只是使用'='将键和值分开,并将它们存储在一个HashMap中,就像这样:
Map<String, String> argsMap = new HashMap<>();
for (String arg: args) {
String[] parts = arg.split("=");
argsMap.put(parts[0], parts[1]);
}
您可以始终维护一个包含您所期望的参数的列表,以帮助用户避免忘记参数或使用错误的参数……然而,如果你想要太多的功能,这个解决方案并不适合你。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 如何从终端/命令行调用VS代码编辑器
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder