在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;

   // ...
}

其他回答

看看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.

我一直试图维护一个Java CLI解析器列表。

航空公司 Active Fork: https://github.com/rvesse/airline argparse4j argparse args4j clajr cli-parser CmdLn 命令行 DocOpt.java 海豚getopt DPML CLI (Jakarta Commons CLI2分支) 马蒂亚斯·劳克斯博士 Jakarta Commons CLI jargo jargp jargs java-getopt jbock JCLAP jcmdline jcommander jcommando Jewelcli(作者:我) JOpt简单 jsap naturalcli Object Mentor CLI文章(关于重构和TDD的更多内容) parse-cmd ritopt 罗普 TE-Code命令 picocli有ANSI彩色使用帮助和自动完成

是的。

我想你在寻找这样的东西: http://commons.apache.org/cli

Apache Commons CLI库提供了一个用于处理命令行接口的API。

我想向您展示我的实现:ReadyCLI

优点:

for lazy programmers: a very small number of classes to learn, just see the two small examples on the README in the repository and you are already at 90% of learning; just start coding your CLI/Parser without any other knowledge; ReadyCLI allows coding CLIs in the most natural way; it is designed with Developer Experience in mind; it largely uses the Builder design pattern and functional interfaces for Lambda Expressions, to allow a very quick coding; it supports Options, Flags and Sub-Commands; it allows to parse arguments from command-line and to build more complex and interactive CLIs; a CLI can be started on Standard I/O just as easily as on any other I/O interface, such as sockets; it gives great support for documentation of commands.

我开发这个项目是因为我需要新的功能(选项,标志,子命令),并且可以在我的项目中以最简单的方式使用。

Argparse4j是我发现的最好的。它模仿Python的argparse库,非常方便和强大。