Preface:
Apache Commons CLI library为用户提供了一个解释命令行的API.它在解释命令行时主要有三个状态,即:定义、解释和询问交互。下面的部分中将会详细的讨论这三个部分的内容,以及怎么样利用CLI实现它们。
接下来的部分就是一些实例,通过实例演示了如何使用Commons CLI来解释处理命令。
Example:
下面就是我们要实现的效果(在这里参数与命令没有任何特殊意义,只是为了表示如何完成相应的功能):
- usage: gmkdir [-p][-v/--verbose][--block-size][-h/--help] DirectoryName
- --block-size use SIZE-byte blocks
- -O <file> search for buildfile towards the root of the filesystem and use it
- -p no error if existing, make parent directories as needed.
- -v,--verbose explain what is being done.
- -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 ...
以上混乱的说明了要实现如上述的命令效果和场景,接下来就对具体的实现进行详细的说明。代码如下,具体实现通过注释表示:
- package org.system.faye.commandline;
-
- import org.apache.commons.cli.CommandLine;
- import org.apache.commons.cli.CommandLineParser;
- import org.apache.commons.cli.HelpFormatter;
- import org.apache.commons.cli.OptionBuilder;
- import org.apache.commons.cli.Options;
- import org.apache.commons.cli.ParseException;
- import org.apache.commons.cli.PosixParser;
-
- public class Mkdir {
-
-
-
-
- public static void main(String[] args) {
- Options opt = new Options();
- opt.addOption("p", false, "no error if existing, " +
- "make parent directories as needed.");
- opt.addOption("v", "verbose", false, "explain what is being done.");
- opt.addOption(OptionBuilder.withArgName("file")
- .hasArg()
- .withDescription("search for buildfile towards the root of the filesystem and use it")
- .create("O"));
- opt.addOption(OptionBuilder.withLongOpt("block-size")
- .withDescription("use SIZE-byte blocks")
- .withValueSeparator('=')
- .hasArg()
- .create() );
- opt.addOption("h", "help", false, "print help for the command.");
-
- String formatstr = "gmkdir [-p][-v/--verbose][--block-size][-h/--help] DirectoryName";
-
- HelpFormatter formatter = new HelpFormatter();
- CommandLineParser parser = new PosixParser();
- CommandLine cl = null;
- try {
-
- cl = parser.parse( opt, args );
- } catch (ParseException e) {
- formatter.printHelp( formatstr, opt );
- }
-
- if (cl.hasOption("h")) {
- HelpFormatter hf = new HelpFormatter();
- hf.printHelp(formatstr, "", opt, "");
- return;
- }
-
- if (cl.hasOption("p")) {
- System.out.println("has p");
- }
-
- if (cl.hasOption("v")) {
- System.out.println("has v");
- }
- DirectoryName
- String[] str = cl.getArgs();
- int length = str.length;
- System.out.println("length="+length);
- System.out.println("Str[0]="+str[0]);
- //判断是否含有block-size参数
- if( cl.hasOption( "block-size" ) ) {
-
- System.out.println("block-size=" + cl.getOptionValue("block-size"));
- }
- }
-
- }
-