歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Commons CLI使用詳解

Commons CLI使用詳解

日期:2017/3/1 9:14:42   编辑:Linux編程

Preface:

Apache Commons CLI library為用戶提供了一個解釋命令行的API.它在解釋命令行時主要有三個狀態,即:定義、解釋和詢問交互。下面的部分中將會詳細的討論這三個部分的內容,以及怎麼樣利用CLI實現它們。

接下來的部分就是一些實例,通過實例演示了如何使用Commons CLI來解釋處理命令。

Example:

下面就是我們要實現的效果(在這裡參數與命令沒有任何特殊意義,只是為了表示如何完成相應的功能):

  1. usage: gmkdir [-p][-v/--verbose][--block-size][-h/--help] DirectoryName
  2. --block-size use SIZE-byte blocks
  3. -O <file> search for buildfile towards the root of the filesystem and use it
  4. -p no error if existing, make parent directories as needed.
  5. -v,--verbose explain what is being done.
  6. -h,--help print help for the command.

參數說明與使用場景:

--block-size: use SIZE-byte blocks,在命令參數中使用場景為:gmkdir --block-size=10 testmkdir

-O <file> search for buildfile towards the root of the filesystem and use it,在命令中使用場景為:gmkdir -O test.txt testmkdir

-p no error if existing, make parent directories as needed.,在命令中使用場景為:gmkdir -p testmkdir

-v,--verbose explain what is being done.,在命令中使用場景為:gmkdir -v testmkdir或gmkdir --verbose testmkdir

-h,--help print help for the command.,在命令中使用場景為:gmkdir -h

綜合使用場景為:

gmkdir -h --可以查看該命令的幫助信息;

gmkdir -v testmkdir 或 gmkdir testmkdir -v

gmkdir -v -p testmkdir/test1/test2/test3 或 gmkdir testmkdir/test1/test2/test3 -v -p

gmkdir -O test.txt testmkdir -v 或 gmkdir testmkdir -O test.txt -v

gmkdir --block-size=10 testmkdir -v 或 gmkdir testmkdir -v --block-size=10

gmkdir --block-size=10 testmkdir/test1/test2/test3 -p

... ...

大家通過以上場景可以看到命令的參數的位置可以放置於命令後的任何位置,CLI將會幫助我們完成相應的解釋工作,需要注意的是類似與-O <file>和--block-size,其後面的參數必須得緊跟其後,如-O test.txt 、--block-size=10 ...

以上混亂的說明了要實現如上述的命令效果和場景,接下來就對具體的實現進行詳細的說明。代碼如下,具體實現通過注釋表示:

  1. package org.system.faye.commandline;
  2. import org.apache.commons.cli.CommandLine;
  3. import org.apache.commons.cli.CommandLineParser;
  4. import org.apache.commons.cli.HelpFormatter;
  5. import org.apache.commons.cli.OptionBuilder;
  6. import org.apache.commons.cli.Options;
  7. import org.apache.commons.cli.ParseException;
  8. import org.apache.commons.cli.PosixParser;
  9. public class Mkdir {
  10. /**
  11. * @param args
  12. */
  13. public static void main(String[] args) {
  14. Options opt = new Options();
  15. opt.addOption("p", false, "no error if existing, " +
  16. "make parent directories as needed.");
  17. opt.addOption("v", "verbose", false, "explain what is being done.");
  18. opt.addOption(OptionBuilder.withArgName("file")
  19. .hasArg()
  20. .withDescription("search for buildfile towards the root of the filesystem and use it")
  21. .create("O"));
  22. opt.addOption(OptionBuilder.withLongOpt("block-size")
  23. .withDescription("use SIZE-byte blocks")
  24. .withValueSeparator('=')
  25. .hasArg()
  26. .create() );
  27. opt.addOption("h", "help", false, "print help for the command.");
  28. String formatstr = "gmkdir [-p][-v/--verbose][--block-size][-h/--help] DirectoryName";
  29. HelpFormatter formatter = new HelpFormatter();
  30. CommandLineParser parser = new PosixParser();
  31. CommandLine cl = null;
  32. try {
  33. // 處理Options和參數
  34. cl = parser.parse( opt, args );
  35. } catch (ParseException e) {
  36. formatter.printHelp( formatstr, opt ); // 如果發生異常,則打印出幫助信息
  37. }
  38. // 如果包含有-h或--help,則打印出幫助信息
  39. if (cl.hasOption("h")) {
  40. HelpFormatter hf = new HelpFormatter();
  41. hf.printHelp(formatstr, "", opt, "");
  42. return;
  43. }
  44. // 判斷是否有-p參數
  45. if (cl.hasOption("p")) {
  46. System.out.println("has p");
  47. }
  48. // 判斷是否有-v或--verbose參數
  49. if (cl.hasOption("v")) {
  50. System.out.println("has v");
  51. }
  52. // 獲取參數值,這裡主要是DirectoryName
  53. String[] str = cl.getArgs();
  54. int length = str.length;
  55. System.out.println("length="+length);
  56. System.out.println("Str[0]="+str[0]);
  57. //判斷是否含有block-size參數
  58. if( cl.hasOption( "block-size" ) ) {
  59. // print the value of block-size
  60. System.out.println("block-size=" + cl.getOptionValue("block-size"));
  61. }
  62. }
  63. }

由於時間關系,這裡對Option、Options與CommandLineParser 暫時不作說明,稍後會加上,如果大家存在任何疑問,可以隨時聯系我,我會在第一時間恢復大家。

大概的了解了CLI,現在馬上運行看一下呢?(*^__^*) 嘻嘻……是不是很酷啊?哈哈!~

Copyright © Linux教程網 All Rights Reserved