0N/A/*
1879N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/Aclass ArgIterator {
0N/A String[] args;
0N/A int i;
0N/A ArgIterator(String[] args) {
0N/A this.args = args;
0N/A this.i = 0;
0N/A }
0N/A String get() { return args[i]; }
0N/A boolean hasMore() { return args != null && i < args.length; }
0N/A boolean next() { return ++i < args.length; }
0N/A}
0N/A
0N/Aabstract class ArgHandler {
0N/A public abstract void handle(ArgIterator it);
0N/A
0N/A}
0N/A
0N/Aclass ArgRule {
0N/A String arg;
0N/A ArgHandler handler;
0N/A ArgRule(String arg, ArgHandler handler) {
0N/A this.arg = arg;
0N/A this.handler = handler;
0N/A }
0N/A
0N/A boolean process(ArgIterator it) {
0N/A if (match(it.get(), arg)) {
0N/A handler.handle(it);
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A boolean match(String rule_pattern, String arg) {
0N/A return arg.equals(rule_pattern);
0N/A }
0N/A}
0N/A
0N/Aclass ArgsParser {
0N/A ArgsParser(String[] args,
0N/A ArgRule[] rules,
0N/A ArgHandler defaulter) {
0N/A ArgIterator ai = new ArgIterator(args);
0N/A while (ai.hasMore()) {
0N/A boolean processed = false;
0N/A for (int i=0; i<rules.length; i++) {
0N/A processed |= rules[i].process(ai);
0N/A if (processed) {
0N/A break;
0N/A }
0N/A }
0N/A if (!processed) {
0N/A if (defaulter != null) {
0N/A defaulter.handle(ai);
0N/A } else {
0N/A System.err.println("ERROR: unparsed \""+ai.get()+"\"");
0N/A ai.next();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}