1 /* Option.java - represent a command-line option
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.tools.getopt;
42 * This is the base class representing an option. An option can have a short
43 * form. This is a single character, like '-x'. An option can have a long form,
44 * like '--verbose'; if the parser is working in "long option only" mode, then a
45 * long flag has a single dash, like '-verbose'. Both a long and a short form
46 * may be specified; it is not valid to have neither. A description is mandatory
47 * for options; this is used to automatically generate '--help' output. An option
48 * which takes an argument and which has a short form can also be "joined", in
49 * this case the option's argument can either be separated, like "-I path" or
50 * joined with the short option name, like "-Ipath".
52 public abstract class Option
54 private char shortName;
56 private String longName;
58 private String description;
60 private String argumentName;
62 private boolean joined;
65 * Create a new option with the given short name and description.
67 * @param shortName the short name
68 * @param description the description
70 protected Option(char shortName, String description)
73 throw new IllegalArgumentException("short name must not be \\0");
74 this.shortName = shortName;
75 this.description = description;
79 * Create a new option with the given short name and description.
81 * @param shortName the short name
82 * @param description the description
83 * @param argumentName the descriptive name of the argument, if this option
84 * takes an argument; otherwise null
86 protected Option(char shortName, String description, String argumentName)
89 throw new IllegalArgumentException("short name must not be \\0");
90 this.shortName = shortName;
91 this.description = description;
92 this.argumentName = argumentName;
96 * Create a new option with the given short name and description.
98 * @param shortName the short name
99 * @param description the description
100 * @param argumentName the descriptive name of the argument, if this option
101 * takes an argument; otherwise null
102 * @param joined true if the short option is joined to its argument
104 protected Option(char shortName, String description, String argumentName,
108 throw new IllegalArgumentException("short name must not be \\0");
109 this.shortName = shortName;
110 this.description = description;
111 this.argumentName = argumentName;
112 this.joined = joined;
116 * Create a new option with the given long name and description. The long name
117 * should be specified without any leading dashes.
119 * @param longName the long name
120 * @param description the description
122 protected Option(String longName, String description)
124 this.longName = longName;
125 this.description = description;
129 * Create a new option with the given long name and description. The long name
130 * should be specified without any leading dashes.
132 * @param longName the long name
133 * @param description the description
134 * @param argumentName the descriptive name of the argument, if this option
135 * takes an argument; otherwise null
137 protected Option(String longName, String description, String argumentName)
139 this.longName = longName;
140 this.description = description;
141 this.argumentName = argumentName;
145 * Create a new option with the given short and long names and description.
146 * The long name should be specified without any leading dashes.
148 * @param longName the long name
149 * @param shortName the short name
150 * @param description the description
152 protected Option(String longName, char shortName, String description)
155 throw new IllegalArgumentException("short name must not be \\0");
156 this.shortName = shortName;
157 this.longName = longName;
158 this.description = description;
162 * Create a new option with the given short and long names and description.
163 * The long name should be specified without any leading dashes.
165 * @param longName the long name
166 * @param shortName the short name
167 * @param description the description
168 * @param argumentName the descriptive name of the argument, if this option
169 * takes an argument; otherwise null
171 protected Option(String longName, char shortName, String description,
175 throw new IllegalArgumentException("short name must not be \\0");
176 this.shortName = shortName;
177 this.longName = longName;
178 this.argumentName = argumentName;
179 this.description = description;
183 * Create a new option with the given short and long names and description.
184 * The long name should be specified without any leading dashes.
186 * @param longName the long name
187 * @param shortName the short name
188 * @param description the description
189 * @param argumentName the descriptive name of the argument, if this option
190 * takes an argument; otherwise null
191 * @param joined true if the short option is joined to its argument
193 protected Option(String longName, char shortName, String description,
194 String argumentName, boolean joined)
197 throw new IllegalArgumentException("short name must not be \\0");
198 this.shortName = shortName;
199 this.longName = longName;
200 this.argumentName = argumentName;
201 this.description = description;
202 this.joined = joined;
206 * Return the short name of the option, or \0 if none.
208 public char getShortName()
214 * Return the long name of the option, or null if none.
216 public String getLongName()
222 * Return true if the argument takes an option.
224 public boolean getTakesArgument()
226 return argumentName != null;
230 * Return the name of the argument. If the option does not take an argument,
233 public String getArgumentName()
239 * Return the description of the option.
241 public String getDescription()
247 * Return true if this is a "joined" option, false otherwise.
248 * Only the short form of an option can be joined; this will always
249 * return false for an option which does not have a short form.
251 public boolean isJoined()
257 * This is called by the parser when this option is recognized. It may be
258 * called multiple times during a single parse. If this option takes an
259 * argument, the argument will be passed in. Otherwise the argument will be
262 * @param argument the argument
263 * @throws OptionException if the option or its argument is somehow invalid
265 public abstract void parsed(String argument) throws OptionException;