Skip to content

Commit 4d2c8a2

Browse files
committed
The default argument name displayed is now properly controlled by the help formatter (CLI-205)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@956303 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4381ba5 commit 4d2c8a2

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,14 @@ private void appendOption(final StringBuffer buff, final Option option, final bo
648648
{
649649
buff.append("--").append(option.getLongOpt());
650650
}
651-
652-
// if the Option has a value
653-
if (option.hasArg() && option.hasArgName())
651+
652+
// if the Option has a value and a non blank argname
653+
if (option.hasArg() && (option.getArgName() == null || option.getArgName().length() != 0))
654654
{
655655
buff.append(option.getOpt() == null ? longOptSeparator : " ");
656-
buff.append("<").append(option.getArgName()).append(">");
656+
buff.append("<").append(option.getArgName() != null ? option.getArgName() : getArgName()).append(">");
657657
}
658-
658+
659659
// if the Option is not a required option
660660
if (!required)
661661
{
@@ -781,14 +781,16 @@ protected StringBuffer renderOptions(StringBuffer sb, int width, Options options
781781

782782
if (option.hasArg())
783783
{
784-
if (option.hasArgName())
784+
String argName = option.getArgName();
785+
if (argName != null && argName.length() == 0)
785786
{
786-
optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
787-
optBuf.append("<").append(option.getArgName()).append(">");
787+
// if the option has a blank argname
788+
optBuf.append(' ');
788789
}
789790
else
790791
{
791-
optBuf.append(' ');
792+
optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
793+
optBuf.append("<").append(argName != null ? option.getArgName() : getArgName()).append(">");
792794
}
793795
}
794796

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Option implements Cloneable, Serializable
5454
private String longOpt;
5555

5656
/** the name of the argument for this option */
57-
private String argName = "arg";
57+
private String argName;
5858

5959
/** description of the option */
6060
private String description;
@@ -319,11 +319,9 @@ public String getArgName()
319319
}
320320

321321
/**
322-
* Returns whether the display name for the argument value
323-
* has been set.
322+
* Returns whether the display name for the argument value has been set.
324323
*
325-
* @return if the display name for the argument value has been
326-
* set.
324+
* @return if the display name for the argument value has been set.
327325
*/
328326
public boolean hasArgName()
329327
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private OptionBuilder()
7777
private static void reset()
7878
{
7979
description = null;
80-
argName = "arg";
80+
argName = null;
8181
longopt = null;
8282
type = null;
8383
required = false;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,22 @@ public void testPrintOptionWithEmptyArgNameUsage()
305305
assertEquals("usage: app -f" + EOL, out.toString());
306306
}
307307

308+
public void testDefaultArgName()
309+
{
310+
Option option = OptionBuilder.hasArg().isRequired().create("f");
311+
312+
Options options = new Options();
313+
options.addOption(option);
314+
315+
StringWriter out = new StringWriter();
316+
317+
HelpFormatter formatter = new HelpFormatter();
318+
formatter.setArgName("argument");
319+
formatter.printUsage(new PrintWriter(out), 80, "app", options);
320+
321+
assertEquals("usage: app -f <argument>" + EOL, out.toString());
322+
}
323+
308324
public void testRtrim()
309325
{
310326
HelpFormatter formatter = new HelpFormatter();

0 commit comments

Comments
 (0)