source: other-projects/gs2-java-client/trunk/gnu/getopt/LongOpt.java@ 21525

Last change on this file since 21525 was 2090, checked in by paynter, 23 years ago

GNU Getopts code.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/**************************************************************************
2/* LongOpt.java -- Long option object for Getopt
3/*
4/* Copyright (c) 1998 by Aaron M. Renn ([email protected])
5/*
6/* This program is free software; you can redistribute it and/or modify
7/* it under the terms of the GNU Library General Public License as published
8/* by the Free Software Foundation; either version 2 of the License or
9/* (at your option) any later version.
10/*
11/* This program 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
14/* GNU Library General Public License for more details.
15/*
16/* You should have received a copy of the GNU Library General Public License
17/* along with this program; see the file COPYING.LIB. If not, write to
18/* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
19/* Boston, MA 02111-1307 USA
20/**************************************************************************/
21
22package gnu.getopt;
23
24import java.util.Locale;
25import java.util.ResourceBundle;
26import java.util.PropertyResourceBundle;
27import java.text.MessageFormat;
28
29/**************************************************************************/
30
31/**
32 * This object represents the definition of a long option in the Java port
33 * of GNU getopt. An array of LongOpt objects is passed to the Getopt
34 * object to define the list of valid long options for a given parsing
35 * session. Refer to the getopt documentation for details on the
36 * format of long options.
37 *
38 * @version 1.0.5
39 * @author Aaron M. Renn ([email protected])
40 *
41 * @see Getopt
42 */
43public class LongOpt extends Object
44{
45
46/**************************************************************************/
47
48/*
49 * Class Variables
50 */
51
52/**
53 * Constant value used for the "has_arg" constructor argument. This
54 * value indicates that the option takes no argument.
55 */
56public static final int NO_ARGUMENT = 0;
57
58/**
59 * Constant value used for the "has_arg" constructor argument. This
60 * value indicates that the option takes an argument that is required.
61 */
62public static final int REQUIRED_ARGUMENT = 1;
63
64/**
65 * Constant value used for the "has_arg" constructor argument. This
66 * value indicates that the option takes an argument that is optional.
67 */
68public static final int OPTIONAL_ARGUMENT = 2;
69
70/**************************************************************************/
71
72/*
73 * Instance Variables
74 */
75
76/**
77 * The name of the long option
78 */
79protected String name;
80
81/**
82 * Indicates whether the option has no argument, a required argument, or
83 * an optional argument.
84 */
85protected int has_arg;
86
87/**
88 * If this variable is not null, then the value stored in "val" is stored
89 * here when this long option is encountered. If this is null, the value
90 * stored in "val" is treated as the name of an equivalent short option.
91 */
92protected StringBuffer flag;
93
94/**
95 * The value to store in "flag" if flag is not null, otherwise the
96 * equivalent short option character for this long option.
97 */
98protected int val;
99
100/**
101 * Localized strings for error messages
102 */
103private ResourceBundle _messages = PropertyResourceBundle.getBundle(
104 "gnu/getopt/MessagesBundle", Locale.getDefault());
105
106/**************************************************************************/
107
108/*
109 * Constructors
110 */
111
112/**
113 * Create a new LongOpt object with the given parameter values. If the
114 * value passed as has_arg is not valid, then an exception is thrown.
115 *
116 * @param name The long option String.
117 * @param has_arg Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT).
118 * @param flag If non-null, this is a location to store the value of "val" when this option is encountered, otherwise "val" is treated as the equivalent short option character.
119 * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null.
120 *
121 * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT.
122 */
123public
124LongOpt(String name, int has_arg,
125 StringBuffer flag, int val) throws IllegalArgumentException
126{
127 // Validate has_arg
128 if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT)
129 && (has_arg != OPTIONAL_ARGUMENT))
130 {
131 Object[] msgArgs = { new Integer(has_arg).toString() };
132 throw new IllegalArgumentException(MessageFormat.format(
133 _messages.getString("getopt.invalidValue"), msgArgs));
134 }
135
136 // Store off values
137 this.name = name;
138 this.has_arg = has_arg;
139 this.flag = flag;
140 this.val = val;
141}
142
143/**************************************************************************/
144
145/**
146 * Returns the name of this LongOpt as a String
147 *
148 * @return Then name of the long option
149 */
150public String
151getName()
152{
153 return(name);
154}
155
156/**************************************************************************/
157
158/**
159 * Returns the value set for the 'has_arg' field for this long option
160 *
161 * @return The value of 'has_arg'
162 */
163public int
164getHasArg()
165{
166 return(has_arg);
167}
168
169/**************************************************************************/
170
171/**
172 * Returns the value of the 'flag' field for this long option
173 *
174 * @return The value of 'flag'
175 */
176public StringBuffer
177getFlag()
178{
179 return(flag);
180}
181
182/**
183 * Returns the value of the 'val' field for this long option
184 *
185 * @return The value of 'val'
186 */
187public int
188getVal()
189{
190 return(val);
191}
192
193/**************************************************************************/
194
195} // Class LongOpt
196
Note: See TracBrowser for help on using the repository browser.