--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/java/adr/org/opensolaris/os/adr/CLI.java Wed Mar 14 10:45:15 2012 -0400
@@ -0,0 +1,144 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ */
+
+package org.opensolaris.os.adr;
+
+import java.util.*;
+
+public class CLI {
+ //
+ // Inner classes
+ //
+
+ public static class Option {
+ private char letter_;
+ private String arg_;
+
+ public Option(char letter, String arg) {
+ letter_ = letter;
+ arg_ = arg;
+ }
+
+ public char getLetter() {
+ return letter_;
+ }
+
+ public String getArg() {
+ return arg_;
+ }
+ }
+
+ //
+ // Instance data
+ //
+
+ private Class mainClass;
+
+ //
+ // Constructors
+ //
+
+ public CLI(Class mainClass) {
+ this.mainClass = mainClass;
+ }
+
+ //
+ // CLI methods
+ //
+
+ public void die(int exit, String format, Object... args) {
+ if (format != null) {
+ System.err.printf("%s: ", getProg());
+ System.err.printf(format, args);
+ System.err.println();
+ }
+ System.exit(exit);
+ }
+
+ public int getOptions(String[] args, String argspec,
+ Map<Character, Option> opts) {
+
+ Map<Character, Boolean> optspecs = new HashMap<Character, Boolean>();
+
+ /* Parse argspec */
+ int length = argspec.length();
+ for (int i = 0; i < length; i++) {
+ char l = argspec.charAt(i);
+ boolean hasargs = i + 1 < length && argspec.charAt(i + 1) == ':';
+ if (hasargs)
+ i++;
+ optspecs.put(l, hasargs);
+ }
+
+ /* Parse args */
+ boolean inopt = false;
+ char pending = '\0';
+ int i;
+ for (i = 0; i < args.length; i++) {
+ if (inopt) {
+ opts.put(pending, new Option(pending, args[i]));
+ inopt = false;
+ continue;
+ }
+
+ if (args[i].length() == 0 || args[i].charAt(0) != '-')
+ break;
+
+ for (int j = 1; j < args[i].length(); j++) {
+ char l = args[i].charAt(j);
+ if (!optspecs.containsKey(l)) {
+ die(2, "invalid option: \"%c\"", l);
+ }
+
+ if (!optspecs.get(l)) {
+ opts.put(l, new Option(l, null));
+ continue;
+ }
+
+ if (j + 1 < args[i].length()) {
+ opts.put(l, new Option(l, args[i].substring(j + 1)));
+ break;
+ }
+
+ inopt = true;
+ pending = l;
+ }
+ }
+
+ if (inopt) {
+ die(2, "option \"%c\" requires an argument", pending);
+ }
+
+ return i;
+ }
+
+ private String getProg() {
+ String prog = System.getProperty("argv0");
+ if (prog == null) {
+ prog = "java " + mainClass.getName();
+ }
+ return prog;
+ }
+}