263b29ca42dde8a21cd066b0bc8f2d6a5457a025
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / example / cli / SimpleCLI.java
1 package org.openflow.codec.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$" + VALUE_WIDTH + "s%3$s\n";
22     Options options;
23
24     int optind;
25
26     /**
27      * Need to use SimpleCLI.parse() instead
28      *
29      * @param options
30      */
31
32     private SimpleCLI(Options options) {
33         this.options = options;
34     }
35
36     /**
37      * @return the index of the last parsed option
38      *
39      *         Useful for finding options that don't start with "-" or "--"
40      */
41     public int getOptind() {
42         return optind;
43     }
44
45     /**
46      * @param optind
47      *            the optind to set
48      */
49     public void setOptind(int optind) {
50         this.optind = optind;
51     }
52
53     public boolean hasOption(String shortName) {
54         Option option = this.options.getOption(shortName);
55         if (option == null)
56             return false;
57         return option.specified;
58     }
59
60     public String getOptionValue(String shortName) {
61         Option option = this.options.getOption(shortName);
62         if (option == null)
63             return null;
64         if (!option.specified)
65             return option.defaultVal.toString();
66         else
67             return option.val;
68     }
69
70     public static SimpleCLI parse(Options options, String[] args) throws ParseException {
71         SimpleCLI simpleCLI = new SimpleCLI(options);
72         int i;
73         for (i = 0; i < args.length; i++) {
74             if (!args[i].startsWith("-"))
75                 break; // not a short or long option
76             String optName = args[i].replaceFirst("^-*", ""); // remove leading
77             // "--"
78             Option option;
79             if (args[i].startsWith("--"))
80                 option = options.getOptionByLongName(optName);
81             else
82                 option = options.getOption(optName);
83             if (option == null)
84                 throw new ParseException("unknown option: " + optName);
85             option.specified = true;
86             if (option.needsArg()) {
87                 if ((i + 1) >= args.length)
88                     throw new ParseException("option " + optName + " requires an argument:: " + option.comment);
89                 option.val = args[i + 1];
90                 i++; // skip next element; we've parsed it
91             }
92         }
93         simpleCLI.setOptind(i);
94         return simpleCLI;
95     }
96
97     public static void printHelp(String canonicalName, Options options) {
98         printHelp(canonicalName, options, System.err);
99     }
100
101     private static void printHelp(String helpString, Options options, PrintStream err) {
102         err.println(helpString);
103         err.format(FORMAT_STRING, "\toption", "type [default]", "usage");
104         for (Option option : options.getOptions()) {
105             String msg = "\t";
106             if (option.shortOpt != null)
107                 msg += "-" + option.shortOpt;
108             if (option.longOpt != null) {
109                 if (!msg.equals("\t"))
110                     msg += "|";
111                 msg += "--" + option.longOpt;
112             }
113             String val = "";
114             if (option.defaultVal != null)
115                 val += option.defaultVal.getClass().getSimpleName() + " [" + option.defaultVal.toString() + "]";
116             String comment;
117             if (option.comment != null)
118                 comment = option.comment;
119             else
120                 comment = "";
121
122             err.format(FORMAT_STRING, msg, val, comment);
123         }
124         err.println(""); // print blank line at the end, to look pretty
125     }
126
127 }