package jargs.gnu;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Locale;
/**
* Largely GNU-compatible command-line options parser. Has short (-v) and
* long-form (--verbose) option support, and also allows options with
* associated values (-d 2, --debug 2, --debug=2). Option processing
* can be explicitly terminated by the argument '--'.
*
* @author Steve Purcell
* @version $Revision: 1.10 $
* @see jargs.examples.gnu.OptionTest
*/
public class CmdLineParser {
/**
* Base class for exceptions that may be thrown when options are parsed
*/
public static abstract class OptionException extends Exception {
OptionException(String msg) { super(msg); }
}
/**
* Thrown when the parsed command-line contains an option that is not
* recognised. <code>getMessage()</code> returns
* an error string suitable for reporting the error to the user (in
* English).
*/
public static class UnknownOptionException extends OptionException {
UnknownOptionException( String optionName ) {
this(optionName, "Unknown option '" + optionName + "'");
}
UnknownOptionException( String optionName, String msg ) {
super(msg);
this.optionName = optionName;
}
/**
* @return the name of the option that was unknown (e.g. "-u")
*/
public String getOptionName() { return this.optionName; }
private String optionName = null;
}
/**
* Thrown when the parsed commandline contains multiple concatenated
* short options, such as -abcd, where one is unknown.
* <code>getMessage()</code> returns an english human-readable error
* string.
* @author Vidar Holen
*/
public static class UnknownSuboptionException
extends UnknownOptionException {
private char suboption;
UnknownSuboptionException( String option, char suboption ) {
super(option, "Illegal option: '"+suboption+"' in '"+option+"'");
this.suboption=suboption;
}
public char getSuboption() { return suboption; }
}
/**
* Thrown when the parsed commandline contains multiple concatenated
* short options, such as -abcd, where one or more requires a value.
* <code>getMessage()</code> returns an english human-readable error
* string.
* @author Vidar Holen
*/
public static class NotFlagException extends UnknownOptionException {
private char notflag;
NotFlagException( String option, char unflaggish ) {
super(option, "Illegal option: '"+option+"', '"+
unflaggish+"' requires a value");
notflag=unflaggish;
}
/**
* @return the first character which wasn't a boolean (e.g 'c')
*/
public char getOptionChar() { return notflag; }
}
/**
* Thrown when an illegal or missing value is given by the user for
* an option that takes a value. <code>getMessage()</code> returns
* an error string suitable for reporting the error to the user (in
* English).
*/
public static class IllegalOptionValueException extends OptionException {
public IllegalOptionValueException( Option opt, String value ) {
super("Illegal value '" + value + "' for option " +
(opt.shortForm() != null ? "-" + opt.shortForm() + "/" : "") +
"--" + opt.longForm());
this.option = opt;
this.value = value;
}
/**
* @return the name of the option whose value was illegal (e.g. "-u")
*/
public Option getOption() { return this.option; }
/**
* @return the illegal value
*/
public String getValue() { return this.value; }
private Option option;
private String value;
}
/**
* Representation of a command-line option
*/
public static abstract class Option {
protected Option( String longForm, boolean wantsValue ) {
this(null, longForm, wantsValue);
}
protected Option( char shortForm, String longForm,
boolean wantsValue ) {
this(new String(new char[]{shortForm}), longForm, wantsValue);
}
private Option( String shortForm, String longForm, boolean wantsValue ) {
if ( longForm == null )
throw new IllegalArgumentException("Null longForm not allowed");
this.shortForm = shortForm;
this.longForm = longForm;
this.wantsValue = wantsValue;
}
public String shortForm() { return this.shortForm; }
public String longForm() { return this.longForm; }
/**
* Tells whether or not this option wants a value
*/
public boolean wantsValue() { return this.wantsValue; }
public final Object getValue( String arg, Locale locale )
throws IllegalOptionValueException {
if ( this.wantsValue ) {
if ( arg == null ) {
throw new IllegalOptionValueException(this, "");
}
return this.parseValue(arg, locale);
}
else {
return Boolean.TRUE;
}
}
/**
* Override to extract and convert an option value passed on the
* command-line
*/
protected Object parseValue( String arg, Locale locale )
throws IllegalOptionValueException {
return null;
}
private String shortForm = null;
private String longForm = null;
private boolean wantsValue = false;
public static class BooleanOption extends Option {
public BooleanOption( char shortForm, String longForm ) {
super(shortForm, longForm, false);
}
public BooleanOption( String longForm ) {
super(longForm, false);
}
}
/**
* An option that expects an integer value
*/
public static class IntegerOption extends Option {
public IntegerOption( char shortForm, String longForm ) {
super(shortForm, longForm, true);
}
public IntegerOption( String longForm ) {
super(longForm, true);
}
protected Object parseValue( String arg, Locale locale )
throws IllegalOptionValueException {
try {
return new Integer(arg);
}
catch (NumberFormatException e) {
throw new IllegalOptionValueException(this, arg);
}
}
}
/**
* An option that expects a long integer value
*/
public static class LongOption extends Option {
public LongOption( char shortForm, String longForm ) {
super(shortForm, longForm, true);
}
public LongOption( String longForm ) {
super(longForm, true);
}
protected Object parseValue( String arg, Locale locale )
throws IllegalOptionValueException {
try {
return new Long(arg);
}
catch (NumberFormatException e) {
throw new IllegalOptionValueException(this, arg);
}
}
}
/**
* An option that expects a floating-point value
*/
public static class DoubleOption extends Option {
public DoubleOption( char shortForm, String longForm ) {
super(shortForm, longForm, true);
}
public DoubleOption( String longForm ) {
super(
没有合适的资源?快使用搜索试试~ 我知道了~
基于Java的实例源码-命令行处理类库 JArgs.zip
共108个文件
html:65个
class:22个
java:8个
0 下载量 71 浏览量
2023-06-30
22:10:03
上传
评论
收藏 198KB ZIP 举报
温馨提示
基于Java的实例源码-命令行处理类库 JArgs.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的实例源码-命令行处理类库 JArgs.zip (108个子文件)
CHANGES 2KB
CmdLineParserTestCase.class 7KB
CmdLineParser.class 6KB
AutoHelpParser.class 3KB
OptionTest.class 3KB
OptionParserSubclassTest.class 2KB
CmdLineParser$Option.class 2KB
CustomOptionTestCase.class 2KB
CustomOptionTest.class 2KB
CmdLineParser$Option$DoubleOption.class 1KB
OptionParserSubclassTest$MyOptionsParser.class 1KB
CustomOptionTestCase$ShortDateOption.class 1KB
CustomOptionTest$ShortDateOption.class 1KB
CmdLineParser$IllegalOptionValueException.class 1KB
AllTests.class 1KB
CmdLineParser$Option$IntegerOption.class 1KB
CmdLineParser$Option$LongOption.class 1KB
CmdLineParser$NotFlagException.class 997B
CmdLineParser$UnknownSuboptionException.class 997B
CmdLineParser$UnknownOptionException.class 995B
CmdLineParser$Option$StringOption.class 872B
CmdLineParser$Option$BooleanOption.class 689B
CmdLineParser$OptionException.class 453B
stylesheet.css 1KB
.cvsignore 48B
.cvsignore 25B
.cvsignore 4B
inherit.gif 57B
index-all.html 34KB
CmdLineParser.html 30KB
CmdLineParser.Option.html 25KB
CmdLineParser.Option.html 18KB
CmdLineParserTestCase.html 18KB
AutoHelpParser.html 15KB
CmdLineParser.Option.IntegerOption.html 15KB
CmdLineParser.Option.DoubleOption.html 15KB
CmdLineParser.Option.LongOption.html 15KB
CustomOptionTest.ShortDateOption.html 15KB
CmdLineParser.Option.StringOption.html 15KB
CustomOptionTestCase.ShortDateOption.html 14KB
CmdLineParser.IllegalOptionValueException.html 14KB
CustomOptionTestCase.html 13KB
CmdLineParser.Option.BooleanOption.html 13KB
CmdLineParser.IllegalOptionValueException.html 13KB
CmdLineParser.NotFlagException.html 11KB
CmdLineParser.UnknownSuboptionException.html 11KB
CmdLineParser.UnknownOptionException.html 11KB
CustomOptionTest.html 11KB
package-use.html 10KB
OptionParserSubclassTest.html 10KB
help-doc.html 10KB
serialized-form.html 10KB
CmdLineParser.UnknownOptionException.html 10KB
OptionTest.html 10KB
package-summary.html 10KB
AllTests.html 10KB
CmdLineParser.OptionException.html 10KB
CmdLineParser.OptionException.html 9KB
overview-tree.html 9KB
package-tree.html 8KB
CmdLineParser.html 8KB
package-summary.html 7KB
package-tree.html 7KB
package-summary.html 7KB
package-tree.html 7KB
CustomOptionTest.ShortDateOption.html 6KB
CustomOptionTestCase.ShortDateOption.html 6KB
CmdLineParser.UnknownSuboptionException.html 6KB
OptionParserSubclassTest.html 6KB
CmdLineParser.Option.IntegerOption.html 6KB
CmdLineParser.Option.BooleanOption.html 6KB
CmdLineParser.Option.DoubleOption.html 6KB
CmdLineParser.Option.StringOption.html 6KB
CmdLineParserTestCase.html 6KB
CmdLineParser.Option.LongOption.html 6KB
CustomOptionTest.html 6KB
CustomOptionTestCase.html 6KB
CmdLineParser.NotFlagException.html 6KB
overview-summary.html 6KB
AutoHelpParser.html 6KB
OptionTest.html 6KB
AllTests.html 6KB
package-use.html 6KB
package-use.html 6KB
constant-values.html 5KB
deprecated-list.html 5KB
allclasses-frame.html 3KB
allclasses-noframe.html 3KB
package-frame.html 3KB
package-frame.html 1KB
overview-frame.html 1KB
index.html 1KB
package-frame.html 1KB
jargs-test.jar 15KB
jargs.jar 11KB
CmdLineParser.java 17KB
CmdLineParserTestCase.java 9KB
OptionTest.java 5KB
AutoHelpParser.java 3KB
CustomOptionTestCase.java 2KB
共 108 条
- 1
- 2
资源评论
毕业课程设计
- 粉丝: 2257
- 资源: 1653
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功