Commons CLI 的使用

java 命令行通用jar包,用于处理命令行控制,如:

 

usage: ls
-A,--almost-all          do not list implied . and ..
-a,--all                 do not hide entries starting with .
-B,--ignore-backups      do not list implied entried ending with ~
-b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
-c                       with -lt: sort by, and show, ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
-C                       list entries by columns

示例:

CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse( options, args);

 

项目实例:

public static void main(String[] args) {
  
 //获取spring
  AbstractApplicationContext context;
  BookStaticizeService bookStaticizeService;
  context = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext-core.xml");
  context.registerShutdownHook();
  bookStaticizeService = (BookStaticizeService) context.getBean("bookStaticizeService");

 

//命令行处理:
  CommandLineParser parser = new PosixParser();
  Options options = new Options();
  options.addOption("h", "help", false, "print this message");
  options.addOption("s", "store", true,
    "store to staticize");
  options.addOption("b", "book", false,
  "book to staticize");
  options.addOption("a", "all", false, "process all");
  options.addOption("f", "force", false, " force process all");
  
  try {
   CommandLine line = parser.parse(options, args);
   if (args.length < 1 || line.hasOption("h")) {
    HelpFormatter formatter = new HelpFormatter();
    formatter
      .printHelp(
        "java -jar com.ztl.book3un.staticize15.client-1.5.0-shaded.jar",
        options);
    return;
   }
   boolean force = false;
   if(line.hasOption("f")){
    if(force){
     
    }
   }
   if (line.hasOption("a")) {
    bookStaticizeService.processAllStatic(force);
    return;
   }
   if (line.hasOption("s")) {
    
    String storeId = line.getOptionValue("s");
    log.debug("storeId :"+storeId);
    bookStaticizeService.processStatic(storeId, force);
    log.debug("last ---------");
   }
   if (line.hasOption("b")) {
    String bookId = line.getOptionValue("b");
    bookStaticizeService.processStaticBookId(bookId, force);
   }
  } catch (ParseException exp) {
   System.err.println(exp.getMessage());
  }

你可能感兴趣的:(spring,C++,c,F#,C#)