在Java中解析命令行参数的好方法是什么?
当前回答
现在是2022年,是时候比Commons CLI做得更好了……: -)
您应该构建自己的Java命令行解析器,还是使用库?
许多类似于实用程序的小型应用程序可能会滚动自己的命令行解析,以避免额外的外部依赖。Picocli可能是一个有趣的替代方案。
Picocli是一个现代化的库和框架,用于轻松构建强大的、用户友好的、支持graalvm的命令行应用程序。它存在于一个源文件中,所以应用程序可以将它作为源文件来包含,以避免添加依赖项。
它支持颜色、自动补全、子命令等等。用Java编写,可从Groovy、Kotlin、Scala等中使用。
特点:
Annotation based: declarative, avoids duplication and expresses programmer intent Convenient: parse user input and run your business logic with one line of code Strongly typed everything - command line options as well as positional parameters POSIX clustered short options (<command> -xvfInputFile as well as <command> -x -v -f InputFile) Fine-grained control: an arity model that allows a minimum, maximum and variable number of parameters, e.g, "1..*", "3..5" Subcommands (can be nested to arbitrary depth) Feature-rich: composable arg groups, splitting quoted args, repeatable subcommands, and many more User-friendly: usage help message uses colors to contrast important elements like option names from the rest of the usage help to reduce the cognitive load on the user Distribute your app as a GraalVM native image Works with Java 5 and higher Extensive and meticulous documentation
使用帮助消息很容易使用注释进行定制(无需编程)。例如:
(源)
我忍不住又加了一张截图来展示使用帮助信息的可能性。使用帮助是应用程序的门面,所以要有创意,玩得开心!
声明:我创建了picocli。欢迎反馈或提问。
其他回答
航空公司@ Github看起来不错。它基于注释,并试图模拟Git命令行结构。
如果您已经在使用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库,非常方便和强大。
看一看最近的JCommander。
我创造了它。我很高兴收到问题或功能请求。
也许这些
JArgs命令行选项解析 这个小项目为Java程序员提供了一个方便、紧凑、预打包和全面文档化的命令行选项解析器套件。最初,提供了与gnu风格的“getopt”兼容的解析。 ritopt, Java的终极选项解析器——尽管已经预先提出了几个命令行选项标准,ritopt仍然遵循opt包中规定的约定。
推荐文章
- 在流中使用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