首页 > 解决方案 > 使用 apache commons cli 无法识别的选项

问题描述

代码部分:

 Options options = new Options();
        options.addOption("h", "help", false, "prints the help content");
        options.addOption("f", "fileType", true, "file type");
        options.addOption("u", "url", true, "url");
        options.addOption("d", "validDate", true, "valid date");

        LoadReferenceFileRequest request=new LoadReferenceFileRequest();
        LoadReferenceFileResponse response=new LoadReferenceFileResponse();

       try{
          //Etape 2: Analyse de la ligne de commande
          CommandLineParser parser = new DefaultParser();
          CommandLine commandLine = parser.parse(options, args);

          if(commandLine.hasOption("f") && commandLine.hasOption("u") && commandLine.hasOption("d")) {
          String fileType = commandLine.getOptionValue('f');
          request.setFileType(fileType);

          String url = commandLine.getOptionValue('u');
          request.setUrl(url);

          String validDate = commandLine.getOptionValue('d');
          request.setValidDate(validDate);

          loadReferenceFileClientService.validateRequest(request,response);
          loadReferenceFileClientService.invokeInternal(request,response);

      }else {
             HelpFormatter formatter = new HelpFormatter();
             formatter.printHelp( "App" , options );
             System.exit(1);

   }

用于执行代码的错误描述和命令:

java -classpath MyClassPath -f .xml -usr/local/data/osmiumspa/ref/channel.xml -d 20180905 错误:

ERROR StatusLogger Unrecognized format specifier [n]
    ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
    %d [%thread] %-5level %logger - %msg%n%d [%thread] %-5level %logger - %msg%nError while parsing the command line: Unrecognized option: -f

标签: spring-bootcommand-lineapache-commons-cli

解决方案


问题似乎在CommandLineParser parser = new DefaultParser();

使用CommandLineParser parser = new GnuParser();可以解决这个问题


推荐阅读