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项目中应用到了这个小的套件。

目录
相关文章
|
14天前
|
Java
Java中ReentrantLock释放锁代码解析
Java中ReentrantLock释放锁代码解析
25 8
|
7天前
|
Java API 数据库
深入解析:使用JPA进行Java对象关系映射的实践与应用
【4月更文挑战第17天】Java Persistence API (JPA) 是Java EE中的ORM规范,简化数据库操作,让开发者以面向对象方式处理数据,提高效率和代码可读性。它定义了Java对象与数据库表的映射,通过@Entity等注解标记实体类,如User类映射到users表。JPA提供持久化上下文和EntityManager,管理对象生命周期,支持Criteria API和JPQL进行数据库查询。同时,JPA包含事务管理功能,保证数据一致性。使用JPA能降低开发复杂性,但需根据项目需求灵活应用,结合框架如Spring Data JPA,进一步提升开发便捷性。
|
12天前
|
Java
Java 15 神秘登场:隐藏类解析未知领域
Java 15 神秘登场:隐藏类解析未知领域
16 0
|
12天前
|
安全 Java 编译器
接口之美,内部之妙:深入解析Java的接口与内部类
接口之美,内部之妙:深入解析Java的接口与内部类
34 0
接口之美,内部之妙:深入解析Java的接口与内部类
|
30天前
|
Java 程序员 C#
静态构造方法解析,Java新手必看技能
静态构造方法解析,Java新手必看技能
9 0
|
1月前
CRM软件推荐2024:五款顶级产品解析,助您找到最佳选项!
2024年,随着民营经济发展,CRM软件成为企业增长的关键。本文推荐了五款高好评CRM:1) Zoho CRM,以其易用性和性价比受青睐;2) Zoho Bigin,轻量级选项适合小微企业;3) Salesforce,CRM巨头,但国内售后不足;4) Hubspot,提供免费版,付费版价格较高;5) Pipedrive,专注小型团队。企业在选择时应考虑试用体验和服务质量。
34 6
|
1月前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
1月前
|
机器学习/深度学习 编解码 API
FFmpeg命令行解析
FFmpeg命令行解析
68 1
|
7天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
18 0
|
7天前
yolo-world 源码解析(六)(1)
yolo-world 源码解析(六)
12 0

推荐镜像

更多