Skip to content

Commit 180a16d

Browse files
committed
Added a parameter in HelpFormatter to specify the separator displayed between a long option and its value (CLI-169)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@954899 13f79535-47bb-0310-9956-ffa450edef68
1 parent bc5f93f commit 180a16d

2 files changed

Lines changed: 80 additions & 10 deletions

File tree

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import java.util.Comparator;
2525
import java.util.Iterator;
2626
import java.util.List;
27-
import java.util.StringTokenizer;
28-
import java.io.InputStream;
29-
import java.io.ByteArrayOutputStream;
3027

3128
/**
3229
* A formatter of help messages for the current command line options
@@ -60,6 +57,9 @@ public class HelpFormatter
6057
/** default prefix for long Option */
6158
public static final String DEFAULT_LONG_OPT_PREFIX = "--";
6259

60+
/** default separator displayed between a long Option and its value */
61+
public static final String DEFAULT_LONG_OPT_SEPARATOR = " ";
62+
6363
/** default name for an argument */
6464
public static final String DEFAULT_ARG_NAME = "arg";
6565

@@ -122,6 +122,9 @@ public class HelpFormatter
122122
*/
123123
public String defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
124124

125+
/** The separator displayed between the long option and its value. */
126+
private String longOptSeparator = DEFAULT_LONG_OPT_SEPARATOR;
127+
125128
/**
126129
* the name of the argument
127130
*
@@ -277,6 +280,30 @@ public String getLongOptPrefix()
277280
return defaultLongOptPrefix;
278281
}
279282

283+
/**
284+
* Set the separator displayed between a long option and its value.
285+
* Ensure that the separator specified is supported by the parser used,
286+
* typically ' ' or '='.
287+
*
288+
* @param longOptSeparator the separator, typically ' ' or '='.
289+
* @since 1.3
290+
*/
291+
public void setLongOptSeparator(String longOptSeparator)
292+
{
293+
this.longOptSeparator = longOptSeparator;
294+
}
295+
296+
/**
297+
* Returns the separator displayed between a long option and its value.
298+
*
299+
* @return the separator
300+
* @since 1.3
301+
*/
302+
public String getLongOptSeparator()
303+
{
304+
return longOptSeparator;
305+
}
306+
280307
/**
281308
* Sets the 'argName'.
282309
*
@@ -500,13 +527,12 @@ public void printHelp(PrintWriter pw, int width, String cmdLineSyntax,
500527
}
501528

502529
/**
503-
* <p>Prints the usage statement for the specified application.</p>
530+
* Prints the usage statement for the specified application.
504531
*
505532
* @param pw The PrintWriter to print the usage statement
506533
* @param width The number of characters to display per line
507534
* @param app The application name
508535
* @param options The command line Options
509-
*
510536
*/
511537
public void printUsage(PrintWriter pw, int width, String app, Options options)
512538
{
@@ -607,7 +633,7 @@ private void appendOptionGroup(final StringBuffer buff, final OptionGroup group)
607633
* @param option the Option to append
608634
* @param required whether the Option is required or not
609635
*/
610-
private static void appendOption(final StringBuffer buff, final Option option, final boolean required)
636+
private void appendOption(final StringBuffer buff, final Option option, final boolean required)
611637
{
612638
if (!required)
613639
{
@@ -626,7 +652,8 @@ private static void appendOption(final StringBuffer buff, final Option option, f
626652
// if the Option has a value
627653
if (option.hasArg() && option.hasArgName())
628654
{
629-
buff.append(" <").append(option.getArgName()).append(">");
655+
buff.append(option.getOpt() == null ? longOptSeparator : " ");
656+
buff.append("<").append(option.getArgName()).append(">");
630657
}
631658

632659
// if the Option is not a required option
@@ -652,8 +679,8 @@ public void printUsage(PrintWriter pw, int width, String cmdLineSyntax)
652679
}
653680

654681
/**
655-
* <p>Print the help for the specified Options to the specified writer,
656-
* using the specified width, left padding and description padding.</p>
682+
* Print the help for the specified Options to the specified writer,
683+
* using the specified width, left padding and description padding.
657684
*
658685
* @param pw The printWriter to write the help to
659686
* @param width The number of characters to display per line
@@ -756,7 +783,8 @@ protected StringBuffer renderOptions(StringBuffer sb, int width, Options options
756783
{
757784
if (option.hasArgName())
758785
{
759-
optBuf.append(" <").append(option.getArgName()).append(">");
786+
optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
787+
optBuf.append("<").append(option.getArgName()).append(">");
760788
}
761789
else
762790
{

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,46 @@ public void testOptionWithoutShortFormat2()
451451
,out.toString());
452452
}
453453

454+
public void testHelpWithLongOptSeparator() throws Exception
455+
{
456+
Options options = new Options();
457+
options.addOption( "f", true, "the file" );
458+
options.addOption(OptionBuilder.withLongOpt("size").withDescription("the size").hasArg().withArgName("SIZE").create('s'));
459+
options.addOption(OptionBuilder.withLongOpt("age").withDescription("the age").hasArg().create());
460+
461+
HelpFormatter formatter = new HelpFormatter();
462+
assertEquals(HelpFormatter.DEFAULT_LONG_OPT_SEPARATOR, formatter.getLongOptSeparator());
463+
formatter.setLongOptSeparator("=");
464+
assertEquals("=", formatter.getLongOptSeparator());
465+
466+
StringWriter out = new StringWriter();
467+
468+
formatter.printHelp(new PrintWriter(out), 80, "create", "header", options, 2, 2, "footer");
469+
470+
assertEquals(
471+
"usage: create" + EOL +
472+
"header" + EOL +
473+
" --age=<arg> the age" + EOL +
474+
" -f <arg> the file" + EOL +
475+
" -s,--size=<SIZE> the size" + EOL +
476+
"footer" + EOL,
477+
out.toString());
478+
}
479+
480+
public void testUsageWithLongOptSeparator() throws Exception
481+
{
482+
Options options = new Options();
483+
options.addOption( "f", true, "the file" );
484+
options.addOption(OptionBuilder.withLongOpt("size").withDescription("the size").hasArg().withArgName("SIZE").create('s'));
485+
options.addOption(OptionBuilder.withLongOpt("age").withDescription("the age").hasArg().create());
486+
487+
HelpFormatter formatter = new HelpFormatter();
488+
formatter.setLongOptSeparator("=");
489+
490+
StringWriter out = new StringWriter();
491+
492+
formatter.printUsage(new PrintWriter(out), 80, "create", options);
493+
494+
assertEquals("usage: create [--age=<arg>] [-f <arg>] [-s <SIZE>]", out.toString().trim());
495+
}
454496
}

0 commit comments

Comments
 (0)