Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/tools/ProjectCreator/ArgsParser.java
32285 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324class ArgIterator {25String[] args;26int i;27ArgIterator(String[] args) {28this.args = args;29this.i = 0;30}31String get() { return args[i]; }32boolean hasMore() { return args != null && i < args.length; }33boolean next() { return ++i < args.length; }34}3536abstract class ArgHandler {37public abstract void handle(ArgIterator it);3839}4041class ArgRule {42String arg;43ArgHandler handler;44ArgRule(String arg, ArgHandler handler) {45this.arg = arg;46this.handler = handler;47}4849boolean process(ArgIterator it) {50if (match(it.get(), arg)) {51handler.handle(it);52return true;53}54return false;55}56boolean match(String rule_pattern, String arg) {57return arg.equals(rule_pattern);58}59}6061class ArgsParser {62ArgsParser(String[] args,63ArgRule[] rules,64ArgHandler defaulter) {65ArgIterator ai = new ArgIterator(args);66while (ai.hasMore()) {67boolean processed = false;68for (int i=0; i<rules.length; i++) {69processed |= rules[i].process(ai);70if (processed) {71break;72}73}74if (!processed) {75if (defaulter != null) {76defaulter.handle(ai);77} else {78System.err.println("ERROR: unparsed \""+ai.get()+"\"");79ai.next();80}81}82}83}84}858687