The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool.
Commons CLI supports different types of options:
- POSIX like options (ie.
tar -zxvf foo.tar.gz
) - GNU like long options (ie.
du --human-readable --max-depth=1
) - Java like properties (ie.
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo
) - Short options with value attached (ie.
gcc -O2 foo.c
) - long options with single hyphen (ie.
ant -projecthelp
)
CLI 即Command Line Interface,也就是"命令行接口",它为Java 程序访问和解析命令行
参数提供了一种统一的接口。
主要处理java启动时,输入命令行的
纯java编译完*.class以后,会通过,下面命令运行带main的类
java 类名
打成jar包的则通过下面命令(带main方法)
java –jar 包名.jar
在eclipse下运行则需要通过Run as /runConfigurations/Arguments来进行命令行参数配置
参数的配置和我们常用的命令一样,横杠+参数名+空格+参数值
-参数名 参数值
然后java会根据main方法中 String[] args来取得命令行参数
通过使用commons-cli则可以很容易的访问参数,而不必去循环String[] args
自动生成帮助声明:
打印出来信息:
参考网址:
http://commons.apache.org/cli/
http://phl.javaeye.com/blog/730737
http://blog.csdn.net/socoolfj/archive/2005/12/01/541429.aspx