OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / getopt / Option.java
1 /* Option.java - represent a command-line option
2  Copyright (C) 2006 Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
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)
9  any later version.
10
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.
15
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
19  02110-1301 USA.
20
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
24  combination.
25
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. */
37
38
39 package gnu.classpath.tools.getopt;
40
41 /**
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".
51  */
52 public abstract class Option
53 {
54   private char shortName;
55
56   private String longName;
57
58   private String description;
59
60   private String argumentName;
61
62   private boolean joined;
63
64   /**
65    * Create a new option with the given short name and description.
66    * 
67    * @param shortName the short name
68    * @param description the description
69    */
70   protected Option(char shortName, String description)
71   {
72     if (shortName == 0)
73       throw new IllegalArgumentException("short name must not be \\0");
74     this.shortName = shortName;
75     this.description = description;
76   }
77
78   /**
79    * Create a new option with the given short name and description.
80    * 
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
85    */
86   protected Option(char shortName, String description, String argumentName)
87   {
88     if (shortName == 0)
89       throw new IllegalArgumentException("short name must not be \\0");
90     this.shortName = shortName;
91     this.description = description;
92     this.argumentName = argumentName;
93   }
94
95   /**
96    * Create a new option with the given short name and description.
97    * 
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
103    */
104   protected Option(char shortName, String description, String argumentName,
105                    boolean joined)
106   {
107     if (shortName == 0)
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;
113   }
114
115   /**
116    * Create a new option with the given long name and description. The long name
117    * should be specified without any leading dashes.
118    * 
119    * @param longName the long name
120    * @param description the description
121    */
122   protected Option(String longName, String description)
123   {
124     this.longName = longName;
125     this.description = description;
126   }
127
128   /**
129    * Create a new option with the given long name and description. The long name
130    * should be specified without any leading dashes.
131    * 
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
136    */
137   protected Option(String longName, String description, String argumentName)
138   {
139     this.longName = longName;
140     this.description = description;
141     this.argumentName = argumentName;
142   }
143
144   /**
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.
147    * 
148    * @param longName the long name
149    * @param shortName the short name
150    * @param description the description
151    */
152   protected Option(String longName, char shortName, String description)
153   {
154     if (shortName == 0)
155       throw new IllegalArgumentException("short name must not be \\0");
156     this.shortName = shortName;
157     this.longName = longName;
158     this.description = description;
159   }
160
161   /**
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.
164    * 
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
170    */
171   protected Option(String longName, char shortName, String description,
172                    String argumentName)
173   {
174     if (shortName == 0)
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;
180   }
181
182   /**
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.
185    * 
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
192    */
193   protected Option(String longName, char shortName, String description,
194                    String argumentName, boolean joined)
195   {
196     if (shortName == 0)
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;
203   }
204
205   /**
206    * Return the short name of the option, or \0 if none.
207    */
208   public char getShortName()
209   {
210     return shortName;
211   }
212
213   /**
214    * Return the long name of the option, or null if none.
215    */
216   public String getLongName()
217   {
218     return longName;
219   }
220
221   /**
222    * Return true if the argument takes an option.
223    */
224   public boolean getTakesArgument()
225   {
226     return argumentName != null;
227   }
228
229   /**
230    * Return the name of the argument. If the option does not take an argument,
231    * returns null.
232    */
233   public String getArgumentName()
234   {
235     return argumentName;
236   }
237
238   /**
239    * Return the description of the option.
240    */
241   public String getDescription()
242   {
243     return description;
244   }
245
246   /**
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.
250    */
251   public boolean isJoined()
252   {
253     return joined;
254   }
255
256   /**
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
260    * null.
261    * 
262    * @param argument the argument
263    * @throws OptionException if the option or its argument is somehow invalid
264    */
265   public abstract void parsed(String argument) throws OptionException;
266 }