Skip to content

Commit d36adeb

Browse files
committed
Added a formatter test for options with a null or empty named argument
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661634 13f79535-47bb-0310-9956-ffa450edef68
1 parent 33abc03 commit d36adeb

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ private static void appendOption(final StringBuffer buff,
629629
}
630630

631631
// if the Option has a value
632-
if (option.hasArg() && (option.getArgName() != null))
632+
if (option.hasArg() && option.hasArgName())
633633
{
634634
buff.append(" <").append(option.getArgName()).append(">");
635635
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,20 @@ public void testPrintRequiredOptionGroupUsage() {
257257
assertEquals("usage: app -a | -b | -c" + EOL, out.toString());
258258
}
259259

260+
public void testPrintOptionWithEmptyArgNameUsage() {
261+
Option option = new Option("f", true, null);
262+
option.setArgName("");
263+
option.setRequired(true);
264+
265+
Options options = new Options();
266+
options.addOption(option);
267+
268+
StringWriter out = new StringWriter();
269+
270+
HelpFormatter formatter = new HelpFormatter();
271+
formatter.printUsage(new PrintWriter(out), 80, "app", options);
272+
273+
assertEquals("usage: app -f" + EOL, out.toString());
274+
}
275+
260276
}

0 commit comments

Comments
 (0)