Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / util / Option.java
blobff3817e448e003a2204da008bbc22564f731a9cd
1 // Copyright 2008 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.util;
5 import com.google.common.collect.ImmutableList;
6 import com.google.common.collect.Iterables;
8 import java.util.LinkedList;
9 import java.util.List;
11 /**
12 * A command line option. There may be multiple command line options for a single
13 * command line. Each option may be represented by a "short" name or a "long" name.
14 * A short option is prefixed by a single dash ("-"), and its value follows in a separate
15 * argument. For example, <pre>
16 * -e foo@example.com
17 * </pre>
18 * A long option is prefixed by a double dash ("--"), and its value is specified with an
19 * equals sign ("="). For example, <pre>
20 * --email=foo@example.com
21 * </pre>
22 * Flag-style {@code Options} have no specified value, and imply their meaning by mere
23 * presence. For example, <pre>
24 * --append (Forces appending to an existing file)</pre>
27 public abstract class Option {
29 public enum Style {
30 Short,
31 Long
34 private final String shortName;
36 private final String longName;
38 private final boolean isFlag;
40 private Style style;
42 private List<String> values;
44 /**
45 * Creates a new {@code Option}. While both {@code shortName} and
46 * {@code longName} are optional, one must be supplied.
48 * @param shortName The short name to support. May be {@code null}.
49 * @param longName The long name to support. May be {@code null}.
50 * @param isFlag true to indicate that the Option represents a boolean value.
52 public Option(String shortName, String longName, boolean isFlag) {
53 this.shortName = shortName;
54 this.longName = longName;
55 this.isFlag = isFlag;
56 this.values = new LinkedList<String>();
59 /**
60 * Returns true if the {@code Option} represents a boolean value.
61 * Flags are always specified using one single argument, for example,
62 * "-h" or "--help". They are never of the form, "-h true" or "--help=true".
64 public boolean isFlag() {
65 return isFlag;
68 /**
69 * Returns the {@code Style} in which the {@code Option} was supplied
70 * on the command line.
72 public Style getArgStyle() {
73 return style;
76 /**
77 * Returns the value specified for the {@code Option}. If the option was
78 * specified on the command line multiple times, the values from the last
79 * option will be returned (i.e. for the command line "--option=1 --option=2"
80 * this method will return "2").
82 public String getValue() {
83 return Iterables.getLast(values, null);
86 /**
87 * Returns all the values that were specified for the {@code Option} in the
88 * order they were specified on the command line. So for the command line
89 * "--option=1 --option=2" this method will return a List that has "1" at
90 * index 0 and "2" at index 1.
92 public List<String> getValues() {
93 return values;
96 public String getLongName() {
97 return longName;
100 @Override
101 public String toString() {
102 return "Option{" + "shortName='" + shortName + '\'' + ", longName='" + longName + '\''
103 + ", isFlag=" + isFlag + ", style=" + style + ", values='" + values + '\'' + '}';
107 * Reads the value parsed for the {@code Option} and applies the
108 * appropriate logic.
110 * This method will be called exactly one time (even if the option was
111 * specified on the command line multiple times). If the value is singular,
112 * the method can call {@link Option.getValue()} to get the appropriate
113 * value; if the value is multiple, the method can call {@link
114 * Option.getValues()} to get all the values.
116 * @throws IllegalArgumentException if the parsed value is invalid.
118 public abstract void apply();
120 public List<String> getHelpLines() {
121 return ImmutableList.<String>of();
124 boolean parse(String[] args, int currentArg) {
125 String argVal = args[currentArg];
127 if (shortName != null) {
128 if (argVal.equals("-" + shortName)) {
129 if (isFlag()) {
130 return true;
132 if (currentArg + 1 == args.length) {
133 throw new IllegalArgumentException(shortName + " requires an argument.\n");
135 values.add(args[currentArg + 1]);
136 style = Style.Short;
137 return true;
141 if (longName != null) {
142 if (isFlag()) {
143 return argVal.equals("--" + longName);
145 if (!argVal.startsWith("--" + longName + "=")) {
146 return false;
148 String[] tokens = argVal.split("=", 2);
149 if (tokens.length == 1) {
150 throw new IllegalArgumentException(
151 longName + " requires an argument, for example, \"" + longName + "=FOO\"\n");
153 values.add(tokens[1]);
154 style = Style.Long;
155 return true;
158 return false;