Skip to content

Commit 62670f5

Browse files
author
John Keyes
committed
fixed bug 13425, added argName support, fixed some bugs in the HelpFormatter
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129819 13f79535-47bb-0310-9956-ffa450edef68
1 parent f577183 commit 62670f5

6 files changed

Lines changed: 139 additions & 54 deletions

File tree

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

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,24 @@ public void printUsage( PrintWriter pw, int width, String app, Options options )
269269
if( !option.isRequired() ) {
270270
buff.append( "[" );
271271
}
272-
buff.append( "-" ).append( option.getOpt() );
272+
273+
if( !" ".equals( option.getOpt() ) ) {
274+
buff.append( "-" ).append( option.getOpt() );
275+
}
276+
else {
277+
buff.append( "--" ).append( option.getLongOpt() );
278+
}
279+
280+
if( option.getValueSeparator() != (char)0 ) {
281+
buff.append( option.getValueSeparator() );
282+
}
283+
else if( option.hasArg() ){
284+
buff.append( " " );
285+
}
273286

274287
// if the Option has a value
275288
if( option.hasArg() ) {
276-
buff.append( " arg" );
289+
buff.append( option.getArgName() );
277290
}
278291

279292
// if the Option is not a required option
@@ -284,7 +297,6 @@ public void printUsage( PrintWriter pw, int width, String app, Options options )
284297
}
285298
}
286299

287-
System.out.println( "->" + buff.toString() );
288300
// call printWrapped
289301
printWrapped( pw, width, buff.toString().indexOf(' ')+1,
290302
buff.toString() );
@@ -334,7 +346,9 @@ protected StringBuffer renderOptions( StringBuffer sb,
334346
StringBuffer optBuf;
335347
List prefixList = new ArrayList();
336348
Option option;
337-
for ( Iterator i = options.getOptions().iterator(); i.hasNext(); )
349+
List optList = options.helpOptions();
350+
Collections.sort( optList, new StringBufferComparator() );
351+
for ( Iterator i = optList.iterator(); i.hasNext(); )
338352
{
339353
option = (Option) i.next();
340354
optBuf = new StringBuffer(8);
@@ -353,49 +367,30 @@ protected StringBuffer renderOptions( StringBuffer sb,
353367

354368
}
355369

356-
357-
if ( option.hasArg() )
358-
{
359-
//FIXME - should have a way to specify arg name per option
360-
optBuf.append(' ').append(defaultArgName);
370+
if( option.hasArg() ) {
371+
if( option.hasArgName() ) {
372+
optBuf.append( option.getArgName() );
373+
}
374+
else {
375+
optBuf.append(' ');
376+
}
361377
}
378+
362379
prefixList.add(optBuf);
363380
max = optBuf.length() > max ? optBuf.length() : max;
364-
}
365381

366-
//right pad the prefixes
367-
for ( Iterator i = prefixList.iterator(); i.hasNext(); )
368-
{
369-
optBuf = (StringBuffer) i.next();
370382
if ( optBuf.length() < max )
371383
{
372-
optBuf.append(createPadding(max-optBuf.length()));
384+
optBuf.append(createPadding(max-optBuf.length()));
373385
}
374386
optBuf.append(dpad);
375-
}
376-
377-
//sort this list ascending
378-
Collections.sort(prefixList, new StringBufferComparator());
379-
380-
//finally render options
381-
int nextLineTabStop = max + descPad;
382-
String opt;
383-
int optOffset = leftPad + defaultOptPrefix.length();
384-
385-
for ( Iterator i = prefixList.iterator(); i.hasNext(); )
386-
{
387-
optBuf = (StringBuffer) i.next();
388-
opt = optBuf.toString().trim();
389-
if( opt.indexOf( ',' ) != -1 ) {
390-
opt = opt.substring(0, opt.indexOf( ',', optOffset ) );
391-
}
392-
option = options.getOption(opt);
393-
387+
388+
int nextLineTabStop = max + descPad;
394389
renderWrappedText(sb, width, nextLineTabStop,
395390
optBuf.append(option.getDescription()).toString());
396391
if ( i.hasNext() )
397392
{
398-
sb.append(defaultNewLine);
393+
sb.append(defaultNewLine);
399394
}
400395
}
401396

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public class Option implements Cloneable {
106106
/** hasArg specifies whether this option has an associated argument */
107107
private boolean hasArg;
108108

109+
/** argName specifies the name of the argument for this option */
110+
private String argName;
111+
109112
/** description of the option */
110113
private String description;
111114

@@ -366,6 +369,35 @@ public void setRequired( boolean required ) {
366369
this.required = required;
367370
}
368371

372+
/**
373+
* <p>Sets the display name for the argument value.</p>
374+
*
375+
* @param argName the display name for the argument value.
376+
*/
377+
public void setArgName( String argName ) {
378+
this.argName = argName;
379+
}
380+
381+
/**
382+
* <p>Gets the display name for the argument value.</p>
383+
*
384+
* @return the display name for the argument value.
385+
*/
386+
public String getArgName() {
387+
return this.argName;
388+
}
389+
390+
/**
391+
* <p>Returns whether the display name for the argument value
392+
* has been set.</p>
393+
*
394+
* @return if the display name for the argument value has been
395+
* set.
396+
*/
397+
public boolean hasArgName() {
398+
return (this.argName != null || this.argName.length() > 0 );
399+
}
400+
369401
/**
370402
* <p>Query to see if this Option can take many values</p>
371403
*

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.10 2002/09/19 22:59:43 jkeyes Exp $
3-
* $Revision: 1.10 $
4-
* $Date: 2002/09/19 22:59:43 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.11 2002/10/08 21:24:11 jkeyes Exp $
3+
* $Revision: 1.11 $
4+
* $Date: 2002/10/08 21:24:11 $
55
*
66
* ====================================================================
77
*
@@ -76,6 +76,8 @@ public class OptionBuilder {
7676
private static String longopt;
7777
/** option description */
7878
private static String description;
79+
/** argument name */
80+
private static String argName;
7981
/** is required? */
8082
private static boolean required;
8183
/** the number of arguments */
@@ -99,6 +101,7 @@ private OptionBuilder() {
99101
*/
100102
private static void reset() {
101103
description = null;
104+
argName = null;
102105
longopt = null;
103106
type = null;
104107
required = false;
@@ -142,6 +145,18 @@ public static OptionBuilder hasArg( boolean hasArg ) {
142145
return instance;
143146
}
144147

148+
/**
149+
* <p>The next Option created will have the specified argument value
150+
* name.</p>
151+
*
152+
* @param name the name for the argument value
153+
* @return the OptionBuilder instance
154+
*/
155+
public static OptionBuilder withArgName( String name ) {
156+
instance.argName = name;
157+
return instance;
158+
}
159+
145160
/**
146161
* <p>The next Option created will be required.</p>
147162
*
@@ -344,6 +359,7 @@ public static Option create( String opt )
344359
option.setArgs( numberOfArgs );
345360
option.setType( type );
346361
option.setValueSeparator( valuesep );
362+
option.setArgName( argName );
347363
// reset the OptionBuilder properties
348364
instance.reset();
349365

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ public Collection getOptions() {
195195
return Collections.unmodifiableCollection( opts );
196196
}
197197

198+
/**
199+
* <p>Returns the Options for use by the HelpFormatter.</p>
200+
*
201+
* @return the List of Options
202+
*/
203+
List helpOptions() {
204+
return new ArrayList( shortOpts.values() );
205+
}
206+
198207
/** <p>Returns the required options as a
199208
* <code>java.util.Collection</code>.</p>
200209
*

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.5 2002/09/19 22:59:43 jkeyes Exp $
3-
* $Revision: 1.5 $
4-
* $Date: 2002/09/19 22:59:43 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/Parser.java,v 1.6 2002/10/08 21:24:11 jkeyes Exp $
3+
* $Revision: 1.6 $
4+
* $Date: 2002/10/08 21:24:11 $
55
*
66
* ====================================================================
77
*
@@ -72,7 +72,7 @@
7272
*
7373
* @author John Keyes (john at integralsource.com)
7474
* @see Parser
75-
* @version $Revision: 1.5 $
75+
* @version $Revision: 1.6 $
7676
*/
7777
public abstract class Parser implements CommandLineParser {
7878

@@ -217,27 +217,27 @@ private void checkRequiredOptions()
217217
}
218218

219219
public void processArgs( Option opt, ListIterator iter )
220-
throws ParseException
220+
throws ParseException
221221
{
222-
if( !iter.hasNext() && !opt.hasOptionalArg() ) {
223-
throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
224-
}
225222
// loop until an option is found
226223
while( iter.hasNext() ) {
227224
String var = (String)iter.next();
225+
226+
// found an Option
228227
if( options.hasOption( var ) ) {
229228
iter.previous();
230229
break;
231230
}
232-
233-
// its a value
234-
else {
235-
if( !opt.addValue( var ) ) {
236-
iter.previous();
237-
break;
238-
}
231+
// found a value
232+
else if( !opt.addValue( var ) ) {
233+
iter.previous();
234+
break;
239235
}
240236
}
237+
238+
if( opt.getValues() == null && !opt.hasOptionalArg() ) {
239+
throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
240+
}
241241
}
242242

243243
private void processOption( String arg, ListIterator iter )

src/test/org/apache/commons/cli/BugsTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* version 1.1, a copy of which has been included with this distribution in
66
* the LICENSE file.
77
*
8-
* $Id: BugsTest.java,v 1.7 2002/09/19 22:59:44 jkeyes Exp $
8+
* $Id: BugsTest.java,v 1.8 2002/10/08 21:24:11 jkeyes Exp $
99
*/
1010

1111
package org.apache.commons.cli;
@@ -229,4 +229,37 @@ else if(cmd.hasOption("rep")){
229229
fail( "Unexpected exception: " + exp.getMessage() );
230230
}
231231
}
232+
233+
public void test13425() {
234+
Options options = new Options();
235+
Option oldpass = OptionBuilder.withLongOpt( "old-password" )
236+
.withDescription( "Use this option to specify the old password" )
237+
.hasArg()
238+
.create( 'o' );
239+
Option newpass = OptionBuilder.withLongOpt( "new-password" )
240+
.withDescription( "Use this option to specify the new password" )
241+
.hasArg()
242+
.create( 'n' );
243+
244+
String[] args = {
245+
"-o",
246+
"-n",
247+
"newpassword"
248+
};
249+
250+
options.addOption( oldpass );
251+
options.addOption( newpass );
252+
253+
Parser parser = new PosixParser();
254+
255+
try {
256+
CommandLine line = parser.parse( options, args );
257+
}
258+
// catch the exception and leave the method
259+
catch( Exception exp ) {
260+
assertTrue( exp != null );
261+
return;
262+
}
263+
fail( "MissingArgumentException not caught." );
264+
}
232265
}

0 commit comments

Comments
 (0)