View Javadoc
1   package org.apache.maven.doxia.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Manager for Doxia converter CLI options.
36   *
37   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
38   */
39  class CLIManager
40  {
41      /** h character */
42      static final String HELP = "h";
43  
44      /** v character */
45      static final String VERSION = "v";
46  
47      /** in String */
48      static final String IN = "in";
49  
50      /** out String */
51      static final String OUT = "out";
52  
53      /** from String */
54      static final String FROM = "from";
55  
56      /** to String */
57      static final String TO = "to";
58  
59      /** inEncoding String */
60      static final String INENCODING = "inEncoding";
61  
62      /** f character */
63      static final String FORMAT = "f";
64  
65      /** outEncoding String */
66      static final String OUTENCODING = "outEncoding";
67  
68      /** X character */
69      static final String DEBUG = "X";
70  
71      /** e character */
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      * @param args not null.
130      * @return a not null command line.
131      * @throws ParseException if any
132      * @throws IllegalArgumentException is args is null
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 }