@@ -382,7 +382,7 @@ try {
382382}
383383catch (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> Option< ></code > to the
501- <code >CommandLine.Builder.deprecatedHandler</code > method.
498+ The usage announcement may be changed by providing a <code >Consumer< 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> Option< 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< 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 < arg> the number of things
582- -n < arg> [Deprecated] the number of things
583- </source >
577+ usage: Command line syntax:
578+ -count < arg> the number of things
579+ -n < 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 < String, Option, String></code >
586+ <li >Calling <code >HelpFormatter.Builder.setShowDeprecated</code > with a <code >Function < 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 < 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 < 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 < arg> the number of things
605- -n < arg> the number of things. Deprecated for removal since now:
606- Use '-count' instead
607- </source >
599+ usage: Command line syntax:
600+ -count < arg> the number of things
601+ -n < arg> the number of things. Deprecated for removal since now:
602+ Use '-count' instead</source >
608603 </p >
609604 </subsection >
610605 </section >
0 commit comments