• maven依赖引入
<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.2</version>
</dependency>
  • 示例代码,以下是从命令行读取文件路径的例子
private static void main(String[] args) {
        
        final Options options = new Options();
        final Option option = new Option("f", true, "Configuration file path");
        options.addOption(option);
        
        final CommandLineParser parser = new PosixParser();
        CommandLine cmd = null;
        try {
            cmd = parser.parse(options, args);
        } catch (final ParseException e) {
            throw new Exception("parser command line error",e);
        }
        
        String configFilePath = null;
        if (cmd.hasOption("f")) {
            configFilePath = cmd.getOptionValue("f");
        }else{
            System.err.println("please input the configuration file path by -f option");
            System.exit(1);
        }
        if (StringUtils.isBlank(configFilePath)) {
            throw new Exception("Blank file path");
        }
        
        return configFilePath;
    }

 

相关文章:

  • 2022-02-15
  • 2022-02-02
  • 2023-02-17
  • 2021-07-18
  • 2021-09-07
  • 2021-07-31
  • 2021-12-05
  • 2022-01-20
猜你喜欢
  • 2021-11-28
  • 2021-12-09
  • 2022-12-23
  • 2021-12-03
  • 2021-09-17
  • 2021-12-25
相关资源
相似解决方案