设为首页
收藏本站
用户名
Email
自动登录
找回密码
密码
登录
注册
快捷导航
平台
Portal
论坛
BBS
文库
项目
群组
Group
我的博客
Space
搜索
搜索
热搜:
shell
linux
mysql
本版
用户
LinuxSir.cn,穿越时空的Linuxsir!
»
论坛
›
编程开发讨论区 —— LinuxSir.cn
›
Linux 程序设计专题讨论
›
GetOpt类《java经典实例》
返回列表
查看:
3531
|
回复:
2
GetOpt类《java经典实例》
[复制链接]
fangshun
fangshun
当前离线
积分
661
IP卡
狗仔卡
发表于 2003-6-10 20:10:59
|
显示全部楼层
|
阅读模式
//package com.darwinsys.util;
/** A class to implement UNIX-style (single-character) command arguments
* @author Ian F. Darwin, [email]ian@darwinsys.com[/email]
* based on the standard UNIX getopt(3) program.
* @version $Id: GetOpt.java,v 1.11 2002/03/02 19:35:31 ian Exp $
*/
class GetOpt {
/** The set of characters to look for */
protected String pattern;
/** Where we are in the options */
protected int optind = 0;
/** Public constant for "no more options"
* XXX should switch to hasNext()/next() pattern.
*/
public static final int DONE = 0;
/** Internal flag - whether we are done all the options */
protected boolean done = false;
/** Retrieve the option index */
public int getOptInd() {
return optind;
}
/** The option argument, if there is one. */
protected String optarg;
/** Retrieve the current option argument */
public String optarg() {
return optarg;
}
/* Construct a GetOpt object, storing the set of option characters. */
public GetOpt(String patt) {
pattern = patt;
rewind();
}
public void rewind() {
done = false;
optind = 0;
}
/** Return one argument.
*/
public char getopt(String argv[]) {
if (optind == (argv.length)) {
done = true;
}
// Do not combine with previous if statement.
if (done) {
return DONE;
}
// Pick off the next command line argument, check if it starts "-".
// If so look it up in the list.
String thisArg = argv[optind++];
if (thisArg.startsWith("-")) {
optarg = null;
for (int i=0; i<pattern.length(); i++) {
char c = pattern.charAt(i);
if (thisArg.equals("-"+c)) { // we found it
// If it needs an option argument, get it.
if (i+1 < pattern.length() &&
pattern.charAt(i+1)==':' &&
optind < argv.length)
optarg = argv[optind++];
return c;
}
}
// Still no match, and not used all args, so must be error.
return '?';
} else {
// Found non-argument non-option word in argv: end of options.
optind--;
done = true;
return DONE;
}
}
}
public class GetOptSimple {
public static void main(String[] args) {
GetOpt go = new GetOpt("h:n:");
char c;
while ((c = go.getopt(args)) != 0) {
switch(c) {
case 'h':
helpAndExit(0);
break;
case 'n'
helpAndExit(0);
break;
default:
System.err.println("Unknown option in " + args[go.getOptInd()]);
helpAndExit(1);
}
}
System.out.println();
}
static void helpAndExit(int returnValue) {
System.err.println("This would tell you how to use this program");
System.exit(returnValue);
}
}
复制代码
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
显身卡
kj501
kj501
当前离线
积分
5317
IP卡
狗仔卡
发表于 2003-6-11 07:25:35
|
显示全部楼层
呵呵,不错。以后贴代码时要注意保持缩进。此外,粘贴的代码要发新贴,不要在置顶的贴子后面跟贴,以便于查找。
回复
支持
反对
使用道具
举报
显身卡
fangshun
fangshun
当前离线
积分
661
IP卡
狗仔卡
楼主
|
发表于 2003-6-11 11:02:18
|
显示全部楼层
知道了,下次注意!!呵呵!
回复
支持
反对
使用道具
举报
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2002-2023
LinuxSir.cn
(http://www.linuxsir.cn/) 版权所有 All Rights Reserved.
Powered by
RedflagLinux!
技术支持:
中科红旗
|
京ICP备19024520号
快速回复
返回顶部
返回列表