Merge "Bug 2347: DOMConcurrentDataCommitCoordinator uses wrong phase name"
[controller.git] / third-party / openflowj / src / main / java / org / openflow / example / cli / SimpleCLI.java
1 package org.openflow.example.cli;
2
3 import java.io.PrintStream;
4
5 /**
6  * Very basic command line interface
7  * 
8  * (really should be something in java.* for this; only implementing this to
9  * remove external dependencies)
10  * 
11  * Modeled after org.apache.common.cli .
12  * 
13  * @author Rob Sherwood (rob.sherwood@stanford.edu)
14  * 
15  */
16
17 public class SimpleCLI {
18
19     private static final String NAME_WIDTH = "-15";
20     private static final String VALUE_WIDTH = "-20";
21     private static final String FORMAT_STRING = "%1$" + NAME_WIDTH + "s%2$"
22             + VALUE_WIDTH + "s%3$s\n";
23     Options options;
24
25     int optind;
26
27     /**
28      * Need to use SimpleCLI.parse() instead
29      * 
30      * @param options
31      */
32
33     private SimpleCLI(Options options) {
34         this.options = options;
35     }
36
37     /**
38      * @return the index of the last parsed option
39      * 
40      *         Useful for finding options that don't start with "-" or "--"
41      */
42     public int getOptind() {
43         return optind;
44     }
45
46     /**
47      * @param optind
48      *            the optind to set
49      */
50     public void setOptind(int optind) {
51         this.optind = optind;
52     }
53
54     public boolean hasOption(String shortName) {
55         Option option = this.options.getOption(shortName);
56         if (option == null)
57             return false;
58         return option.specified;
59     }
60
61     public String getOptionValue(String shortName) {
62         Option option = this.options.getOption(shortName);
63         if (option == null)
64             return null;
65         if (!option.specified)
66             return option.defaultVal.toString();
67         else
68             return option.val;
69     }
70
71     public static SimpleCLI parse(Options options, String[] args)
72             throws ParseException {
73         SimpleCLI simpleCLI = new SimpleCLI(options);
74         int i;
75         for (i = 0; i < args.length; i++) {
76             if (!args[i].startsWith("-"))
77                 break; // not a short or long option
78             String optName = args[i].replaceFirst("^-*", ""); // remove leading
79             // "--"
80             Option option;
81             if (args[i].startsWith("--"))
82                 option = options.getOptionByLongName(optName);
83             else
84                 option = options.getOption(optName);
85             if (option == null)
86                 throw new ParseException("unknown option: " + optName);
87             option.specified = true;
88             if (option.needsArg()) {
89                 if ((i + 1) >= args.length)
90                     throw new ParseException("option " + optName
91                             + " requires an argument:: " + option.comment);
92                 option.val = args[i + 1];
93                 i++; // skip next element; we've parsed it
94             }
95         }
96         simpleCLI.setOptind(i);
97         return simpleCLI;
98     }
99
100     public static void printHelp(String canonicalName, Options options) {
101         printHelp(canonicalName, options, System.err);
102     }
103
104     private static void printHelp(String helpString, Options options,
105             PrintStream err) {
106         err.println(helpString);
107         err.format(FORMAT_STRING, "\toption", "type [default]", "usage");
108         for (Option option : options.getOptions()) {
109             String msg = "\t";
110             if (option.shortOpt != null)
111                 msg += "-" + option.shortOpt;
112             if (option.longOpt != null) {
113                 if (!msg.equals("\t"))
114                     msg += "|";
115                 msg += "--" + option.longOpt;
116             }
117             String val = "";
118             if (option.defaultVal != null)
119                 val += option.defaultVal.getClass().getSimpleName() + " ["
120                         + option.defaultVal.toString() + "]";
121             String comment;
122             if (option.comment != null)
123                 comment = option.comment;
124             else
125                 comment = "";
126
127             err.format(FORMAT_STRING, msg, val, comment);
128         }
129         err.println(""); // print blank line at the end, to look pretty
130     }
131
132 }