📄 argparser.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/util/ArgParser.java,v $// $RCSfile: ArgParser.java,v $// $Revision: 1.2.2.4 $// $Date: 2005/08/24 20:16:58 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.util;import java.util.Vector;/** * A simple class to manage the line arguments of a program. Takes the * String[] argv that is provided to the main method of a class, and * separates them out, depending on the options given to the * ArgParser. After you create the parser, add your options that you * want. */public class ArgParser { /** * The length to submit if you want a variable length list at the * end of the command line, like all the arguments left over. */ public final static int TO_END = -1; /** The program name that's using the parser. */ protected String programName; /** The Args that the parser is looking for. */ protected Vector args; /** The String array that holds all of the leftover argvs. */ protected String[] rest = new String[0]; /** The character flag for an option. */ protected char option = '-'; /** * Tells the Args to accept the first letter of their name for * argv options specified with one letter. */ protected boolean allowAbbr = true; /** * Create a parser for the named program. Automatically adds the * -help option. * * @param pName the program name. */ public ArgParser(String pName) { programName = pName; args = new Vector(); args.add(new HelpArg()); } /** * Add a argument to the parser. Don't include the '-' in the * argName, that's added automatically. Assumes that the option * expects no arguments. * * @param argName the command line option * @param desc a help line description. */ public void add(String argName, String desc) { add(argName, desc, 0); } /** * Add a argument to the parser. Don't include the '-' in the * argName, that's added automatically. * * @param argName the command line option * @param desc a help line description. * @param expectedNumberOfArguments the number of option * parameters expected for this option. */ public void add(String argName, String desc, int expectedNumberOfArguments) { add(argName, desc, expectedNumberOfArguments, false); } /** * Add a argument to the parser. Don't include the '-' in the * argName, that's added automatically. * * @param argName the command line option * @param desc a help line description. * @param expectedNumberOfArguments the number of option * parameters expected for this option. * @param expectDashedArguments tell the parser that this option * may have arguments that may start with dashes, for * instance, a negative number. False by default. */ public void add(String argName, String desc, int expectedNumberOfArguments, boolean expectDashedArguments) { Arg newArg = new Arg(argName, desc, expectedNumberOfArguments, expectDashedArguments); args.add(newArg); if (Debug.debugging("parse")) { Debug.output("ArgParser: adding " + argName); } } /** * Parse and organize the array of Strings. If something goes * wrong, bail() may be called. * * @return true if everything goes well, false if not. */ public boolean parse(String[] argv) { try { if (argv == null || argv.length == 0) { return false; } for (int i = 0; i < argv.length; i++) { boolean hit = false; if (argv[i].charAt(0) == option) { String eval = argv[i].substring(1); for (int j = 0; j < args.size(); j++) { Arg curArg = (Arg) args.elementAt(j); if (curArg.is(eval, allowAbbr)) { if (Debug.debugging("parse")) { Debug.output("ArgParser: arg " + curArg.name + " reading values."); } if (!curArg.readArgs(argv, ++i)) { // Something's wrong with the // arguments. bail("ArgParser: Unexpected arguments with option " + curArg.name + ".", true); } hit = true; if (curArg.numExpectedValues != TO_END) { i += (curArg.numExpectedValues - 1); } else { i = argv.length; } } } if (hit == false) { //option flagged, but option unknown. bail(programName + ": unknown option " + argv[i], false); } } if (hit == false) { if (i == 0) { rest = argv; } else { int diff = argv.length - i; rest = new String[diff]; for (int k = 0; k < diff; k++) { rest[k] = argv[i + k]; if (rest[k].charAt(0) == option) { bail("ArgParser: Not expecting option in list of arguments.", true); } } } if (Debug.debugging("parse")) { Debug.output("ArgParser: adding " + rest.length + " strings to the leftover list."); } return true; } } } catch (ArrayIndexOutOfBoundsException aioobe) { bail("Expecting more arguments for option", true); } catch (NegativeArraySizeException nase) { return false; } return true; } /** * Called if something is messed up. Prints a message, and the * usage statement, if desired. * * @param message a message to display. * @param printUsageStatement true to display a list of available * options. */ public void bail(String message, boolean printUsageStatement) { Debug.output(message); if (printUsageStatement) printUsage(); System.exit(0); } /** * Tell the parser to accept first-letter representations of the * options. */ public void setAllowAbbr(boolean set) { allowAbbr = set; } /** * Tells whether the parser accepts first-letter representations * of the options. */ public boolean getAllowAbbr() { return allowAbbr; } /** * Returns a Vector of Arg objects. */ public Vector getArgs() { return args; } /** * Return a Arg object with a particular name. */ public Arg getArg(String name) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -