Java命令行参数类 Commons CLI使用
来源:互联网 发布:java可变参数不传入 编辑:程序博客网 时间:2024/06/03 17:35
一、介绍
Commons CLI库是apache的一个子项目,比较起来,其使用方法相对规范。
http://commons.apache.org/proper/commons-cli/
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)A typical help message displayed by Commons CLI looks like this: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
二、例子
package Test;import org.apache.commons.cli.BasicParser;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.GnuParser;import org.apache.commons.cli.HelpFormatter;import org.apache.commons.cli.Option;import org.apache.commons.cli.Options;import org.apache.commons.cli.ParseException;import org.apache.commons.cli.PosixParser;public class CLITest { public static void main(String[] args) throws ParseException { Options options = new Options(); //短选项,长选项,选项后是否有参数,描述 Option option = new Option("n", "name", true, "the name of this agent"); option.setRequired(true);//必须设置 options.addOption(option); option = new Option("f", "conf-file", true, "specify a config file (required if -z missing)"); option.setRequired(false); options.addOption(option); option = new Option("h", "help", false, "display help text"); options.addOption(option); //CommandLineParser parser = new PosixParser();//Posix风格 CommandLineParser parser = new GnuParser();//gun风格 CommandLine commandLine = parser.parse(options, args); //判断 if (commandLine.hasOption('h')) { //格式化输出 new HelpFormatter().printHelp("flume-ng agent", options, true); return; } if (commandLine.hasOption('f')) { //获取参数 String file = commandLine.getOptionValue('f'); } }}
阅读全文
0 0
- Java命令行参数类 Commons CLI使用
- Java基础:Java的命令行参数类 Commons CLI的使用
- commons-cli命令行参数解析
- 使用 Apache Commons CLI 开发命令行工具
- 使用 Apache Commons CLI 开发命令行工具
- 使用 Apache Commons CLI 开发命令行工具
- 使用Apache Commons CLI开发命令行工具
- 使用 Apache Commons CLI 开发命令行工具
- 使用Apache Commons CLI 开发命令行工具
- 使用Apache Commons CLI开发命令行工具
- commons-cli-1.2 java启动参数工具包
- 使用 Apache Commons CLI 开发命令行工具示例
- Commons CLI使用详解
- Commons CLI使用详解
- Commons CLI使用
- commons-cli使用介绍
- commons-cli启动参数处理
- Apache Commons CLI 开发命令行工具
- 神经网络:2)Hi,死宅的感知器
- js将string转化为json
- 进程同步
- c++ 文件操作
- JavaScript改变this指向的3种方法(代码说明)
- Java命令行参数类 Commons CLI使用
- 第7章 51DOS操作系统设计与实现
- [IDA*] BZOJ1085: [SCOI2005]骑士精神
- 392. Is Subsequence
- 查看IPC使用情况
- 链接————csapp
- zabbix-3.2 install-guide on ubuntu 14.04 server
- 第十三天:ArrayList的底层add方法原理,按自己理解重写了一下
- SecureCRT远程连接Linux---终端中文字符出现乱码的解决方式