8000 configurable parser support added, PosixParser added · apache/commons-cli@4cca25d · GitHub
Skip to content

Commit 4cca25d

Browse files
author
John Keyes
committed
configurable parser support added, PosixParser added
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129770 13f79535-47bb-0310-9956-ffa450edef68
1 parent 809bd30 commit 4cca25d

16 files changed

Lines changed: 534 additions & 315 deletions

src/java/org/apache/commons/cli/CommandLine.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,17 @@
6969
/** <p>Represents list of arguments parsed against
7070
* a {@link Options} descriptor.<p>
7171
*
72-
* <p>It allows querying of a boolean {@link #hasOption(char opt)},
73-
* in addition to retrieving the {@link #getOptionValue(char opt)}
72+
* <p>It allows querying of a boolean {@link #hasOption(String opt)},
73+
* in addition to retrieving the {@link #getOptionValue(String opt)}
7474
* for options requiring arguments.</p>
7575
*
7676
* <p>Additionally, any left-over or unrecognized arguments,
7777
* are available for further processing.</p>
7878
*
7979
* @author bob mcwhirter (bob @ werken.com)
8080
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
81+
* @author John Keyes (jbjk at mac.com)
82+
*
8183
* @version $Revision: 1.4 $
8284
*/
8385
public class CommandLine {
@@ -102,13 +104,13 @@ public class CommandLine {
102104
* @param opt Short single-character name of the option
103105
* @return true if set, false if not
104106
*/
105-
public boolean hasOption(char opt) {
106-
return options.containsKey( new Character(opt) );
107+
public boolean hasOption(String opt) {
108+
return options.containsKey( opt );
107109
}
108110

109-
public Object getOptionObject(char opt) {
110-
String[] result = (String[])options.get( new Character(opt) );
111-
Object type = types.get( new Character(opt) );
111+
public Object getOptionObject(String opt) {
112+
String[] result = (String[])options.get( opt );
113+
Object type = types.get( opt );
112114
String res = result == null ? null : result[0];
113115
if(res == null) {
114116
return null;
@@ -121,8 +123,8 @@ public Object getOptionObject(char opt) {
121123
* @param opt Short single-character name of the option
122124
* @return Value of the argument if option is set, and has an argument, else null.
123125
*/
124-
public String getOptionValue(char opt) {
125-
String[] result = (String[])options.get( new Character(opt) );
126+
public String getOptionValue(String opt) {
127+
String[] result = (String[])options.get( opt );
126128
return result == null ? null : result[0];
127129
}
128130

@@ -131,8 +133,8 @@ public String getOptionValue(char opt) {
131133
* @param opt Single-character name of the option
132134
* @return An array of values if the option is set, and has an argument, else null.
133135
*/
134-
public String[] getOptionValues(char opt) {
135-
String[] result = (String[])options.get( new Character(opt) );
136+
public String[] getOptionValues(String opt) {
137+
String[] result = (String[])options.get( opt );
136138
return result == null ? null : result;
137139
}
138140

@@ -142,7 +144,7 @@ public String[] getOptionValues(char opt) {
142144
* @param defaultValue is the default value to be returned if the option is not specified
143145
* @return Value of the argument if option is set, and has an argument, else null.
144146
*/
145-
public String getOptionValue(char opt, String defaultValue) {
147+
public String getOptionValue(String opt, String defaultValue) {
146148
String answer = getOptionValue(opt);
147149
return (answer != null) ? answer : defaultValue;
148150
}
@@ -196,8 +198,8 @@ void addArg(String arg) {
196198
*
197199
* @param opt the processed option
198200
*/
199-
void setOpt(char opt) {
200-
options.put( new Character(opt), null );
201+
void setOpt(String opt) {
202+
options.put( opt, null );
201203
}
202204

203205
/**
@@ -207,8 +209,8 @@ void setOpt(char opt) {
207209
* @param opt the processed option
208210
* @param value the value of the option
209211
*/
210-
void setOpt(char opt, String value) {
211-
options.put( new Character(opt), value );
212+
void setOpt(String opt, String value) {
213+
options.put( opt, value );
212214
}
213215

214216
/**
@@ -218,8 +220,7 @@ void setOpt(char opt, String value) {
218220
* @param opt the processed option
219221
*/
220222
void setOpt(Option opt) {
221-
Character chr = new Character( opt.getOpt() );
222-
options.put( chr, opt.getValues() );
223-
types.put( chr, opt.getType() );
223+
options.put( opt.getOpt(), opt.getValues() );
224+
types.put( opt.getOpt(), opt.getType() );
224225
}
225226
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/CommandLineParser.java,v 1.1 2002/06/16 23:00:57 jkeyes Exp $
3+
* $Revision: 1.1 $
4+
* $Date: 2002/06/16 23:00:57 $
5+
*
6+
* ====================================================================
7+
*
8+
* The Apache Software License, Version 1.1
9+
*
10+
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11+
* reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
*
17+
* 1. Redistributions of source code must retain the above copyright
18+
* notice, this list of conditions and the following disclaimer.
19+
*
20+
* 2. Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimer in
22+
* the documentation and/or other materials provided with the
23+
* distribution.
24+
*
25+
* 3. The end-user documentation included with the redistribution, if
26+
* any, must include the following acknowlegement:
27+
* "This product includes software developed by the
28+
* Apache Software Foundation (http://www.apache.org/)."
29+
* Alternately, this acknowlegement may appear in the software itself,
30+
* if and wherever such third-party acknowlegements normally appear.
31+
*
32+
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
33+
* Foundation" must not be used to endorse or promote products derived
34+
* from this software without prior written permission. For written
35+
* permission, please contact apache@apache.org.
36+
*
37+
* 5. Products derived from this software may not be called "Apache"
38+
* nor may "Apache" appear in their names without prior written
39+
* permission of the Apache Group.
40+
*
41+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52+
* SUCH DAMAGE.
53+
* ====================================================================
54+
*
55+
* This software consists of voluntary contributions made by many
56+
* individuals on behalf of the Apache Software Foundation. For more
57+
* information on the Apache Software Foundation, please see
58+
* <http://www.apache.org/>.
59+
*
60+
*/
61+
package org.apache.commons.cli;
62+
63+
import java.util.List;
64+
65+
/**
66+
* @author John Keyes (jbjk at mac.com)
67+
*/
68+
public interface CommandLineParser {
69+
70+
/**
71+
* Parse the arguments according to the specified options.
72+
* @param options the specified Options
73+
* @param arguments the command line arguments
74+
* @return the list of atomic option and value tokens
75+
*/
76+
public List parse( Options options, List arguments );
77+
78+
/**
79+
* Parse the arguments according to the specified options.
80+
* @param options the specified Options
81+
* @param arguments the command line arguments
82+
* @param stopAtNonOption specifies whether to continue parsing the
83+
* arguments if a non option is encountered.
84+
* @return the list of atomic option and value tokens
85+
*/
86+
public List parse( Options options, List arguments, boolean stopAtNonOption );
87+
}

src/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ protected StringBuffer renderOptions( StringBuffer sb,
199199
{
200200
optBuf = (StringBuffer) i.next();
201201
opt = optBuf.charAt(optOffset);
202-
option = options.getOption(opt);
202+
option = options.getOption( "" + opt);
203203
renderWrappedText(sb, width, nextLineTabStop,
204204
optBuf.append(option.getDescription()).toString());
205205
if ( i.hasNext() )

src/java/org/apache/commons/cli/Option.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
public class Option {
9393

9494
/** opt the single character representation of the option */
95-
private Character opt = null;
95+
private String opt = null;
9696

9797
/** longOpt is the long representation of the option */
9898
private String longOpt = null;
@@ -119,58 +119,71 @@ public class Option {
119119
/**
120120
* Creates an Option using the specified parameters.
121121
*
122-
* @param opt character representation of the option
122+
* @param opt short representation of the option
123123
* @param hasArg specifies whether the Option takes an argument or not
124124
* @param description describes the function of the option
125125
*/
126-
public Option(char opt, boolean hasArg, String description) {
126+
public Option(String opt, boolean hasArg, String description) {
127127
this(opt, null, hasArg, description, false, false);
128128
}
129129

130130
/**
131131
* Creates an Option using the specified parameters.
132132
*
133-
* @param opt character representation of the option
133+
* @param opt short representation of the option
134134
* @param longOpt the long representation of the option
135135
* @param hasArg specifies whether the Option takes an argument or not
136136
* @param description describes the function of the option
137137
*/
138-
public Option(char opt, String longOpt, boolean hasArg, String description) {
138+
public Option(String opt, String longOpt, boolean hasArg, String description) {
139139
this(opt, longOpt, hasArg, description, false, false );
140140
}
141141

142142
/**
23A
143143
* Creates an Option using the specified parameters.
144144
*
145-
* @param opt character representation of the option
145+
* @param opt short representation of the option
146146
* @param longOpt the long representation of the option
147147
* @param hasArg specifies whether the Option takes an argument or not
148148
* @param description describes the function of the option
149149
* @param required specifies whether the option is required or not
150150
*/
151-
public Option(char opt, String longOpt, boolean hasArg, String description,
151+
public Option(String opt, String longOpt, boolean hasArg, String description,
152152
boolean required ) {
153153
this(opt, longOpt, hasArg, description, required, false );
154154
}
155155

156156
/**
157157
* Creates an Option using the specified parameters.
158158
*
159-
* @param opt character representation of the option
159+
* @param opt short representation of the option
160160
* @param longOpt the long representation of the option
161161
* @param hasArg specifies whether the Option takes an argument or not
162162
* @param description describes the function of the option
163163
* @param required specifies whether the option is required or not
164164
* @param multipleArgs specifies whether the option has multiple argument
165165
* values
166166
*/
167-
public Option(char opt, String longOpt, boolean hasArg, String description,
167+
public Option(String opt, String longOpt, boolean hasArg, String description,
168168
boolean required, boolean multipleArgs ) {
169169
this(opt, longOpt, hasArg, description, required, multipleArgs, null );
170170
}
171-
public Option(char opt, String longOpt, boolean hasArg, String description,
171+
172+
/**
173+
* Creates an Option using the specified parameters.
174+
*
175+
* @param opt short representation of the option
176+
* @param longOpt the long representation of the option
177+
* @param hasArg specifies whether the Option takes an argument or not
178+
* @param description describes the function of the option
179+
* @param required specifies whether the option is required or not
180+
* @param multipleArgs specifies whether the option has multiple argument
181+
* values
182+
* @param type specifies the type of the option
183+
*/
184+
public Option(String opt, String longOpt, boolean hasArg, String description,
172185
boolean required, boolean multipleArgs, Object type ) {
173-
this.opt = new Character( opt );
186+
this.opt = opt;
174187
this.longOpt = longOpt;
175188
this.hasArg = hasArg;
176189
this.description = description;
@@ -182,14 +195,14 @@ public Option(char opt, String longOpt, boolean hasArg, String description,
182195
/** <p>Retrieve the single-character name of this Option</p>
183196
*
184197
* <p>It is this character which can be used with
185-
* {@link CommandLine#hasOption(char opt)} and
186-
* {@link CommandLine#getOptionValue(char opt)} to check
198+
* {@link CommandLine#hasOption(String opt)} and
199+
* {@link CommandLine#getOptionValue(String opt)} to check
187200
* for existence and argument.<p>
188201
*
189202
* @return Single character name of this option
190203
*/
191-
public char getOpt() {
192-
return this.opt.charValue();
204+
public String getOpt() {
205+
return this.opt;
193206
}
194207

195208
public Object getType() {

0 commit comments

Comments
 (0)