Commons CLI使用详解

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 {
/**
* @param args
*/
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 {
// 处理Options和参数
cl = parser.parse( opt, args );
} catch (ParseException e) {
formatter.printHelp( formatstr, opt ); // 如果发生异常,则打印出帮助信息
}
// 如果包含有-h或--help,则打印出帮助信息
if (cl.hasOption("h")) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp(formatstr, "", opt, "");
return;
}
// 判断是否有-p参数
if (cl.hasOption("p")) {
System.out.println("has p");
}
// 判断是否有-v或--verbose参数
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" ) ) {
// print the value of block-size
System.out.println("block-size=" + cl.getOptionValue("block-size"));
}
}
}

由于时间关系,这里对Option、Options与CommandLineParser 暂时不作说明,稍后会加上,如果大家存在任何疑问,可以随时联系我,我会在第一时间恢复大家。


————————————————
版权声明:本文为CSDN博主「我晕死哦」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/faye0412/article/details/2949753

原文地址:https://www.cnblogs.com/javalinux/p/15007072.html