1 package org.apache.maven.doxia.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.cli.DefaultParser;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Option;
26 import org.apache.commons.cli.Options;
27 import org.apache.commons.cli.ParseException;
28 import org.apache.maven.doxia.DefaultConverter;
29
30 import com.ibm.icu.text.CharsetDetector;
31
32 import static org.codehaus.plexus.util.StringUtils.join;
33
34
35
36
37
38
39 class CLIManager
40 {
41
42 static final String HELP = "h";
43
44
45 static final String VERSION = "v";
46
47
48 static final String IN = "in";
49
50
51 static final String OUT = "out";
52
53
54 static final String FROM = "from";
55
56
57 static final String TO = "to";
58
59
60 static final String INENCODING = "inEncoding";
61
62
63 static final String FORMAT = "f";
64
65
66 static final String OUTENCODING = "outEncoding";
67
68
69 static final String DEBUG = "X";
70
71
72 static final String ERRORS = "e";
73
74 private static final Options OPTIONS;
75
76 static
77 {
78 OPTIONS = new Options();
79
80 OPTIONS.addOption( Option.builder( HELP )
81 .longOpt( "help" )
82 .desc( "Display help information." )
83 .build() );
84 OPTIONS.addOption( Option.builder( VERSION )
85 .longOpt( "version" )
86 .desc( "Display version information." )
87 .build() );
88 OPTIONS.addOption( Option.builder( IN )
89 .longOpt( "input" )
90 .desc( "Input file or directory." )
91 .hasArg()
92 .build() );
93 OPTIONS.addOption( Option.builder( OUT )
94 .longOpt( "output" )
95 .desc( "Output file or directory." )
96 .hasArg()
97 .build() );
98 OPTIONS.addOption( Option.builder( FROM )
99 .desc( "From format. If not specified, try to autodetect it." )
100 .hasArg()
101 .build() );
102 OPTIONS.addOption( Option.builder( TO )
103 .desc( "To format." )
104 .hasArg()
105 .build() );
106 OPTIONS.addOption( Option.builder( INENCODING )
107 .desc( "Input file encoding. If not specified, try to autodetect it." )
108 .hasArg()
109 .build() );
110 OPTIONS.addOption( Option.builder( FORMAT )
111 .longOpt( "format" )
112 .desc( "Format the output (actually only xml based outputs) to be human readable." )
113 .build() );
114 OPTIONS.addOption( Option.builder( OUTENCODING )
115 .desc( "Output file encoding. If not specified, use the input encoding (or autodetected)." )
116 .hasArg()
117 .build() );
118 OPTIONS.addOption( Option.builder( DEBUG )
119 .longOpt( "debug" )
120 .desc( "Produce execution debug output." )
121 .build() );
122 OPTIONS.addOption( Option.builder( ERRORS )
123 .longOpt( "errors" )
124 .desc( "Produce execution error messages." )
125 .build() );
126 }
127
128
129
130
131
132
133
134 CommandLine parse( String[] args )
135 throws ParseException
136 {
137 if ( args == null )
138 {
139 throw new IllegalArgumentException( "args is required." );
140 }
141
142 DefaultParser parser = new DefaultParser();
143 return parser.parse( OPTIONS, args );
144 }
145
146 static void displayHelp()
147 {
148 System.out.println();
149
150 HelpFormatter formatter = new HelpFormatter();
151 formatter.setWidth( 128 );
152 formatter.printHelp( "doxia-converter", "\nOptions:", OPTIONS, getSupportedFormatAndEncoding(), true );
153 }
154
155 private static String getSupportedFormatAndEncoding()
156 {
157 return getSupportedFormat() + "\n" + getSupportedEncoding();
158 }
159
160 private static String getSupportedFormat()
161 {
162 return "\nSupported Formats:\n from: " + join( DefaultConverter.SUPPORTED_FROM_FORMAT, ", " )
163 + " or autodetect" + "\n to: " + join( DefaultConverter.SUPPORTED_TO_FORMAT, ", " )
164 + "\n";
165 }
166
167 private static String getSupportedEncoding()
168 {
169 return "\nSupported Encoding:\n " + join( CharsetDetector.getAllDetectableCharsets(), ", " );
170 }
171 }