Skip to content

Commit 4394bf7

Browse files
committed
Changed deprecatedFormatFunc BiFunction to Function, updated docs, added HelpFromatter.getDescription(Option)
1 parent 65cb6cd commit 4394bf7

3 files changed

Lines changed: 141 additions & 138 deletions

File tree

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3030
import java.util.Iterator;
3131
import java.util.List;
3232
import java.util.Objects;
33-
import java.util.function.BiFunction;
33+
import java.util.function.Function;
3434
import java.util.function.Supplier;
3535

3636
/**
@@ -78,12 +78,12 @@ public static final class Builder implements Supplier<HelpFormatter> {
7878
/**
7979
* A function to convert a description (not null) and a deprecated Option (not null) to help description
8080
*/
81-
private static final BiFunction<String, Option, String> DEFAULT_DEPRECATED_FORMAT = (d, o) -> "[Deprecated] " + d;
81+
private static final Function<Option, String> DEFAULT_DEPRECATED_FORMAT = o -> "[Deprecated] " + getDescription(o);
8282

8383
/**
8484
* Formatter for deprecated options.
8585
*/
86-
private BiFunction<String, Option, String> deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT;
86+
private Function<Option, String> deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT;
8787

8888
/**
8989
* The output PrintWriter, defaults to wrapping {@link System#out}.
@@ -113,7 +113,7 @@ public Builder setPrintWriter(final PrintWriter printWriter) {
113113
* @return this.
114114
* @since 1.8.0
115115
*/
116-
public Builder setShowDeprecated(final BiFunction<String, Option, String> showDeprecatedFunc) {
116+
public Builder setShowDeprecated(final Function<Option, String> showDeprecatedFunc) {
117117
this.deprecatedFormatFunc = showDeprecatedFunc;
118118
return this;
119119
}
@@ -129,6 +129,14 @@ public Builder setShowDeprecated(final boolean useDefaultFormat) {
129129
}
130130
}
131131

132+
/**
133+
* Returns the option description or an empty string if the description is {@code null}.
134+
* @param option The option to get the description from.
135+
* @return the option description or an empty string if the description is {@code null}.
136+
*/
137+
public static String getDescription(Option option) {
138+
return option.getDescription() == null ? "" : option.getDescription();
139+
}
132140
/**
133141
* This class implements the {@code Comparator} interface for comparing Options.
134142
*/
@@ -268,7 +276,7 @@ private static PrintWriter createDefaultPrintWriter() {
268276
/**
269277
* BiFunction to format the description for a deprecated option.
270278
*/
271-
private final BiFunction<String, Option, String> deprecatedFormatFunc;
279+
private final Function<Option, String> deprecatedFormatFunc;
272280

273281
/**
274282
* Where to print help.
@@ -291,7 +299,7 @@ public HelpFormatter() {
291299
* Constructs a new instance.
292300
* @param printStream TODO
293301
*/
294-
private HelpFormatter(final BiFunction<String, Option, String> deprecatedFormatFunc, final PrintWriter printStream) {
302+
private HelpFormatter(final Function<Option, String> deprecatedFormatFunc, final PrintWriter printStream) {
295303
// TODO All other instance HelpFormatter instance variables.
296304
// Make HelpFormatter immutable for 2.0
297305
this.deprecatedFormatFunc = deprecatedFormatFunc;
@@ -795,7 +803,7 @@ protected StringBuffer renderOptions(final StringBuffer sb, final int width, fin
795803
optBuf.append(dpad);
796804
final int nextLineTabStop = max + descPad;
797805
if (deprecatedFormatFunc != null && option.isDeprecated()) {
798-
optBuf.append(deprecatedFormatFunc.apply(option.getDescription() == null ? "" : option.getDescription(), option).trim());
806+
optBuf.append(deprecatedFormatFunc.apply(option).trim());
799807
} else if (option.getDescription() != null) {
800808
optBuf.append(option.getDescription());
801809
}

src/site/xdoc/usage.xml

Lines changed: 114 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ try {
382382
}
383383
catch (ParseException exp) {
384384
System.out.println("Unexpected exception:" + exp.getMessage());
385-
}</source>
385+
} </source>
386386
</section>
387387
<section name="Converting (Parsing) Option Values">
388388
<p>
@@ -392,31 +392,31 @@ catch (ParseException exp) {
392392
that the "count" option should return an Integer the following code could be used:
393393
</p>
394394
<source>
395-
public static void main(String[] args) {
396-
Option count = Option.builder("count")
397-
.hasArg()
398-
.desc("the number of things")
399-
.type(Integer.class)
400-
.build();
401-
Options options = new Options().addOption(count);
402-
// create the parser
403-
CommandLineParser parser = new DefaultParser();
404-
try {
405-
// parse the command line arguments
406-
CommandLine line = parser.parse(options, args);
407-
}
408-
catch (ParseException exp) {
409-
// oops, something went wrong
410-
System.err.println("Parsing failed. Reason: " + exp.getMessage());
411-
}
412-
413-
try {
414-
Integer value = line.getParsedOptionValue(count);
415-
System.out.format("The value is %s\m", value );
416-
} catch (ParseException e) {
417-
e.printStackTrace();
418-
}
419-
}</source>
395+
public static void main(String[] args) {
396+
Option count = Option.builder("count")
397+
.hasArg()
398+
.desc("the number of things")
399+
.type(Integer.class)
400+
.build();
401+
Options options = new Options().addOption(count);
402+
// create the parser
403+
CommandLineParser parser = new DefaultParser();
404+
try {
405+
// parse the command line arguments
406+
CommandLine line = parser.parse(options, args);
407+
}
408+
catch (ParseException exp) {
409+
// oops, something went wrong
410+
System.err.println("Parsing failed. Reason: " + exp.getMessage());
411+
}
412+
413+
try {
414+
Integer value = line.getParsedOptionValue(count);
415+
System.out.format("The value is %s\m", value );
416+
} catch (ParseException e) {
417+
e.printStackTrace();
418+
}
419+
} </source>
420420
<p>
421421
The value types natively supported by commons-cli are:
422422
<ul>
@@ -446,12 +446,11 @@ catch (ParseException exp) {
446446
Conversions can be specified without using the <code>TypeHandler</code> class by specifying the converter
447447
directly during the option build. For example:
448448
<source>
449-
Option fooOpt = Option.builder("foo")
450-
.hasArg()
451-
.desc("the foo arg")
452-
.converter((s) -> new Foo(s))
453-
.build();
454-
</source>
449+
Option fooOpt = Option.builder("foo")
450+
.hasArg()
451+
.desc("the foo arg")
452+
.converter((s) -> new Foo(s))
453+
.build();</source>
455454
The above will create an option that passes the string value to the Foo constructor when <code>commandLine.getParsedOptionValue(fooOpt)</code> is called.
456455
</p>
457456
<p>
@@ -466,145 +465,141 @@ catch (ParseException exp) {
466465
<code>deprecated</code> method.
467466

468467
Usage of the deprecated option is announced when the presence of the option is checked
469-
or the value of the option is retrieved. By default the announcement printed to Standard out.
468+
or the value of the option is retrieved. By default, the announcement printed to Standard out.
470469

471-
The HelpFormatter output will be default show the description prefixed by "[Deprecated]"
470+
The HelpFormatter output will by default show the description prefixed by "[Deprecated]"
472471
</p>
473472
<p>
474473
The examples below will implement <code>doSomething</code> in the following code block.
475474
<source>
476-
public static void main(String[] args) {
477-
Option n = Option.builder("n")
478-
.deprecated(DeprecatedAttributes.builder()
479-
.setDescription("Use '-count' instead")
480-
.setForRemoval(true)
481-
.setSince("now").get())
482-
.hasArg()
483-
.desc("the number of things")
484-
.type(Integer.class)
485-
.build();
486-
Option count = Option.builder("count")
487-
.hasArg()
488-
.desc("the number of things")
489-
.type(Integer.class)
490-
.build();
491-
Options options = new Options().addOption(n).addOption(count);
475+
public static void main(String[] args) {
476+
Option n = Option.builder("n")
477+
.deprecated(DeprecatedAttributes.builder()
478+
.setDescription("Use '-count' instead")
479+
.setForRemoval(true)
480+
.setSince("now").get())
481+
.hasArg()
482+
.desc("the number of things")
483+
.type(Integer.class)
484+
.build();
485+
Option count = Option.builder("count")
486+
.hasArg()
487+
.desc("the number of things")
488+
.type(Integer.class)
489+
.build();
490+
Options options = new Options().addOption(n).addOption(count);
492491

493-
doSomething(options);
494-
}
495-
</source>
492+
doSomething(options);
493+
} </source>
496494
</p>
497495

498496
<subsection name="Changing Usage Announcement">
499497
<p>
500-
The usage announcement may be changed by providing a <code>Consumer&gt;Option&lt;></code> to the
501-
<code>CommandLine.Builder.deprecatedHandler</code> method.
498+
The usage announcement may be changed by providing a <code>Consumer&lt;Option></code> to the
499+
<code>CommandLine.Builder.deprecatedHandler</code> method. This is commonly used to log usage
500+
of deprecated options rather than printing them on the standard output.
502501
</p>
503502
<p>
504503
for example if <code>doSomething</code> is implemented as:
505504
</p>
506-
<sorce>
507-
void doSomething(Options options) {
508-
CommandLineParser parser = new DefaultParser();
509-
CommandLine line;
510-
try {
511-
// parse the command line arguments
512-
line = parser.parse(options, new String[] {"-n", "5"});
513-
System.out.println( "n="+line.getParsedOptionValue("n"));
514-
} catch (ParseException exp) {
515-
// oops, something went wrong
516-
System.err.println("Parsing failed. Reason: " + exp.getMessage());
517-
}
518-
}
519-
</sorce>
505+
<source>
506+
void doSomething(Options options) {
507+
CommandLineParser parser = new DefaultParser();
508+
CommandLine line;
509+
try {
510+
// parse the command line arguments
511+
line = parser.parse(options, new String[] {"-n", "5"});
512+
System.out.println( "n="+line.getParsedOptionValue("n"));
513+
} catch (ParseException exp) {
514+
// oops, something went wrong
515+
System.err.println("Parsing failed. Reason: " + exp.getMessage());
516+
}
517+
} </source>
520518
<p>
521519
The output of the run would be.
522520
</p>
523521

524522
<source>
525-
Option 'n': Deprecated for removal since now: Use '-count' instead
526-
n=5
527-
</source>
523+
Option 'n': Deprecated for removal since now: Use '-count' instead
524+
n=5 </source>
528525

529526
<p>
530527
for example if <code>doSomething</code> is implemented as:
531528
</p>
532529

533530
<source>
534-
void doSomething(Options options) {
535-
Consumer&gt;Option&lt; deprecatedUsageAnnouncement = o -> {
536-
final StringBuilder buf = new StringBuilder()
537-
.append("'")
538-
.append(o.getOpt())
539-
.append("'");
540-
if (o.getLongOpt() != null) {
541-
buf.append("'").append(o.getLongOpt()).append("'");
542-
}
543-
System.err.printf("ERROR: Option %s: %s%n", buf, o.getDeprecated());
544-
};
545-
DefaultParser parser = DefaultParser.builder().setDeprecatedHandler(deprecatedUsageAnnouncement).build();
546-
CommandLine line;
547-
try {
548-
// parse the command line arguments
549-
line = parser.parse(options, new String[] {"-n", "5"});
550-
System.out.println( "n="+line.getParsedOptionValue("n"));
551-
} catch (ParseException exp) {
552-
// oops, something went wrong
553-
System.err.println("Parsing failed. Reason: " + exp.getMessage());
554-
}
555-
}
556-
</source>
531+
void doSomething(Options options) {
532+
Consumer&lt;Option> deprecatedUsageAnnouncement = o -> {
533+
final StringBuilder buf = new StringBuilder()
534+
.append("'")
535+
.append(o.getOpt())
536+
.append("'");
537+
if (o.getLongOpt() != null) {
538+
buf.append("'").append(o.getLongOpt()).append("'");
539+
}
540+
System.err.printf("ERROR: Option %s: %s%n", buf, o.getDeprecated());
541+
};
542+
DefaultParser parser = DefaultParser.builder().setDeprecatedHandler(deprecatedUsageAnnouncement).build();
543+
CommandLine line;
544+
try {
545+
// parse the command line arguments
546+
line = parser.parse(options, new String[] {"-n", "5"});
547+
System.out.println( "n="+line.getParsedOptionValue("n"));
548+
} catch (ParseException exp) {
549+
// oops, something went wrong
550+
System.err.println("Parsing failed. Reason: " + exp.getMessage());
551+
}
552+
} </source>
557553
<p>
558554
The output of the run would be.
559555
</p>
560556
<source>
561-
ERROR: Option 'n': Deprecated for removal since now: Use '-count' instead
562-
n=5
563-
</source>
557+
ERROR: Option 'n': Deprecated for removal since now: Use '-count' instead
558+
n=5 </source>
564559
<p>
565560
However, the first line would be printed on system err instead of system out.
566561
</p>
567562

568563
</subsection>
569564
<subsection name="Changing help format">
565+
<p>By default the help formater prints "[Deprecated]" in front of the description for the option. This can
566+
be changed to display any values desired.
567+
</p>
570568
<p>
571569
If <code>doSomething</code> is implemented as:
572570
<source>
573-
void doSomething(Options options) {
574-
HelpFormatter formatter = HelpFormatter.builder().get();
575-
formatter.printHelp("Command line syntax:", options);
576-
}
577-
</source>
571+
void doSomething(Options options) {
572+
HelpFormatter formatter = HelpFormatter.builder().get();
573+
formatter.printHelp("Command line syntax:", options);
574+
} </source>
578575
To output is
579576
<source>
580-
usage: Command line syntax:
581-
-count &lt;arg> the number of things
582-
-n &lt;arg> [Deprecated] the number of things
583-
</source>
577+
usage: Command line syntax:
578+
-count &lt;arg> the number of things
579+
-n &lt;arg> [Deprecated] the number of things</source>
584580
</p>
585581
<p>
586582
The display of deprecated options may be changed through the use of the
587583
<code>HelpFormatter.Builder.setShowDeprecated()</code> method.
588584
<ul>
589585
<li>Calling <code>HelpFormatter.Builder.setShowDeprecated(false)</code> will disable the "[Deprecated]" tag.</li>
590-
<li>Calling <code>HelpFormatter.Builder.setShowDeprecated</code> with a <code>BiFunction&lt;String, Option, String></code>
586+
<li>Calling <code>HelpFormatter.Builder.setShowDeprecated</code> with a <code>Function&lt;Option, String></code>
591587
will use the output of the function as the description for the option.</li>
592588
</ul>
593589
As an example of the second case above, changing the implementation of <code>doSomething</code> to
594590
<source>
595-
void doSomething(Options options) {
596-
BiFunction&lt;String, Option, String> disp = (desc, option) -> String.format( "%s. %s", desc, option.getDeprecated().toString());
597-
HelpFormatter formatter = HelpFormatter.builder().setShowDeprecated(disp).get();
598-
formatter.printHelp("Command line syntax:", options);
599-
}
600-
</source>
591+
void doSomething(Options options) {
592+
Function&lt;Option, String> disp = option -> String.format( "%s. %s", HelpFormatter.getDescription(option),
593+
option.getDeprecated().toString());
594+
HelpFormatter formatter = HelpFormatter.builder().setShowDeprecated(disp).get();
595+
formatter.printHelp("Command line syntax:", options);
596+
} </source>
601597
changes the output to
602598
<source>
603-
usage: Command line syntax:
604-
-count &lt;arg> the number of things
605-
-n &lt;arg> the number of things. Deprecated for removal since now:
606-
Use '-count' instead
607-
</source>
599+
usage: Command line syntax:
600+
-count &lt;arg> the number of things
601+
-n &lt;arg> the number of things. Deprecated for removal since now:
602+
Use '-count' instead</source>
608603
</p>
609604
</subsection>
610605
</section>

0 commit comments

Comments
 (0)