| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package joptsimple; |
| 7 | |
|
| 8 | |
import java.util.Comparator; |
| 9 | |
import java.util.Iterator; |
| 10 | |
import java.util.List; |
| 11 | |
import java.util.Map; |
| 12 | |
import java.util.SortedSet; |
| 13 | |
import java.util.TreeSet; |
| 14 | |
|
| 15 | |
import joptsimple.internal.Classes; |
| 16 | |
import joptsimple.internal.ColumnarData; |
| 17 | |
import joptsimple.internal.Strings; |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
class OptionParserHelpFormatter implements OptionSpecVisitor { |
| 24 | |
private final ColumnarData grid; |
| 25 | |
|
| 26 | 42 | OptionParserHelpFormatter() { |
| 27 | 42 | grid = new ColumnarData( new String[] { "Option", "Description" } ); |
| 28 | 42 | } |
| 29 | |
|
| 30 | |
String format( Map options ) { |
| 31 | 42 | if ( options.isEmpty() ) |
| 32 | 6 | return "No options specified"; |
| 33 | |
|
| 34 | 36 | grid.clear(); |
| 35 | |
|
| 36 | 36 | SortedSet sorted = new TreeSet( new OptionSpecComparator() ); |
| 37 | 36 | sorted.addAll( options.values() ); |
| 38 | |
|
| 39 | 36 | for ( Iterator iter = sorted.iterator(); iter.hasNext(); ) |
| 40 | 82 | ( (OptionSpec) iter.next() ).accept( this ); |
| 41 | |
|
| 42 | 36 | return grid.format(); |
| 43 | |
} |
| 44 | |
|
| 45 | |
void addHelpLineFor( OptionSpec spec, String additionalInfo ) { |
| 46 | 82 | grid.addRow( new Object[] { |
| 47 | |
createOptionDisplay( spec ) + additionalInfo, |
| 48 | |
spec.description() |
| 49 | |
} ); |
| 50 | 82 | } |
| 51 | |
|
| 52 | |
public void visit( NoArgumentOptionSpec spec ) { |
| 53 | 42 | addHelpLineFor( spec, "" ); |
| 54 | 42 | } |
| 55 | |
|
| 56 | |
public void visit( RequiredArgumentOptionSpec spec ) { |
| 57 | 28 | visit( spec, '<', '>' ); |
| 58 | 28 | } |
| 59 | |
|
| 60 | |
public void visit( OptionalArgumentOptionSpec spec ) { |
| 61 | 10 | visit( spec, '[', ']' ); |
| 62 | 10 | } |
| 63 | |
|
| 64 | |
private void visit( ArgumentAcceptingOptionSpec spec, char begin, char end ) { |
| 65 | 38 | String argDescription = spec.argumentDescription(); |
| 66 | 38 | String argType = nameOfArgumentType( spec ); |
| 67 | 38 | StringBuffer collector = new StringBuffer(); |
| 68 | |
|
| 69 | 38 | if ( argType.length() > 0 ) { |
| 70 | 14 | collector.append( argType ); |
| 71 | |
|
| 72 | 14 | if ( argDescription.length() > 0 ) |
| 73 | 8 | collector.append( ": " ).append( argDescription ); |
| 74 | |
} |
| 75 | 24 | else if ( argDescription.length() > 0 ) |
| 76 | 4 | collector.append( argDescription ); |
| 77 | |
|
| 78 | 38 | String helpLine = collector.length() == 0 |
| 79 | |
? "" |
| 80 | |
: ' ' + Strings.surround( collector.toString(), begin, end ); |
| 81 | 38 | addHelpLineFor( spec, helpLine ); |
| 82 | 38 | } |
| 83 | |
|
| 84 | |
public void visit( AlternativeLongOptionSpec spec ) { |
| 85 | 2 | String argDescription = spec.argumentDescription(); |
| 86 | 2 | addHelpLineFor( spec, ' ' + Strings.surround( argDescription, '<', '>' ) ); |
| 87 | 2 | } |
| 88 | |
|
| 89 | |
private String createOptionDisplay( OptionSpec spec ) { |
| 90 | 82 | StringBuffer buffer = new StringBuffer(); |
| 91 | |
|
| 92 | 82 | for ( Iterator iter = spec.options().iterator(); iter.hasNext(); ) { |
| 93 | 140 | String option = (String) iter.next(); |
| 94 | 140 | buffer.append( option.length() > 1 |
| 95 | |
? ParserRules.DOUBLE_HYPHEN |
| 96 | |
: ParserRules.HYPHEN ); |
| 97 | 140 | buffer.append( option ); |
| 98 | |
|
| 99 | 140 | if ( iter.hasNext() ) |
| 100 | 58 | buffer.append( ", " ); |
| 101 | 140 | } |
| 102 | |
|
| 103 | 82 | return buffer.toString(); |
| 104 | |
} |
| 105 | |
|
| 106 | |
private static String nameOfArgumentType( ArgumentAcceptingOptionSpec spec ) { |
| 107 | 38 | Class argType = spec.argumentType(); |
| 108 | 40 | return String.class.equals( argType ) ? "" : Classes.shortNameOf( argType ); |
| 109 | |
} |
| 110 | |
|
| 111 | 72 | private static class OptionSpecComparator implements Comparator { |
| 112 | |
public int compare( Object first, Object second ) { |
| 113 | 414 | OptionSpec firstSpec = (OptionSpec) first; |
| 114 | 414 | OptionSpec secondSpec = (OptionSpec) second; |
| 115 | |
|
| 116 | 414 | if ( firstSpec.equals( secondSpec ) ) |
| 117 | 58 | return 0; |
| 118 | |
|
| 119 | 356 | List firstOptions = firstSpec.options(); |
| 120 | 356 | List secondOptions = secondSpec.options(); |
| 121 | |
|
| 122 | 356 | return ( (String) firstOptions.get( 0 ) ) |
| 123 | |
.compareTo( (String) secondOptions.get( 0 ) ); |
| 124 | |
} |
| 125 | |
} |
| 126 | |
} |