⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 argparser.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        for (int i = 0; i < args.size(); i++) {            ArgParser.Arg arg = (ArgParser.Arg) args.elementAt(i);            if (name.equalsIgnoreCase(arg.name)) {                return arg;            }        }        return null;    }    /**     * Given an Arg name, return the values. Returns a zero length     * array (non-null) value for options that don't require     * arguments. Returns null if the option name wasn't found in the     * list, or if the option wasn't chosen in the parsed array of     * Strings.     */    public String[] getArgValues(String name) {        for (int i = 0; i < args.size(); i++) {            ArgParser.Arg arg = (ArgParser.Arg) args.elementAt(i);            if (name.equalsIgnoreCase(arg.name)) {                if (arg.flagged) {                    return arg.values;                }            }        }        return null;    }    /**     * Get the String[] that makes up the trailing Strings after the     * options were parsed.     */    public String[] getRest() {        return rest;    }    /**     * Print a list of options added to the parser.     */    public void printUsage() {        Debug.output(programName + " Arguments:");        for (int i = 0; i < args.size(); i++) {            ArgParser.Arg arg = (ArgParser.Arg) args.elementAt(i);            StringBuffer sb = new StringBuffer();            String filler = arg.name.length() < 6?"\t\t":"\t";            sb.append("  -" + arg.name + filler + arg.description);            if (arg.numExpectedValues == TO_END) {                sb.append(" (Variable number of arguments expected)");            } else if (arg.numExpectedValues == 1) {                sb.append(" (1 argument expected)");            } else {                sb.append(" (" + arg.numExpectedValues + " arguments expected)");            }            Debug.output(sb.toString());        }    }    public static void main(String[] argv) {        Debug.init();        ArgParser ap = new ArgParser("ArgParser");        ap.add("first", "First test argument, no parameters expected");        ap.add("second", "Second test argument, two parameters expected", 2);        ap.add("third", "Third test argument, no parameters expected");        ap.add("fourth", "Fourth test argument, one parameter expected", 1);        if (!ap.parse(argv)) {            ap.printUsage();            System.exit(0);        }        int i;        Vector args = ap.getArgs();        for (i = 0; i < args.size(); i++) {            ArgParser.Arg a = (ArgParser.Arg) args.elementAt(i);            Debug.output(a.toString());        }        String[] rest = ap.getRest();        Debug.output("Rest:");        for (i = 0; i < rest.length; i++) {            Debug.output(rest[i]);        }    }    /**     * A default version of the Arg class used to represent options     * for the ArgParser to use.     */    public class Arg {        public String name;        public String description;        public int numExpectedValues;        public String[] values = null;        public char c;        public boolean flagged = false;        public boolean dashedArguments = false;        /**         * Create an Arg with a name and help line description.         */        public Arg(String aName, String desc) {            this(aName, desc, 0);        }        /**         * Create an Arg with a name and help line description, along         * with a number of expected arguments to follow this option.         */        public Arg(String aName, String desc, int expectedNumberOfArguments) {            this(aName, desc, expectedNumberOfArguments, false);        }        /**         * Create an Arg with a name and help line description, along         * with a number of expected arguments to follow this option.         * Has an argument to not check for arguments that may start         * with dashes, in case one of the arguements may be a         * negative number.         */        public Arg(String aName, String desc, int expectedNumberOfArguments,                boolean expectDashedArguments) {            name = aName;            description = desc;            numExpectedValues = expectedNumberOfArguments;            c = name.charAt(0);            dashedArguments = expectDashedArguments;        }        /**         * Returns true if the atg string matches the name of the Arg,         * or, if allowAbbr is true, returns true if the arg length is         * one and it matches the first letter of the arg name.         */        public boolean is(String arg, boolean allowAbbr) {            if (name.equalsIgnoreCase(arg)) {                return true;            }            if (allowAbbr && arg.length() == 1) {                if (arg.charAt(0) == c) {                    return true;                }            }            return false;        }        /**         * Runs through the array of Strings, starting at the         * argIndex, and creates the values array from it. Uses the         * expected number of arguments to tell when it's done.         * Returns true if everything happens as expected.         *          * @param argv the entire array passed to the parser.         * @param argIndex the index of the first option argument         *        value.         * @return true if what was read was what was expected.         */        public boolean readArgs(String[] argv, int argIndex)                throws ArrayIndexOutOfBoundsException,                NegativeArraySizeException {            if (numExpectedValues != TO_END) {                values = new String[numExpectedValues];            } else {                values = new String[argv.length - argIndex];            }            for (int i = 0; i < values.length; i++) {                values[i] = argv[argIndex + i];                if (values[i].charAt(0) == option && !dashedArguments) {                    if (numExpectedValues != TO_END) {                        Debug.output("ArgParser: Option "                                + name                                + " expects "                                + numExpectedValues                                + (numExpectedValues == 1 ? " argument."                                        : " arguments."));                    } else {                        Debug.output("ArgParser: Option " + name                                + " not expecting options after its values.");                    }                    return false; // Unexpected argument.                }            }            flagged = true;            return true;        }        public String toString() {            StringBuffer sb = new StringBuffer();            sb.append("Arg: " + name + " expects " + numExpectedValues                    + (numExpectedValues == 1 ? " value.\n" : " values.\n"));            if (values != null) {                sb.append("Values: ");                for (int i = 0; i < values.length; i++) {                    sb.append("[" + values[i] + "]");                }                sb.append("\n");            }            return sb.toString();        }    }    /**     * A Arg class to spur off help messages. Gets added automatically     * to the parser.     */    public class HelpArg extends ArgParser.Arg {        public HelpArg() {            super("help", "Print usage statement, with arguments.", 0);        }        public boolean is(String arg, boolean allowAbbr) {            boolean askingForHelp = super.is(arg, allowAbbr);            if (askingForHelp) {                ArgParser.this.bail("", true);            }            return false;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -