JArgs命令行选项解析->Java套件
时间:2022-05-04
本文章向大家介绍JArgs命令行选项解析->Java套件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
项目简介和意图
这个小的工程是为java开发者提供的,使用命令行方便的,结构紧凑的解析器工具。
public class OptionTest {
private static void printUsage() {
System.err
.println("Usage: OptionTest [-d,--debug] [{-v,--verbose}] [{--alt}] [{--name} a_name]n"
+ " [{-s,--size} a_number] [{-f,--fraction} a_float] [a_nother]");
}
public static void main(String[] args) {
// First, you must create a CmdLineParser, and add to it the
// appropriate Options.
// To start with, we add the Options -d, -v, -s, and -f, with aliases
// --debug, --verbose, --size, and --fraction respectively.
// The -d and -v options have no associated value -- they are either
// present, or they are not. The -s and -f options take integer and
// double-precision floating-point values respectively.
CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
CmdLineParser.Option size = parser.addIntegerOption('s', "size");
CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
// Options may have just a long form with no corresponding short form.
// Here, we add --alt and --name options.
CmdLineParser.Option alt = parser.addBooleanOption("alt");
CmdLineParser.Option name = parser.addStringOption('n', "name");
// Next, you must parse the user-provided command line arguments, and
// catch any errors therein.
// Options may appear on the command line in any order, and may even
// appear after some or all of the non-option arguments.
// If the user needs to specify non-option arguments that start with a
// minus, then they may indicate the end of the parsable options with
// -- , like this:
// prog -f 20 -- -10 -fred
// The -f 20 will be parsed as the fraction option, with the value 20.
// The -10 and -fred arguments will be regarded as non-option
// arguments, and passed through getRemainingArgs as unparsed Strings.
// Short boolean options may be specified separately (-d -v) or
// together (-dv).
// Options with values may be given on the command line as -f 1.0 or
// --fraction=1.0.
try {
parser.parse(args);
} catch (CmdLineParser.OptionException e) {
System.err.println(e.getMessage());
printUsage();
System.exit(2);
}
// For options that may be specified only zero or one time, the value
// of that option may be extracted as shown below. If the options
// were not specified, the corresponding values will be null.
Boolean debugValue = (Boolean) parser.getOptionValue(debug);
String nameValue = (String) parser.getOptionValue(name);
// Alternatively, you may specify a default value. This will be
// returned (instead of null) when the command line argument is
// missing.
Boolean altValue = (Boolean) parser.getOptionValue(alt, Boolean.FALSE);
Integer sizeValue = (Integer) parser.getOptionValue(size, new Integer(
42));
// If your application requires it, options may be specified more than
// once. In this case, you may get all the values specified by the
// user, as a Vector:
Vector fractionValues = parser.getOptionValues(fraction);
// Alternatively, you may make the loop explicit:
int verbosity = 0;
while (true) {
Boolean verboseValue = (Boolean) parser.getOptionValue(verbose);
if (verboseValue == null) {
break;
} else {
verbosity++;
}
}
// The remaining command-line arguments -- those that do not start
// with a minus sign -- can be captured like this:
String[] otherArgs = parser.getRemainingArgs();
// For testing purposes, we just print out the option values and
// remaining command-line arguments. In a real program, of course,
// one would pass them to a function that does something more useful.
System.out.println("debug: " + debugValue);
System.out.println("alt: " + altValue);
System.out.println("size: " + sizeValue);
System.out.println("name: " + nameValue);
System.out.println("verbosity: " + verbosity);
Enumeration e = fractionValues.elements();
while (e.hasMoreElements()) {
System.out.println("fraction: " + (Double) e.nextElement());
}
System.out.println("remaining args: ");
for (int i = 0; i < otherArgs.length; ++i) {
System.out.println(otherArgs[i]);
}
System.exit(0);
}
}
使用JArgs的项目有JBoss,Columba等。
总结一下就是这是一个java的命令解析的小的工程,可以方便的解析各种输入的参数信息。在YUICompressor项目中应用到了这个小的套件。
- Git命令速记
- linux设备驱动第三篇:如何写一个简单的字符设备驱动
- Tensorflow高级API的进阶--利用tf.contrib.learn建立输入函数
- Spring速查手册(三)——Spring+JDBC
- [WebKit] JavaScriptCore解析--基础篇(一)字节码的生成及抽象语法树的构建详情分析
- Spring速查手册(二)——Bean的作用域
- pyTorch自然语言处理简单例子
- 一文初探Tensorflow高级API使用(初学者篇)
- Spring速查手册——Bean装配
- 回溯法(一)——n皇后问题
- 图的邻接表示法Java版
- 图的遍历(BFS+DFS)
- 工厂模式
- Oracle 12.2新特性 | 基于权重的节点驱逐
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- PHP pthreads v3下worker和pool的使用方法示例
- 浅谈python脚本设置运行参数的方法
- laravel框架实现敏感词汇过滤功能示例
- python 接收处理外带的参数方法
- Django框架实现的简单分页功能示例
- php测试kafka项目示例
- Python读取YUV文件,并显示的方法
- php-7.3.6 编译安装过程
- PHP查找一列有序数组是否包含某值的方法
- python 从文件夹抽取图片另存的方法
- laravel框架使用极光推送消息操作示例
- 对pycharm 修改程序运行所需内存详解
- Python小工具之消耗系统指定大小内存的方法
- Thinkphp框架+Layui实现图片/文件上传功能分析
- PHP实现单例模式建立数据库连接的方法分析