LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3529|回复: 2

GetOpt类《java经典实例》

[复制链接]
发表于 2003-6-10 20:10:59 | 显示全部楼层 |阅读模式

  1. //package com.darwinsys.util;

  2. /** A class to implement UNIX-style (single-character) command arguments
  3. * @author Ian F. Darwin, [email]ian@darwinsys.com[/email]
  4. * based on the standard UNIX getopt(3) program.
  5. * @version $Id: GetOpt.java,v 1.11 2002/03/02 19:35:31 ian Exp $
  6. */
  7. class GetOpt {
  8.         /** The set of characters to look for */
  9.         protected String pattern;
  10.         /** Where we are in the options */
  11.         protected int optind = 0;
  12.         /** Public constant for "no more options"
  13.          * XXX should switch to hasNext()/next() pattern.
  14.          */
  15.         public static final int DONE = 0;
  16.         /** Internal flag - whether we are done all the options */
  17.         protected boolean done = false;

  18.         /** Retrieve the option index */
  19.         public int getOptInd() {
  20.                 return optind;
  21.         }

  22.         /** The option argument, if there is one. */
  23.         protected String optarg;

  24.         /** Retrieve the current option argument */
  25.         public String optarg() {
  26.                 return optarg;
  27.         }

  28.         /* Construct a GetOpt object, storing the set of option characters. */
  29.         public GetOpt(String patt) {
  30.                 pattern = patt;
  31.                 rewind();
  32.         }

  33.         public void rewind() {
  34.                 done = false;
  35.                 optind = 0;
  36.         }

  37.         /** Return one argument.
  38.          */
  39.    
  40.         public char getopt(String argv[]) {
  41.                 if (optind == (argv.length)) {
  42.                         done = true;
  43.                 }

  44.                 // Do not combine with previous if statement.
  45.                 if (done) {
  46.                         return DONE;
  47.                 }

  48.                 // Pick off the next command line argument, check if it starts "-".
  49.                 // If so look it up in the list.
  50.                 String thisArg = argv[optind++];

  51.                 if (thisArg.startsWith("-")) {
  52.                         optarg = null;
  53.                         for (int i=0; i<pattern.length(); i++) {
  54.                                 char c = pattern.charAt(i);
  55.                                 if (thisArg.equals("-"+c)) {        // we found it
  56.                                         // If it needs an option argument, get it.
  57.                                         if (i+1 < pattern.length() &&
  58.                                                 pattern.charAt(i+1)==':' &&
  59.                                                 optind < argv.length)
  60.                                                 optarg = argv[optind++];
  61.                                         return c;
  62.                                        
  63.                                 }
  64.                         }
  65.                         // Still no match, and not used all args, so must be error.
  66.                         return '?';
  67.                 } else {
  68.                         // Found non-argument non-option word in argv: end of options.
  69.                         optind--;
  70.                         done = true;
  71.                         return DONE;
  72.                 }
  73.         }
  74. }
  75. public class GetOptSimple {
  76.     public static void main(String[] args) {
  77.     GetOpt go = new GetOpt("h:n:");
  78.     char c;
  79.     while ((c = go.getopt(args)) != 0) {
  80.         switch(c) {
  81.         case 'h':
  82.             helpAndExit(0);
  83.             break;
  84.             case 'n'
  85.             helpAndExit(0);
  86.             break;
  87.         default:
  88.             System.err.println("Unknown option in " + args[go.getOptInd()]);
  89.             helpAndExit(1);
  90.         }
  91.     }
  92.     System.out.println();
  93.     }


  94. static void helpAndExit(int returnValue) {
  95.     System.err.println("This would tell you how to use this program");
  96.     System.exit(returnValue);
  97. }
  98. }
复制代码
发表于 2003-6-11 07:25:35 | 显示全部楼层
呵呵,不错。以后贴代码时要注意保持缩进。此外,粘贴的代码要发新贴,不要在置顶的贴子后面跟贴,以便于查找。
 楼主| 发表于 2003-6-11 11:02:18 | 显示全部楼层
知道了,下次注意!!呵呵!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表