JArgs命令行选项解析->Java套件

简介: 项目简介和意图 这个小的工程是为java开发者提供的,使用命令行方便的,结构紧凑的解析器工具。   public class OptionTest { private static void printUsage() { System.

项目简介和意图

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

 

目录
相关文章
|
2月前
|
机器学习/深度学习 JSON Java
Java调用Python的5种实用方案:从简单到进阶的全场景解析
在机器学习与大数据融合背景下,Java与Python协同开发成为企业常见需求。本文通过真实案例解析5种主流调用方案,涵盖脚本调用到微服务架构,助力开发者根据业务场景选择最优方案,提升开发效率与系统性能。
630 0
|
2月前
|
Java
Java的CAS机制深度解析
CAS(Compare-And-Swap)是并发编程中的原子操作,用于实现多线程环境下的无锁数据同步。它通过比较内存值与预期值,决定是否更新值,从而避免锁的使用。CAS广泛应用于Java的原子类和并发包中,如AtomicInteger和ConcurrentHashMap,提升了并发性能。尽管CAS具有高性能、无死锁等优点,但也存在ABA问题、循环开销大及仅支持单变量原子操作等缺点。合理使用CAS,结合实际场景选择同步机制,能有效提升程序性能。
|
2月前
|
Java 开发者
Java并发编程:CountDownLatch实战解析
Java并发编程:CountDownLatch实战解析
423 100
|
3月前
|
存储 缓存 Java
Java数组全解析:一维、多维与内存模型
本文深入解析Java数组的内存布局与操作技巧,涵盖一维及多维数组的声明、初始化、内存模型,以及数组常见陷阱和性能优化。通过图文结合的方式帮助开发者彻底理解数组本质,并提供Arrays工具类的实用方法与面试高频问题解析,助你掌握数组核心知识,避免常见错误。
|
3月前
|
缓存 安全 Java
Java并发性能优化|读写锁与互斥锁解析
本文深入解析Java中两种核心锁机制——互斥锁与读写锁,通过概念对比、代码示例及性能测试,揭示其适用场景。互斥锁适用于写多或强一致性场景,读写锁则在读多写少时显著提升并发性能。结合锁降级、公平模式等高级特性,助你编写高效稳定的并发程序。
227 0
|
1月前
|
存储 安全 Java
《数据之美》:Java集合框架全景解析
Java集合框架是数据管理的核心工具,涵盖List、Set、Map等体系,提供丰富接口与实现类,支持高效的数据操作与算法处理。
|
2月前
|
Java 开发者
Java 函数式编程全解析:静态方法引用、实例方法引用、特定类型方法引用与构造器引用实战教程
本文介绍Java 8函数式编程中的四种方法引用:静态、实例、特定类型及构造器引用,通过简洁示例演示其用法,帮助开发者提升代码可读性与简洁性。
|
1月前
|
存储 人工智能 算法
从零掌握贪心算法Java版:LeetCode 10题实战解析(上)
在算法世界里,有一种思想如同生活中的"见好就收"——每次做出当前看来最优的选择,寄希望于通过局部最优达成全局最优。这种思想就是贪心算法,它以其简洁高效的特点,成为解决最优问题的利器。今天我们就来系统学习贪心算法的核心思想,并通过10道LeetCode经典题目实战演练,带你掌握这种"步步为营"的解题思维。
|
2月前
|
安全 Java API
Java SE 与 Java EE 区别解析及应用场景对比
在Java编程世界中,Java SE(Java Standard Edition)和Java EE(Java Enterprise Edition)是两个重要的平台版本,它们各自有着独特的定位和应用场景。理解它们之间的差异,对于开发者选择合适的技术栈进行项目开发至关重要。
331 1
|
3月前
|
安全 Oracle Java
JAVA高级开发必备·卓伊凡详细JDK、JRE、JVM与Java生态深度解析-形象比喻系统理解-优雅草卓伊凡
JAVA高级开发必备·卓伊凡详细JDK、JRE、JVM与Java生态深度解析-形象比喻系统理解-优雅草卓伊凡
286 0
JAVA高级开发必备·卓伊凡详细JDK、JRE、JVM与Java生态深度解析-形象比喻系统理解-优雅草卓伊凡

推荐镜像

更多
  • DNS