1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.core.tools.picocli;
18
19 import java.io.File;
20 import java.io.PrintStream;
21 import java.lang.annotation.ElementType;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.annotation.Target;
25 import java.lang.reflect.Array;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.Field;
28 import java.lang.reflect.ParameterizedType;
29 import java.lang.reflect.Type;
30 import java.lang.reflect.WildcardType;
31 import java.math.BigDecimal;
32 import java.math.BigInteger;
33 import java.net.InetAddress;
34 import java.net.MalformedURLException;
35 import java.net.URI;
36 import java.net.URISyntaxException;
37 import java.net.URL;
38 import java.nio.charset.Charset;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.sql.Time;
42 import java.text.BreakIterator;
43 import java.text.ParseException;
44 import java.text.SimpleDateFormat;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.Comparator;
50 import java.util.Date;
51 import java.util.HashMap;
52 import java.util.HashSet;
53 import java.util.LinkedHashMap;
54 import java.util.LinkedHashSet;
55 import java.util.LinkedList;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Queue;
59 import java.util.Set;
60 import java.util.SortedSet;
61 import java.util.Stack;
62 import java.util.TreeSet;
63 import java.util.UUID;
64 import java.util.concurrent.Callable;
65 import java.util.regex.Pattern;
66
67 import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Ansi.IStyle;
68 import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Ansi.Style;
69 import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Ansi.Text;
70
71 import static java.util.Locale.ENGLISH;
72 import static org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Column.Overflow.SPAN;
73 import static org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Column.Overflow.TRUNCATE;
74 import static org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Column.Overflow.WRAP;
75
76 /**
77 * <p>
78 * CommandLine interpreter that uses reflection to initialize an annotated domain object with values obtained from the
79 * command line arguments.
80 * </p><h2>Example</h2>
81 * <pre>import static picocli.CommandLine.*;
82 *
83 * @Command(header = "Encrypt FILE(s), or standard input, to standard output or to the output file.",
84 * version = "v1.2.3")
85 * public class Encrypt {
86 *
87 * @Parameters(type = File.class, description = "Any number of input files")
88 * private List<File> files = new ArrayList<File>();
89 *
90 * @Option(names = { "-o", "--out" }, description = "Output file (default: print to console)")
91 * private File outputFile;
92 *
93 * @Option(names = { "-v", "--verbose"}, description = "Verbosely list files processed")
94 * private boolean verbose;
95 *
96 * @Option(names = { "-h", "--help", "-?", "-help"}, usageHelp = true, description = "Display this help and exit")
97 * private boolean help;
98 *
99 * @Option(names = { "-V", "--version"}, versionHelp = true, description = "Display version info and exit")
100 * private boolean versionHelp;
101 * }
102 * </pre>
103 * <p>
104 * Use {@code CommandLine} to initialize a domain object as follows:
105 * </p><pre>
106 * public static void main(String... args) {
107 * Encrypt encrypt = new Encrypt();
108 * try {
109 * List<CommandLine> parsedCommands = new CommandLine(encrypt).parse(args);
110 * if (!CommandLine.printHelpIfRequested(parsedCommands, System.err, Help.Ansi.AUTO)) {
111 * runProgram(encrypt);
112 * }
113 * } catch (ParameterException ex) { // command line arguments could not be parsed
114 * System.err.println(ex.getMessage());
115 * ex.getCommandLine().usage(System.err);
116 * }
117 * }
118 * </pre><p>
119 * Invoke the above program with some command line arguments. The below are all equivalent:
120 * </p>
121 * <pre>
122 * --verbose --out=outfile in1 in2
123 * --verbose --out outfile in1 in2
124 * -v --out=outfile in1 in2
125 * -v -o outfile in1 in2
126 * -v -o=outfile in1 in2
127 * -vo outfile in1 in2
128 * -vo=outfile in1 in2
129 * -v -ooutfile in1 in2
130 * -vooutfile in1 in2
131 * </pre>
132 */
133 public class CommandLine {
134 /** This is picocli version {@value}. */
135 public static final String VERSION = "2.0.3";
136
137 private final Tracer tracer = new Tracer();
138 private final Interpreter interpreter;
139 private String commandName = Help.DEFAULT_COMMAND_NAME;
140 private boolean overwrittenOptionsAllowed = false;
141 private boolean unmatchedArgumentsAllowed = false;
142 private List<String> unmatchedArguments = new ArrayList<String>();
143 private CommandLine parent;
144 private boolean usageHelpRequested;
145 private boolean versionHelpRequested;
146 private List<String> versionLines = new ArrayList<String>();
147
148 /**
149 * Constructs a new {@code CommandLine} interpreter with the specified annotated object.
150 * When the {@link #parse(String...)} method is called, fields of the specified object that are annotated
151 * with {@code @Option} or {@code @Parameters} will be initialized based on command line arguments.
152 * @param command the object to initialize from the command line arguments
153 * @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
154 */
155 public CommandLine(Object command) {
156 interpreter = new Interpreter(command);
157 }
158
159 /** Registers a subcommand with the specified name. For example:
160 * <pre>
161 * CommandLine commandLine = new CommandLine(new Git())
162 * .addSubcommand("status", new GitStatus())
163 * .addSubcommand("commit", new GitCommit();
164 * .addSubcommand("add", new GitAdd())
165 * .addSubcommand("branch", new GitBranch())
166 * .addSubcommand("checkout", new GitCheckout())
167 * //...
168 * ;
169 * </pre>
170 *
171 * <p>The specified object can be an annotated object or a
172 * {@code CommandLine} instance with its own nested subcommands. For example:</p>
173 * <pre>
174 * CommandLine commandLine = new CommandLine(new MainCommand())
175 * .addSubcommand("cmd1", new ChildCommand1()) // subcommand
176 * .addSubcommand("cmd2", new ChildCommand2())
177 * .addSubcommand("cmd3", new CommandLine(new ChildCommand3()) // subcommand with nested sub-subcommands
178 * .addSubcommand("cmd3sub1", new GrandChild3Command1())
179 * .addSubcommand("cmd3sub2", new GrandChild3Command2())
180 * .addSubcommand("cmd3sub3", new CommandLine(new GrandChild3Command3()) // deeper nesting
181 * .addSubcommand("cmd3sub3sub1", new GreatGrandChild3Command3_1())
182 * .addSubcommand("cmd3sub3sub2", new GreatGrandChild3Command3_2())
183 * )
184 * );
185 * </pre>
186 * <p>The default type converters are available on all subcommands and nested sub-subcommands, but custom type
187 * converters are registered only with the subcommand hierarchy as it existed when the custom type was registered.
188 * To ensure a custom type converter is available to all subcommands, register the type converter last, after
189 * adding subcommands.</p>
190 * <p>See also the {@link Command#subcommands()} annotation to register subcommands declaratively.</p>
191 *
192 * @param name the string to recognize on the command line as a subcommand
193 * @param command the object to initialize with command line arguments following the subcommand name.
194 * This may be a {@code CommandLine} instance with its own (nested) subcommands
195 * @return this CommandLine object, to allow method chaining
196 * @see #registerConverter(Class, ITypeConverter)
197 * @since 0.9.7
198 * @see Command#subcommands()
199 */
200 public CommandLine addSubcommand(String name, Object command) {
201 CommandLine commandLine = toCommandLine(command);
202 commandLine.parent = this;
203 interpreter.commands.put(name, commandLine);
204 return this;
205 }
206 /** Returns a map with the subcommands {@linkplain #addSubcommand(String, Object) registered} on this instance.
207 * @return a map with the registered subcommands
208 * @since 0.9.7
209 */
210 public Map<String, CommandLine> getSubcommands() {
211 return new LinkedHashMap<String, CommandLine>(interpreter.commands);
212 }
213 /**
214 * Returns the command that this is a subcommand of, or {@code null} if this is a top-level command.
215 * @return the command that this is a subcommand of, or {@code null} if this is a top-level command
216 * @see #addSubcommand(String, Object)
217 * @see Command#subcommands()
218 * @since 0.9.8
219 */
220 public CommandLine getParent() {
221 return parent;
222 }
223
224 /** Returns the annotated object that this {@code CommandLine} instance was constructed with.
225 * @param <T> the type of the variable that the return value is being assigned to
226 * @return the annotated object that this {@code CommandLine} instance was constructed with
227 * @since 0.9.7
228 */
229 public <T> T getCommand() {
230 return (T) interpreter.command;
231 }
232
233 /** Returns {@code true} if an option annotated with {@link Option#usageHelp()} was specified on the command line.
234 * @return whether the parser encountered an option annotated with {@link Option#usageHelp()}.
235 * @since 0.9.8 */
236 public boolean isUsageHelpRequested() { return usageHelpRequested; }
237
238 /** Returns {@code true} if an option annotated with {@link Option#versionHelp()} was specified on the command line.
239 * @return whether the parser encountered an option annotated with {@link Option#versionHelp()}.
240 * @since 0.9.8 */
241 public boolean isVersionHelpRequested() { return versionHelpRequested; }
242
243 /** Returns whether options for single-value fields can be specified multiple times on the command line.
244 * The default is {@code false} and a {@link OverwrittenOptionException} is thrown if this happens.
245 * When {@code true}, the last specified value is retained.
246 * @return {@code true} if options for single-value fields can be specified multiple times on the command line, {@code false} otherwise
247 * @since 0.9.7
248 */
249 public boolean isOverwrittenOptionsAllowed() {
250 return overwrittenOptionsAllowed;
251 }
252
253 /** Sets whether options for single-value fields can be specified multiple times on the command line without a {@link OverwrittenOptionException} being thrown.
254 * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its
255 * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added
256 * later will have the default setting. To ensure a setting is applied to all
257 * subcommands, call the setter last, after adding subcommands.</p>
258 * @param newValue the new setting
259 * @return this {@code CommandLine} object, to allow method chaining
260 * @since 0.9.7
261 */
262 public CommandLine setOverwrittenOptionsAllowed(boolean newValue) {
263 this.overwrittenOptionsAllowed = newValue;
264 for (CommandLine command : interpreter.commands.values()) {
265 command.setOverwrittenOptionsAllowed(newValue);
266 }
267 return this;
268 }
269
270 /** Returns whether the end user may specify arguments on the command line that are not matched to any option or parameter fields.
271 * The default is {@code false} and a {@link UnmatchedArgumentException} is thrown if this happens.
272 * When {@code true}, the last unmatched arguments are available via the {@link #getUnmatchedArguments()} method.
273 * @return {@code true} if the end use may specify unmatched arguments on the command line, {@code false} otherwise
274 * @see #getUnmatchedArguments()
275 * @since 0.9.7
276 */
277 public boolean isUnmatchedArgumentsAllowed() {
278 return unmatchedArgumentsAllowed;
279 }
280
281 /** Sets whether the end user may specify unmatched arguments on the command line without a {@link UnmatchedArgumentException} being thrown.
282 * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its
283 * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added
284 * later will have the default setting. To ensure a setting is applied to all
285 * subcommands, call the setter last, after adding subcommands.</p>
286 * @param newValue the new setting. When {@code true}, the last unmatched arguments are available via the {@link #getUnmatchedArguments()} method.
287 * @return this {@code CommandLine} object, to allow method chaining
288 * @since 0.9.7
289 * @see #getUnmatchedArguments()
290 */
291 public CommandLine setUnmatchedArgumentsAllowed(boolean newValue) {
292 this.unmatchedArgumentsAllowed = newValue;
293 for (CommandLine command : interpreter.commands.values()) {
294 command.setUnmatchedArgumentsAllowed(newValue);
295 }
296 return this;
297 }
298
299 /** Returns the list of unmatched command line arguments, if any.
300 * @return the list of unmatched command line arguments or an empty list
301 * @see #isUnmatchedArgumentsAllowed()
302 * @since 0.9.7
303 */
304 public List<String> getUnmatchedArguments() {
305 return unmatchedArguments;
306 }
307
308 /**
309 * <p>
310 * Convenience method that initializes the specified annotated object from the specified command line arguments.
311 * </p><p>
312 * This is equivalent to
313 * </p><pre>
314 * CommandLine cli = new CommandLine(command);
315 * cli.parse(args);
316 * return command;
317 * </pre>
318 *
319 * @param command the object to initialize. This object contains fields annotated with
320 * {@code @Option} or {@code @Parameters}.
321 * @param args the command line arguments to parse
322 * @param <T> the type of the annotated object
323 * @return the specified annotated object
324 * @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
325 * @throws ParameterException if the specified command line arguments are invalid
326 * @since 0.9.7
327 */
328 public static <T> T populateCommand(T command, String... args) {
329 CommandLine cli = toCommandLine(command);
330 cli.parse(args);
331 return command;
332 }
333
334 /** Parses the specified command line arguments and returns a list of {@code CommandLine} objects representing the
335 * top-level command and any subcommands (if any) that were recognized and initialized during the parsing process.
336 * <p>
337 * If parsing succeeds, the first element in the returned list is always {@code this CommandLine} object. The
338 * returned list may contain more elements if subcommands were {@linkplain #addSubcommand(String, Object) registered}
339 * and these subcommands were initialized by matching command line arguments. If parsing fails, a
340 * {@link ParameterException} is thrown.
341 * </p>
342 *
343 * @param args the command line arguments to parse
344 * @return a list with the top-level command and any subcommands initialized by this method
345 * @throws ParameterException if the specified command line arguments are invalid; use
346 * {@link ParameterException#getCommandLine()} to get the command or subcommand whose user input was invalid
347 */
348 public List<CommandLine> parse(String... args) {
349 return interpreter.parse(args);
350 }
351 /**
352 * Represents a function that can process a List of {@code CommandLine} objects resulting from successfully
353 * {@linkplain #parse(String...) parsing} the command line arguments. This is a
354 * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">functional interface</a>
355 * whose functional method is {@link #handleParseResult(List, PrintStream, CommandLine.Help.Ansi)}.
356 * <p>
357 * Implementations of this functions can be passed to the {@link #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...) CommandLine::parseWithHandler}
358 * methods to take some next step after the command line was successfully parsed.
359 * </p>
360 * @see RunFirst
361 * @see RunLast
362 * @see RunAll
363 * @since 2.0 */
364 public static interface IParseResultHandler {
365 /** Processes a List of {@code CommandLine} objects resulting from successfully
366 * {@linkplain #parse(String...) parsing} the command line arguments and optionally returns a list of results.
367 * @param parsedCommands the {@code CommandLine} objects that resulted from successfully parsing the command line arguments
368 * @param out the {@code PrintStream} to print help to if requested
369 * @param ansi for printing help messages using ANSI styles and colors
370 * @return a list of results, or an empty list if there are no results
371 * @throws ExecutionException if a problem occurred while processing the parse results; use
372 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
373 */
374 List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) throws ExecutionException;
375 }
376 /**
377 * Represents a function that can handle a {@code ParameterException} that occurred while
378 * {@linkplain #parse(String...) parsing} the command line arguments. This is a
379 * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">functional interface</a>
380 * whose functional method is {@link #handleException(CommandLine.ParameterException, PrintStream, CommandLine.Help.Ansi, String...)}.
381 * <p>
382 * Implementations of this functions can be passed to the {@link #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...) CommandLine::parseWithHandler}
383 * methods to handle situations when the command line could not be parsed.
384 * </p>
385 * @see DefaultExceptionHandler
386 * @since 2.0 */
387 public static interface IExceptionHandler {
388 /** Handles a {@code ParameterException} that occurred while {@linkplain #parse(String...) parsing} the command
389 * line arguments and optionally returns a list of results.
390 * @param ex the ParameterException describing the problem that occurred while parsing the command line arguments,
391 * and the CommandLine representing the command or subcommand whose input was invalid
392 * @param out the {@code PrintStream} to print help to if requested
393 * @param ansi for printing help messages using ANSI styles and colors
394 * @param args the command line arguments that could not be parsed
395 * @return a list of results, or an empty list if there are no results
396 */
397 List<Object> handleException(ParameterException ex, PrintStream out, Help.Ansi ansi, String... args);
398 }
399 /**
400 * Default exception handler that prints the exception message to the specified {@code PrintStream}, followed by the
401 * usage message for the command or subcommand whose input was invalid.
402 * <p>Implementation roughly looks like this:</p>
403 * <pre>
404 * System.err.println(paramException.getMessage());
405 * paramException.getCommandLine().usage(System.err);
406 * </pre>
407 * @since 2.0 */
408 public static class DefaultExceptionHandler implements IExceptionHandler {
409 @Override
410 public List<Object> handleException(ParameterException ex, PrintStream out, Help.Ansi ansi, String... args) {
411 out.println(ex.getMessage());
412 ex.getCommandLine().usage(out, ansi);
413 return Collections.emptyList();
414 }
415 }
416 /**
417 * Helper method that may be useful when processing the list of {@code CommandLine} objects that result from successfully
418 * {@linkplain #parse(String...) parsing} command line arguments. This method prints out
419 * {@linkplain #usage(PrintStream, Help.Ansi) usage help} if {@linkplain #isUsageHelpRequested() requested}
420 * or {@linkplain #printVersionHelp(PrintStream, Help.Ansi) version help} if {@linkplain #isVersionHelpRequested() requested}
421 * and returns {@code true}. Otherwise, if none of the specified {@code CommandLine} objects have help requested,
422 * this method returns {@code false}.
423 * <p>
424 * Note that this method <em>only</em> looks at the {@link Option#usageHelp() usageHelp} and
425 * {@link Option#versionHelp() versionHelp} attributes. The {@link Option#help() help} attribute is ignored.
426 * </p>
427 * @param parsedCommands the list of {@code CommandLine} objects to check if help was requested
428 * @param out the {@code PrintStream} to print help to if requested
429 * @param ansi for printing help messages using ANSI styles and colors
430 * @return {@code true} if help was printed, {@code false} otherwise
431 * @since 2.0 */
432 public static boolean printHelpIfRequested(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) {
433 for (CommandLine parsed : parsedCommands) {
434 if (parsed.isUsageHelpRequested()) {
435 parsed.usage(out, ansi);
436 return true;
437 } else if (parsed.isVersionHelpRequested()) {
438 parsed.printVersionHelp(out, ansi);
439 return true;
440 }
441 }
442 return false;
443 }
444 private static Object execute(CommandLine parsed) {
445 Object command = parsed.getCommand();
446 if (command instanceof Runnable) {
447 try {
448 ((Runnable) command).run();
449 return null;
450 } catch (Exception ex) {
451 throw new ExecutionException(parsed, "Error while running command (" + command + ")", ex);
452 }
453 } else if (command instanceof Callable) {
454 try {
455 return ((Callable<Object>) command).call();
456 } catch (Exception ex) {
457 throw new ExecutionException(parsed, "Error while calling command (" + command + ")", ex);
458 }
459 }
460 throw new ExecutionException(parsed, "Parsed command (" + command + ") is not Runnable or Callable");
461 }
462 /**
463 * Command line parse result handler that prints help if requested, and otherwise executes the top-level
464 * {@code Runnable} or {@code Callable} command.
465 * For use in the {@link #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...) parseWithHandler} methods.
466 * <p>
467 * From picocli v2.0, {@code RunFirst} is used to implement the {@link #run(Runnable, PrintStream, Help.Ansi, String...) run}
468 * and {@link #call(Callable, PrintStream, Help.Ansi, String...) call} convenience methods.
469 * </p>
470 * @since 2.0 */
471 public static class RunFirst implements IParseResultHandler {
472 /** Prints help if requested, and otherwise executes the top-level {@code Runnable} or {@code Callable} command.
473 * If the top-level command does not implement either {@code Runnable} or {@code Callable}, a {@code ExecutionException}
474 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
475 *
476 * @param parsedCommands the {@code CommandLine} objects that resulted from successfully parsing the command line arguments
477 * @param out the {@code PrintStream} to print help to if requested
478 * @param ansi for printing help messages using ANSI styles and colors
479 * @return an empty list if help was requested, or a list containing a single element: the result of calling the
480 * {@code Callable}, or a {@code null} element if the top-level command was a {@code Runnable}
481 * @throws ExecutionException if a problem occurred while processing the parse results; use
482 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
483 */
484 public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) {
485 if (printHelpIfRequested(parsedCommands, out, ansi)) { return Collections.emptyList(); }
486 return Arrays.asList(execute(parsedCommands.get(0)));
487 }
488 }
489 /**
490 * Command line parse result handler that prints help if requested, and otherwise executes the most specific
491 * {@code Runnable} or {@code Callable} subcommand.
492 * For use in the {@link #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...) parseWithHandler} methods.
493 * <p>
494 * Something like this:</p>
495 * <pre>
496 * // RunLast implementation: print help if requested, otherwise execute the most specific subcommand
497 * if (CommandLine.printHelpIfRequested(parsedCommands, System.err, Help.Ansi.AUTO)) {
498 * return emptyList();
499 * }
500 * CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
501 * Object command = last.getCommand();
502 * if (command instanceof Runnable) {
503 * try {
504 * ((Runnable) command).run();
505 * } catch (Exception ex) {
506 * throw new ExecutionException(last, "Error in runnable " + command, ex);
507 * }
508 * } else if (command instanceof Callable) {
509 * Object result;
510 * try {
511 * result = ((Callable) command).call();
512 * } catch (Exception ex) {
513 * throw new ExecutionException(last, "Error in callable " + command, ex);
514 * }
515 * // ...do something with result
516 * } else {
517 * throw new ExecutionException(last, "Parsed command (" + command + ") is not Runnable or Callable");
518 * }
519 * </pre>
520 * @since 2.0 */
521 public static class RunLast implements IParseResultHandler {
522 /** Prints help if requested, and otherwise executes the most specific {@code Runnable} or {@code Callable} subcommand.
523 * If the last (sub)command does not implement either {@code Runnable} or {@code Callable}, a {@code ExecutionException}
524 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
525 *
526 * @param parsedCommands the {@code CommandLine} objects that resulted from successfully parsing the command line arguments
527 * @param out the {@code PrintStream} to print help to if requested
528 * @param ansi for printing help messages using ANSI styles and colors
529 * @return an empty list if help was requested, or a list containing a single element: the result of calling the
530 * {@code Callable}, or a {@code null} element if the last (sub)command was a {@code Runnable}
531 * @throws ExecutionException if a problem occurred while processing the parse results; use
532 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
533 */
534 public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) {
535 if (printHelpIfRequested(parsedCommands, out, ansi)) { return Collections.emptyList(); }
536 CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
537 return Arrays.asList(execute(last));
538 }
539 }
540 /**
541 * Command line parse result handler that prints help if requested, and otherwise executes the top-level command and
542 * all subcommands as {@code Runnable} or {@code Callable}.
543 * For use in the {@link #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...) parseWithHandler} methods.
544 * @since 2.0 */
545 public static class RunAll implements IParseResultHandler {
546 /** Prints help if requested, and otherwise executes the top-level command and all subcommands as {@code Runnable}
547 * or {@code Callable}. If any of the {@code CommandLine} commands does not implement either
548 * {@code Runnable} or {@code Callable}, a {@code ExecutionException}
549 * is thrown detailing the problem and capturing the offending {@code CommandLine} object.
550 *
551 * @param parsedCommands the {@code CommandLine} objects that resulted from successfully parsing the command line arguments
552 * @param out the {@code PrintStream} to print help to if requested
553 * @param ansi for printing help messages using ANSI styles and colors
554 * @return an empty list if help was requested, or a list containing the result of executing all commands:
555 * the return values from calling the {@code Callable} commands, {@code null} elements for commands that implement {@code Runnable}
556 * @throws ExecutionException if a problem occurred while processing the parse results; use
557 * {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
558 */
559 public List<Object> handleParseResult(List<CommandLine> parsedCommands, PrintStream out, Help.Ansi ansi) {
560 if (printHelpIfRequested(parsedCommands, out, ansi)) {
561 return null;
562 }
563 List<Object> result = new ArrayList<Object>();
564 for (CommandLine parsed : parsedCommands) {
565 result.add(execute(parsed));
566 }
567 return result;
568 }
569 }
570 /**
571 * Returns the result of calling {@link #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...)}
572 * with {@code Help.Ansi.AUTO} and a new {@link DefaultExceptionHandler} in addition to the specified parse result handler,
573 * {@code PrintStream}, and the specified command line arguments.
574 * <p>
575 * This is a convenience method intended to offer the same ease of use as the {@link #run(Runnable, PrintStream, Help.Ansi, String...) run}
576 * and {@link #call(Callable, PrintStream, Help.Ansi, String...) call} methods, but with more flexibility and better
577 * support for nested subcommands.
578 * </p>
579 * <p>Calling this method roughly expands to:</p>
580 * <pre>
581 * try {
582 * List<CommandLine> parsedCommands = parse(args);
583 * return parseResultsHandler.handleParseResult(parsedCommands, out, Help.Ansi.AUTO);
584 * } catch (ParameterException ex) {
585 * return new DefaultExceptionHandler().handleException(ex, out, ansi, args);
586 * }
587 * </pre>
588 * <p>
589 * Picocli provides some default handlers that allow you to accomplish some common tasks with very little code.
590 * The following handlers are available:</p>
591 * <ul>
592 * <li>{@link RunLast} handler prints help if requested, and otherwise gets the last specified command or subcommand
593 * and tries to execute it as a {@code Runnable} or {@code Callable}.</li>
594 * <li>{@link RunFirst} handler prints help if requested, and otherwise executes the top-level command as a {@code Runnable} or {@code Callable}.</li>
595 * <li>{@link RunAll} handler prints help if requested, and otherwise executes all recognized commands and subcommands as {@code Runnable} or {@code Callable} tasks.</li>
596 * <li>{@link DefaultExceptionHandler} prints the error message followed by usage help</li>
597 * </ul>
598 * @param handler the function that will process the result of successfully parsing the command line arguments
599 * @param out the {@code PrintStream} to print help to if requested
600 * @param args the command line arguments
601 * @return a list of results, or an empty list if there are no results
602 * @throws ExecutionException if the command line arguments were parsed successfully but a problem occurred while processing the
603 * parse results; use {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
604 * @see RunLast
605 * @see RunAll
606 * @since 2.0 */
607 public List<Object> parseWithHandler(IParseResultHandler handler, PrintStream out, String... args) {
608 return parseWithHandlers(handler, out, Help.Ansi.AUTO, new DefaultExceptionHandler(), args);
609 }
610 /**
611 * Tries to {@linkplain #parse(String...) parse} the specified command line arguments, and if successful, delegates
612 * the processing of the resulting list of {@code CommandLine} objects to the specified {@linkplain IParseResultHandler handler}.
613 * If the command line arguments were invalid, the {@code ParameterException} thrown from the {@code parse} method
614 * is caught and passed to the specified {@link IExceptionHandler}.
615 * <p>
616 * This is a convenience method intended to offer the same ease of use as the {@link #run(Runnable, PrintStream, Help.Ansi, String...) run}
617 * and {@link #call(Callable, PrintStream, Help.Ansi, String...) call} methods, but with more flexibility and better
618 * support for nested subcommands.
619 * </p>
620 * <p>Calling this method roughly expands to:</p>
621 * <pre>
622 * try {
623 * List<CommandLine> parsedCommands = parse(args);
624 * return parseResultsHandler.handleParseResult(parsedCommands, out, ansi);
625 * } catch (ParameterException ex) {
626 * return new exceptionHandler.handleException(ex, out, ansi, args);
627 * }
628 * </pre>
629 * <p>
630 * Picocli provides some default handlers that allow you to accomplish some common tasks with very little code.
631 * The following handlers are available:</p>
632 * <ul>
633 * <li>{@link RunLast} handler prints help if requested, and otherwise gets the last specified command or subcommand
634 * and tries to execute it as a {@code Runnable} or {@code Callable}.</li>
635 * <li>{@link RunFirst} handler prints help if requested, and otherwise executes the top-level command as a {@code Runnable} or {@code Callable}.</li>
636 * <li>{@link RunAll} handler prints help if requested, and otherwise executes all recognized commands and subcommands as {@code Runnable} or {@code Callable} tasks.</li>
637 * <li>{@link DefaultExceptionHandler} prints the error message followed by usage help</li>
638 * </ul>
639 *
640 * @param handler the function that will process the result of successfully parsing the command line arguments
641 * @param out the {@code PrintStream} to print help to if requested
642 * @param ansi for printing help messages using ANSI styles and colors
643 * @param exceptionHandler the function that can handle the {@code ParameterException} thrown when the command line arguments are invalid
644 * @param args the command line arguments
645 * @return a list of results produced by the {@code IParseResultHandler} or the {@code IExceptionHandler}, or an empty list if there are no results
646 * @throws ExecutionException if the command line arguments were parsed successfully but a problem occurred while processing the parse
647 * result {@code CommandLine} objects; use {@link ExecutionException#getCommandLine()} to get the command or subcommand where processing failed
648 * @see RunLast
649 * @see RunAll
650 * @see DefaultExceptionHandler
651 * @since 2.0 */
652 public List<Object> parseWithHandlers(IParseResultHandler handler, PrintStream out, Help.Ansi ansi, IExceptionHandler exceptionHandler, String... args) {
653 try {
654 List<CommandLine> result = parse(args);
655 return handler.handleParseResult(result, out, ansi);
656 } catch (ParameterException ex) {
657 return exceptionHandler.handleException(ex, out, ansi, args);
658 }
659 }
660 /**
661 * Equivalent to {@code new CommandLine(command).usage(out)}. See {@link #usage(PrintStream)} for details.
662 * @param command the object annotated with {@link Command}, {@link Option} and {@link Parameters}
663 * @param out the print stream to print the help message to
664 * @throws IllegalArgumentException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
665 */
666 public static void usage(Object command, PrintStream out) {
667 toCommandLine(command).usage(out);
668 }
669
670 /**
671 * Equivalent to {@code new CommandLine(command).usage(out, ansi)}.
672 * See {@link #usage(PrintStream, Help.Ansi)} for details.
673 * @param command the object annotated with {@link Command}, {@link Option} and {@link Parameters}
674 * @param out the print stream to print the help message to
675 * @param ansi whether the usage message should contain ANSI escape codes or not
676 * @throws IllegalArgumentException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
677 */
678 public static void usage(Object command, PrintStream out, Help.Ansi ansi) {
679 toCommandLine(command).usage(out, ansi);
680 }
681
682 /**
683 * Equivalent to {@code new CommandLine(command).usage(out, colorScheme)}.
684 * See {@link #usage(PrintStream, Help.ColorScheme)} for details.
685 * @param command the object annotated with {@link Command}, {@link Option} and {@link Parameters}
686 * @param out the print stream to print the help message to
687 * @param colorScheme the {@code ColorScheme} defining the styles for options, parameters and commands when ANSI is enabled
688 * @throws IllegalArgumentException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
689 */
690 public static void usage(Object command, PrintStream out, Help.ColorScheme colorScheme) {
691 toCommandLine(command).usage(out, colorScheme);
692 }
693
694 /**
695 * Delegates to {@link #usage(PrintStream, Help.Ansi)} with the {@linkplain Help.Ansi#AUTO platform default}.
696 * @param out the printStream to print to
697 * @see #usage(PrintStream, Help.ColorScheme)
698 */
699 public void usage(PrintStream out) {
700 usage(out, Help.Ansi.AUTO);
701 }
702
703 /**
704 * Delegates to {@link #usage(PrintStream, Help.ColorScheme)} with the {@linkplain Help#defaultColorScheme(CommandLine.Help.Ansi) default color scheme}.
705 * @param out the printStream to print to
706 * @param ansi whether the usage message should include ANSI escape codes or not
707 * @see #usage(PrintStream, Help.ColorScheme)
708 */
709 public void usage(PrintStream out, Help.Ansi ansi) {
710 usage(out, Help.defaultColorScheme(ansi));
711 }
712 /**
713 * Prints a usage help message for the annotated command class to the specified {@code PrintStream}.
714 * Delegates construction of the usage help message to the {@link Help} inner class and is equivalent to:
715 * <pre>
716 * Help help = new Help(command).addAllSubcommands(getSubcommands());
717 * StringBuilder sb = new StringBuilder()
718 * .append(help.headerHeading())
719 * .append(help.header())
720 * .append(help.synopsisHeading()) //e.g. Usage:
721 * .append(help.synopsis()) //e.g. <main class> [OPTIONS] <command> [COMMAND-OPTIONS] [ARGUMENTS]
722 * .append(help.descriptionHeading()) //e.g. %nDescription:%n%n
723 * .append(help.description()) //e.g. {"Converts foos to bars.", "Use options to control conversion mode."}
724 * .append(help.parameterListHeading()) //e.g. %nPositional parameters:%n%n
725 * .append(help.parameterList()) //e.g. [FILE...] the files to convert
726 * .append(help.optionListHeading()) //e.g. %nOptions:%n%n
727 * .append(help.optionList()) //e.g. -h, --help displays this help and exits
728 * .append(help.commandListHeading()) //e.g. %nCommands:%n%n
729 * .append(help.commandList()) //e.g. add adds the frup to the frooble
730 * .append(help.footerHeading())
731 * .append(help.footer());
732 * out.print(sb);
733 * </pre>
734 * <p>Annotate your class with {@link Command} to control many aspects of the usage help message, including
735 * the program name, text of section headings and section contents, and some aspects of the auto-generated sections
736 * of the usage help message.
737 * <p>To customize the auto-generated sections of the usage help message, like how option details are displayed,
738 * instantiate a {@link Help} object and use a {@link Help.TextTable} with more of fewer columns, a custom
739 * {@linkplain Help.Layout layout}, and/or a custom option {@linkplain Help.IOptionRenderer renderer}
740 * for ultimate control over which aspects of an Option or Field are displayed where.</p>
741 * @param out the {@code PrintStream} to print the usage help message to
742 * @param colorScheme the {@code ColorScheme} defining the styles for options, parameters and commands when ANSI is enabled
743 */
744 public void usage(PrintStream out, Help.ColorScheme colorScheme) {
745 Help help = new Help(interpreter.command, colorScheme).addAllSubcommands(getSubcommands());
746 if (!Help.DEFAULT_SEPARATOR.equals(getSeparator())) {
747 help.separator = getSeparator();
748 help.parameterLabelRenderer = help.createDefaultParamLabelRenderer(); // update for new separator
749 }
750 if (!Help.DEFAULT_COMMAND_NAME.equals(getCommandName())) {
751 help.commandName = getCommandName();
752 }
753 StringBuilder sb = new StringBuilder()
754 .append(help.headerHeading())
755 .append(help.header())
756 .append(help.synopsisHeading()) //e.g. Usage:
757 .append(help.synopsis(help.synopsisHeadingLength())) //e.g. <main class> [OPTIONS] <command> [COMMAND-OPTIONS] [ARGUMENTS]
758 .append(help.descriptionHeading()) //e.g. %nDescription:%n%n
759 .append(help.description()) //e.g. {"Converts foos to bars.", "Use options to control conversion mode."}
760 .append(help.parameterListHeading()) //e.g. %nPositional parameters:%n%n
761 .append(help.parameterList()) //e.g. [FILE...] the files to convert
762 .append(help.optionListHeading()) //e.g. %nOptions:%n%n
763 .append(help.optionList()) //e.g. -h, --help displays this help and exits
764 .append(help.commandListHeading()) //e.g. %nCommands:%n%n
765 .append(help.commandList()) //e.g. add adds the frup to the frooble
766 .append(help.footerHeading())
767 .append(help.footer());
768 out.print(sb);
769 }
770
771 /**
772 * Delegates to {@link #printVersionHelp(PrintStream, Help.Ansi)} with the {@linkplain Help.Ansi#AUTO platform default}.
773 * @param out the printStream to print to
774 * @see #printVersionHelp(PrintStream, Help.Ansi)
775 * @since 0.9.8
776 */
777 public void printVersionHelp(PrintStream out) { printVersionHelp(out, Help.Ansi.AUTO); }
778
779 /**
780 * Prints version information from the {@link Command#version()} annotation to the specified {@code PrintStream}.
781 * Each element of the array of version strings is printed on a separate line. Version strings may contain
782 * <a href="http://picocli.info/#_usage_help_with_styles_and_colors">markup for colors and style</a>.
783 * @param out the printStream to print to
784 * @param ansi whether the usage message should include ANSI escape codes or not
785 * @see Command#version()
786 * @see Option#versionHelp()
787 * @see #isVersionHelpRequested()
788 * @since 0.9.8
789 */
790 public void printVersionHelp(PrintStream out, Help.Ansi ansi) {
791 for (String versionInfo : versionLines) {
792 out.println(ansi.new Text(versionInfo));
793 }
794 }
795 /**
796 * Prints version information from the {@link Command#version()} annotation to the specified {@code PrintStream}.
797 * Each element of the array of version strings is {@linkplain String#format(String, Object...) formatted} with the
798 * specified parameters, and printed on a separate line. Both version strings and parameters may contain
799 * <a href="http://picocli.info/#_usage_help_with_styles_and_colors">markup for colors and style</a>.
800 * @param out the printStream to print to
801 * @param ansi whether the usage message should include ANSI escape codes or not
802 * @param params Arguments referenced by the format specifiers in the version strings
803 * @see Command#version()
804 * @see Option#versionHelp()
805 * @see #isVersionHelpRequested()
806 * @since 1.0.0
807 */
808 public void printVersionHelp(PrintStream out, Help.Ansi ansi, Object... params) {
809 for (String versionInfo : versionLines) {
810 out.println(ansi.new Text(String.format(versionInfo, params)));
811 }
812 }
813
814 /**
815 * Delegates to {@link #call(Callable, PrintStream, Help.Ansi, String...)} with {@link Help.Ansi#AUTO}.
816 * <p>
817 * From picocli v2.0, this method prints usage help or version help if {@linkplain #printHelpIfRequested(List, PrintStream, Help.Ansi) requested},
818 * and any exceptions thrown by the {@code Callable} are caught and rethrown wrapped in an {@code ExecutionException}.
819 * </p>
820 * @param callable the command to call when {@linkplain #parse(String...) parsing} succeeds.
821 * @param out the printStream to print to
822 * @param args the command line arguments to parse
823 * @param <C> the annotated object must implement Callable
824 * @param <T> the return type of the most specific command (must implement {@code Callable})
825 * @see #call(Callable, PrintStream, Help.Ansi, String...)
826 * @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
827 * @throws ExecutionException if the Callable throws an exception
828 * @return {@code null} if an error occurred while parsing the command line options, otherwise returns the result of calling the Callable
829 * @see #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...)
830 * @see RunFirst
831 */
832 public static <C extends Callable<T>, T> T call(C callable, PrintStream out, String... args) {
833 return call(callable, out, Help.Ansi.AUTO, args);
834 }
835 /**
836 * Convenience method to allow command line application authors to avoid some boilerplate code in their application.
837 * The annotated object needs to implement {@link Callable}. Calling this method is equivalent to:
838 * <pre>
839 * CommandLine cmd = new CommandLine(callable);
840 * List<CommandLine> parsedCommands;
841 * try {
842 * parsedCommands = cmd.parse(args);
843 * } catch (ParameterException ex) {
844 * out.println(ex.getMessage());
845 * cmd.usage(out, ansi);
846 * return null;
847 * }
848 * if (CommandLine.printHelpIfRequested(parsedCommands, out, ansi)) {
849 * return null;
850 * }
851 * CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
852 * try {
853 * Callable<Object> subcommand = last.getCommand();
854 * return subcommand.call();
855 * } catch (Exception ex) {
856 * throw new ExecutionException(last, "Error calling " + last.getCommand(), ex);
857 * }
858 * </pre>
859 * <p>
860 * If the specified Callable command has subcommands, the {@linkplain RunLast last} subcommand specified on the
861 * command line is executed.
862 * Commands with subcommands may be interested in calling the {@link #parseWithHandler(IParseResultHandler, PrintStream, String...) parseWithHandler}
863 * method with a {@link RunAll} handler or a custom handler.
864 * </p><p>
865 * From picocli v2.0, this method prints usage help or version help if {@linkplain #printHelpIfRequested(List, PrintStream, Help.Ansi) requested},
866 * and any exceptions thrown by the {@code Callable} are caught and rethrown wrapped in an {@code ExecutionException}.
867 * </p>
868 * @param callable the command to call when {@linkplain #parse(String...) parsing} succeeds.
869 * @param out the printStream to print to
870 * @param ansi whether the usage message should include ANSI escape codes or not
871 * @param args the command line arguments to parse
872 * @param <C> the annotated object must implement Callable
873 * @param <T> the return type of the specified {@code Callable}
874 * @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
875 * @throws ExecutionException if the Callable throws an exception
876 * @return {@code null} if an error occurred while parsing the command line options, or if help was requested and printed. Otherwise returns the result of calling the Callable
877 * @see #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...)
878 * @see RunLast
879 */
880 public static <C extends Callable<T>, T> T call(C callable, PrintStream out, Help.Ansi ansi, String... args) {
881 CommandLine cmd = new CommandLine(callable); // validate command outside of try-catch
882 List<Object> results = cmd.parseWithHandlers(new RunLast(), out, ansi, new DefaultExceptionHandler(), args);
883 return results == null || results.isEmpty() ? null : (T) results.get(0);
884 }
885
886 /**
887 * Delegates to {@link #run(Runnable, PrintStream, Help.Ansi, String...)} with {@link Help.Ansi#AUTO}.
888 * <p>
889 * From picocli v2.0, this method prints usage help or version help if {@linkplain #printHelpIfRequested(List, PrintStream, Help.Ansi) requested},
890 * and any exceptions thrown by the {@code Runnable} are caught and rethrown wrapped in an {@code ExecutionException}.
891 * </p>
892 * @param runnable the command to run when {@linkplain #parse(String...) parsing} succeeds.
893 * @param out the printStream to print to
894 * @param args the command line arguments to parse
895 * @param <R> the annotated object must implement Runnable
896 * @see #run(Runnable, PrintStream, Help.Ansi, String...)
897 * @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
898 * @throws ExecutionException if the Runnable throws an exception
899 * @see #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...)
900 * @see RunFirst
901 */
902 public static <R extends Runnable> void run(R runnable, PrintStream out, String... args) {
903 run(runnable, out, Help.Ansi.AUTO, args);
904 }
905 /**
906 * Convenience method to allow command line application authors to avoid some boilerplate code in their application.
907 * The annotated object needs to implement {@link Runnable}. Calling this method is equivalent to:
908 * <pre>
909 * CommandLine cmd = new CommandLine(runnable);
910 * List<CommandLine> parsedCommands;
911 * try {
912 * parsedCommands = cmd.parse(args);
913 * } catch (ParameterException ex) {
914 * out.println(ex.getMessage());
915 * cmd.usage(out, ansi);
916 * return null;
917 * }
918 * if (CommandLine.printHelpIfRequested(parsedCommands, out, ansi)) {
919 * return null;
920 * }
921 * CommandLine last = parsedCommands.get(parsedCommands.size() - 1);
922 * try {
923 * Runnable subcommand = last.getCommand();
924 * subcommand.run();
925 * } catch (Exception ex) {
926 * throw new ExecutionException(last, "Error running " + last.getCommand(), ex);
927 * }
928 * </pre>
929 * <p>
930 * If the specified Runnable command has subcommands, the {@linkplain RunLast last} subcommand specified on the
931 * command line is executed.
932 * Commands with subcommands may be interested in calling the {@link #parseWithHandler(IParseResultHandler, PrintStream, String...) parseWithHandler}
933 * method with a {@link RunAll} handler or a custom handler.
934 * </p><p>
935 * From picocli v2.0, this method prints usage help or version help if {@linkplain #printHelpIfRequested(List, PrintStream, Help.Ansi) requested},
936 * and any exceptions thrown by the {@code Runnable} are caught and rethrown wrapped in an {@code ExecutionException}.
937 * </p>
938 * @param runnable the command to run when {@linkplain #parse(String...) parsing} succeeds.
939 * @param out the printStream to print to
940 * @param ansi whether the usage message should include ANSI escape codes or not
941 * @param args the command line arguments to parse
942 * @param <R> the annotated object must implement Runnable
943 * @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
944 * @throws ExecutionException if the Runnable throws an exception
945 * @see #parseWithHandlers(IParseResultHandler, PrintStream, Help.Ansi, IExceptionHandler, String...)
946 * @see RunLast
947 */
948 public static <R extends Runnable> void run(R runnable, PrintStream out, Help.Ansi ansi, String... args) {
949 CommandLine cmd = new CommandLine(runnable); // validate command outside of try-catch
950 cmd.parseWithHandlers(new RunLast(), out, ansi, new DefaultExceptionHandler(), args);
951 }
952
953 /**
954 * Registers the specified type converter for the specified class. When initializing fields annotated with
955 * {@link Option}, the field's type is used as a lookup key to find the associated type converter, and this
956 * type converter converts the original command line argument string value to the correct type.
957 * <p>
958 * Java 8 lambdas make it easy to register custom type converters:
959 * </p>
960 * <pre>
961 * commandLine.registerConverter(java.nio.file.Path.class, s -> java.nio.file.Paths.get(s));
962 * commandLine.registerConverter(java.time.Duration.class, s -> java.time.Duration.parse(s));</pre>
963 * <p>
964 * Built-in type converters are pre-registered for the following java 1.5 types:
965 * </p>
966 * <ul>
967 * <li>all primitive types</li>
968 * <li>all primitive wrapper types: Boolean, Byte, Character, Double, Float, Integer, Long, Short</li>
969 * <li>any enum</li>
970 * <li>java.io.File</li>
971 * <li>java.math.BigDecimal</li>
972 * <li>java.math.BigInteger</li>
973 * <li>java.net.InetAddress</li>
974 * <li>java.net.URI</li>
975 * <li>java.net.URL</li>
976 * <li>java.nio.charset.Charset</li>
977 * <li>java.sql.Time</li>
978 * <li>java.util.Date</li>
979 * <li>java.util.UUID</li>
980 * <li>java.util.regex.Pattern</li>
981 * <li>StringBuilder</li>
982 * <li>CharSequence</li>
983 * <li>String</li>
984 * </ul>
985 * <p>The specified converter will be registered with this {@code CommandLine} and the full hierarchy of its
986 * subcommands and nested sub-subcommands <em>at the moment the converter is registered</em>. Subcommands added
987 * later will not have this converter added automatically. To ensure a custom type converter is available to all
988 * subcommands, register the type converter last, after adding subcommands.</p>
989 *
990 * @param cls the target class to convert parameter string values to
991 * @param converter the class capable of converting string values to the specified target type
992 * @param <K> the target type
993 * @return this CommandLine object, to allow method chaining
994 * @see #addSubcommand(String, Object)
995 */
996 public <K> CommandLine registerConverter(Class<K> cls, ITypeConverter<K> converter) {
997 interpreter.converterRegistry.put(Assert.notNull(cls, "class"), Assert.notNull(converter, "converter"));
998 for (CommandLine command : interpreter.commands.values()) {
999 command.registerConverter(cls, converter);
1000 }
1001 return this;
1002 }
1003
1004 /** Returns the String that separates option names from option values when parsing command line options. {@value Help#DEFAULT_SEPARATOR} by default.
1005 * @return the String the parser uses to separate option names from option values */
1006 public String getSeparator() {
1007 return interpreter.separator;
1008 }
1009
1010 /** Sets the String the parser uses to separate option names from option values to the specified value.
1011 * The separator may also be set declaratively with the {@link CommandLine.Command#separator()} annotation attribute.
1012 * @param separator the String that separates option names from option values
1013 * @return this {@code CommandLine} object, to allow method chaining */
1014 public CommandLine setSeparator(String separator) {
1015 interpreter.separator = Assert.notNull(separator, "separator");
1016 return this;
1017 }
1018
1019 /** Returns the command name (also called program name) displayed in the usage help synopsis. {@value Help#DEFAULT_COMMAND_NAME} by default.
1020 * @return the command name (also called program name) displayed in the usage */
1021 public String getCommandName() {
1022 return commandName;
1023 }
1024
1025 /** Sets the command name (also called program name) displayed in the usage help synopsis to the specified value.
1026 * Note that this method only modifies the usage help message, it does not impact parsing behaviour.
1027 * The command name may also be set declaratively with the {@link CommandLine.Command#name()} annotation attribute.
1028 * @param commandName command name (also called program name) displayed in the usage help synopsis
1029 * @return this {@code CommandLine} object, to allow method chaining */
1030 public CommandLine setCommandName(String commandName) {
1031 this.commandName = Assert.notNull(commandName, "commandName");
1032 return this;
1033 }
1034 private static boolean empty(String str) { return str == null || str.trim().length() == 0; }
1035 private static boolean empty(Object[] array) { return array == null || array.length == 0; }
1036 private static boolean empty(Text txt) { return txt == null || txt.plain.toString().trim().length() == 0; }
1037 private static String str(String[] arr, int i) { return (arr == null || arr.length == 0) ? "" : arr[i]; }
1038 private static boolean isBoolean(Class<?> type) { return type == Boolean.class || type == Boolean.TYPE; }
1039 private static CommandLine toCommandLine(Object obj) { return obj instanceof CommandLine ? (CommandLine) obj : new CommandLine(obj);}
1040 private static boolean isMultiValue(Field field) { return isMultiValue(field.getType()); }
1041 private static boolean isMultiValue(Class<?> cls) { return cls.isArray() || Collection.class.isAssignableFrom(cls) || Map.class.isAssignableFrom(cls); }
1042 private static Class<?>[] getTypeAttribute(Field field) {
1043 Class<?>[] explicit = field.isAnnotationPresent(Parameters.class) ? field.getAnnotation(Parameters.class).type() : field.getAnnotation(Option.class).type();
1044 if (explicit.length > 0) { return explicit; }
1045 if (field.getType().isArray()) { return new Class<?>[] { field.getType().getComponentType() }; }
1046 if (isMultiValue(field)) {
1047 Type type = field.getGenericType(); // e.g. Map<Long, ? extends Number>
1048 if (type instanceof ParameterizedType) {
1049 ParameterizedType parameterizedType = (ParameterizedType) type;
1050 Type[] paramTypes = parameterizedType.getActualTypeArguments(); // e.g. ? extends Number
1051 Class<?>[] result = new Class<?>[paramTypes.length];
1052 for (int i = 0; i < paramTypes.length; i++) {
1053 if (paramTypes[i] instanceof Class) { result[i] = (Class<?>) paramTypes[i]; continue; } // e.g. Long
1054 if (paramTypes[i] instanceof WildcardType) { // e.g. ? extends Number
1055 WildcardType wildcardType = (WildcardType) paramTypes[i];
1056 Type[] lower = wildcardType.getLowerBounds(); // e.g. []
1057 if (lower.length > 0 && lower[0] instanceof Class) { result[i] = (Class<?>) lower[0]; continue; }
1058 Type[] upper = wildcardType.getUpperBounds(); // e.g. Number
1059 if (upper.length > 0 && upper[0] instanceof Class) { result[i] = (Class<?>) upper[0]; continue; }
1060 }
1061 Arrays.fill(result, String.class); return result; // too convoluted generic type, giving up
1062 }
1063 return result; // we inferred all types from ParameterizedType
1064 }
1065 return new Class<?>[] {String.class, String.class}; // field is multi-value but not ParameterizedType
1066 }
1067 return new Class<?>[] {field.getType()}; // not a multi-value field
1068 }
1069 /**
1070 * <p>
1071 * Annotate fields in your class with {@code @Option} and picocli will initialize these fields when matching
1072 * arguments are specified on the command line.
1073 * </p><p>
1074 * For example:
1075 * </p>
1076 * <pre>import static picocli.CommandLine.*;
1077 *
1078 * public class MyClass {
1079 * @Parameters(type = File.class, description = "Any number of input files")
1080 * private List<File> files = new ArrayList<File>();
1081 *
1082 * @Option(names = { "-o", "--out" }, description = "Output file (default: print to console)")
1083 * private File outputFile;
1084 *
1085 * @Option(names = { "-v", "--verbose"}, description = "Verbosely list files processed")
1086 * private boolean verbose;
1087 *
1088 * @Option(names = { "-h", "--help", "-?", "-help"}, usageHelp = true, description = "Display this help and exit")
1089 * private boolean help;
1090 *
1091 * @Option(names = { "-V", "--version"}, versionHelp = true, description = "Display version information and exit")
1092 * private boolean version;
1093 * }
1094 * </pre>
1095 * <p>
1096 * A field cannot be annotated with both {@code @Parameters} and {@code @Option} or a
1097 * {@code ParameterException} is thrown.
1098 * </p>
1099 */
1100 @Retention(RetentionPolicy.RUNTIME)
1101 @Target(ElementType.FIELD)
1102 public @interface Option {
1103 /**
1104 * One or more option names. At least one option name is required.
1105 * <p>
1106 * Different environments have different conventions for naming options, but usually options have a prefix
1107 * that sets them apart from parameters.
1108 * Picocli supports all of the below styles. The default separator is {@code '='}, but this can be configured.
1109 * </p><p>
1110 * <b>*nix</b>
1111 * </p><p>
1112 * In Unix and Linux, options have a short (single-character) name, a long name or both.
1113 * Short options
1114 * (<a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02">POSIX
1115 * style</a> are single-character and are preceded by the {@code '-'} character, e.g., {@code `-v'}.
1116 * <a href="https://www.gnu.org/software/tar/manual/html_node/Long-Options.html">GNU-style</a> long
1117 * (or <em>mnemonic</em>) options start with two dashes in a row, e.g., {@code `--file'}.
1118 * </p><p>Picocli supports the POSIX convention that short options can be grouped, with the last option
1119 * optionally taking a parameter, which may be attached to the option name or separated by a space or
1120 * a {@code '='} character. The below examples are all equivalent:
1121 * </p><pre>
1122 * -xvfFILE
1123 * -xvf FILE
1124 * -xvf=FILE
1125 * -xv --file FILE
1126 * -xv --file=FILE
1127 * -x -v --file FILE
1128 * -x -v --file=FILE
1129 * </pre><p>
1130 * <b>DOS</b>
1131 * </p><p>
1132 * DOS options mostly have upper case single-character names and start with a single slash {@code '/'} character.
1133 * Option parameters are separated by a {@code ':'} character. Options cannot be grouped together but
1134 * must be specified separately. For example:
1135 * </p><pre>
1136 * DIR /S /A:D /T:C
1137 * </pre><p>
1138 * <b>PowerShell</b>
1139 * </p><p>
1140 * Windows PowerShell options generally are a word preceded by a single {@code '-'} character, e.g., {@code `-Help'}.
1141 * Option parameters are separated by a space or by a {@code ':'} character.
1142 * </p>
1143 * @return one or more option names
1144 */
1145 String[] names();
1146
1147 /**
1148 * Indicates whether this option is required. By default this is false.
1149 * If an option is required, but a user invokes the program without specifying the required option,
1150 * a {@link MissingParameterException} is thrown from the {@link #parse(String...)} method.
1151 * @return whether this option is required
1152 */
1153 boolean required() default false;
1154
1155 /**
1156 * Set {@code help=true} if this option should disable validation of the remaining arguments:
1157 * If the {@code help} option is specified, no error message is generated for missing required options.
1158 * <p>
1159 * This attribute is useful for special options like help ({@code -h} and {@code --help} on unix,
1160 * {@code -?} and {@code -Help} on Windows) or version ({@code -V} and {@code --version} on unix,
1161 * {@code -Version} on Windows).
1162 * </p>
1163 * <p>
1164 * Note that the {@link #parse(String...)} method will not print help documentation. It will only set
1165 * the value of the annotated field. It is the responsibility of the caller to inspect the annotated fields
1166 * and take the appropriate action.
1167 * </p>
1168 * @return whether this option disables validation of the other arguments
1169 * @deprecated Use {@link #usageHelp()} and {@link #versionHelp()} instead. See {@link #printHelpIfRequested(List, PrintStream, CommandLine.Help.Ansi)}
1170 */
1171 boolean help() default false;
1172
1173 /**
1174 * Set {@code usageHelp=true} if this option allows the user to request usage help. If this option is
1175 * specified on the command line, picocli will not validate the remaining arguments (so no "missing required
1176 * option" errors) and the {@link CommandLine#isUsageHelpRequested()} method will return {@code true}.
1177 * <p>
1178 * This attribute is useful for special options like help ({@code -h} and {@code --help} on unix,
1179 * {@code -?} and {@code -Help} on Windows).
1180 * </p>
1181 * <p>
1182 * Note that the {@link #parse(String...)} method will not print usage help documentation. It will only set
1183 * the value of the annotated field. It is the responsibility of the caller to inspect the annotated fields
1184 * and take the appropriate action.
1185 * </p>
1186 * @return whether this option allows the user to request usage help
1187 * @since 0.9.8
1188 */
1189 boolean usageHelp() default false;
1190
1191 /**
1192 * Set {@code versionHelp=true} if this option allows the user to request version information. If this option is
1193 * specified on the command line, picocli will not validate the remaining arguments (so no "missing required
1194 * option" errors) and the {@link CommandLine#isVersionHelpRequested()} method will return {@code true}.
1195 * <p>
1196 * This attribute is useful for special options like version ({@code -V} and {@code --version} on unix,
1197 * {@code -Version} on Windows).
1198 * </p>
1199 * <p>
1200 * Note that the {@link #parse(String...)} method will not print version information. It will only set
1201 * the value of the annotated field. It is the responsibility of the caller to inspect the annotated fields
1202 * and take the appropriate action.
1203 * </p>
1204 * @return whether this option allows the user to request version information
1205 * @since 0.9.8
1206 */
1207 boolean versionHelp() default false;
1208
1209 /**
1210 * Description of this option, used when generating the usage documentation.
1211 * @return the description of this option
1212 */
1213 String[] description() default {};
1214
1215 /**
1216 * Specifies the minimum number of required parameters and the maximum number of accepted parameters.
1217 * If an option declares a positive arity, and the user specifies an insufficient number of parameters on the
1218 * command line, a {@link MissingParameterException} is thrown by the {@link #parse(String...)} method.
1219 * <p>
1220 * In many cases picocli can deduce the number of required parameters from the field's type.
1221 * By default, flags (boolean options) have arity zero,
1222 * and single-valued type fields (String, int, Integer, double, Double, File, Date, etc) have arity one.
1223 * Generally, fields with types that cannot hold multiple values can omit the {@code arity} attribute.
1224 * </p><p>
1225 * Fields used to capture options with arity two or higher should have a type that can hold multiple values,
1226 * like arrays or Collections. See {@link #type()} for strongly-typed Collection fields.
1227 * </p><p>
1228 * For example, if an option has 2 required parameters and any number of optional parameters,
1229 * specify {@code @Option(names = "-example", arity = "2..*")}.
1230 * </p>
1231 * <b>A note on boolean options</b>
1232 * <p>
1233 * By default picocli does not expect boolean options (also called "flags" or "switches") to have a parameter.
1234 * You can make a boolean option take a required parameter by annotating your field with {@code arity="1"}.
1235 * For example: </p>
1236 * <pre>@Option(names = "-v", arity = "1") boolean verbose;</pre>
1237 * <p>
1238 * Because this boolean field is defined with arity 1, the user must specify either {@code <program> -v false}
1239 * or {@code <program> -v true}
1240 * on the command line, or a {@link MissingParameterException} is thrown by the {@link #parse(String...)}
1241 * method.
1242 * </p><p>
1243 * To make the boolean parameter possible but optional, define the field with {@code arity = "0..1"}.
1244 * For example: </p>
1245 * <pre>@Option(names="-v", arity="0..1") boolean verbose;</pre>
1246 * <p>This will accept any of the below without throwing an exception:</p>
1247 * <pre>
1248 * -v
1249 * -v true
1250 * -v false
1251 * </pre>
1252 * @return how many arguments this option requires
1253 */
1254 String arity() default "";
1255
1256 /**
1257 * Specify a {@code paramLabel} for the option parameter to be used in the usage help message. If omitted,
1258 * picocli uses the field name in fish brackets ({@code '<'} and {@code '>'}) by default. Example:
1259 * <pre>class Example {
1260 * @Option(names = {"-o", "--output"}, paramLabel="FILE", description="path of the output file")
1261 * private File out;
1262 * @Option(names = {"-j", "--jobs"}, arity="0..1", description="Allow N jobs at once; infinite jobs with no arg.")
1263 * private int maxJobs = -1;
1264 * }</pre>
1265 * <p>By default, the above gives a usage help message like the following:</p><pre>
1266 * Usage: <main class> [OPTIONS]
1267 * -o, --output FILE path of the output file
1268 * -j, --jobs [<maxJobs>] Allow N jobs at once; infinite jobs with no arg.
1269 * </pre>
1270 * @return name of the option parameter used in the usage help message
1271 */
1272 String paramLabel() default "";
1273
1274 /** <p>
1275 * Optionally specify a {@code type} to control exactly what Class the option parameter should be converted
1276 * to. This may be useful when the field type is an interface or an abstract class. For example, a field can
1277 * be declared to have type {@code java.lang.Number}, and annotating {@code @Option(type=Short.class)}
1278 * ensures that the option parameter value is converted to a {@code Short} before setting the field value.
1279 * </p><p>
1280 * For array fields whose <em>component</em> type is an interface or abstract class, specify the concrete <em>component</em> type.
1281 * For example, a field with type {@code Number[]} may be annotated with {@code @Option(type=Short.class)}
1282 * to ensure that option parameter values are converted to {@code Short} before adding an element to the array.
1283 * </p><p>
1284 * Picocli will use the {@link ITypeConverter} that is
1285 * {@linkplain #registerConverter(Class, ITypeConverter) registered} for the specified type to convert
1286 * the raw String values before modifying the field value.
1287 * </p><p>
1288 * Prior to 2.0, the {@code type} attribute was necessary for {@code Collection} and {@code Map} fields,
1289 * but starting from 2.0 picocli will infer the component type from the generic type's type arguments.
1290 * For example, for a field of type {@code Map<TimeUnit, Long>} picocli will know the option parameter
1291 * should be split up in key=value pairs, where the key should be converted to a {@code java.util.concurrent.TimeUnit}
1292 * enum value, and the value should be converted to a {@code Long}. No {@code @Option(type=...)} type attribute
1293 * is required for this. For generic types with wildcards, picocli will take the specified upper or lower bound
1294 * as the Class to convert to, unless the {@code @Option} annotation specifies an explicit {@code type} attribute.
1295 * </p><p>
1296 * If the field type is a raw collection or a raw map, and you want it to contain other values than Strings,
1297 * or if the generic type's type arguments are interfaces or abstract classes, you may
1298 * specify a {@code type} attribute to control the Class that the option parameter should be converted to.
1299 * @return the type(s) to convert the raw String values
1300 */
1301 Class<?>[] type() default {};
1302
1303 /**
1304 * Specify a regular expression to use to split option parameter values before applying them to the field.
1305 * All elements resulting from the split are added to the array or Collection. Ignored for single-value fields.
1306 * @return a regular expression to split option parameter values or {@code ""} if the value should not be split
1307 * @see String#split(String)
1308 */
1309 String split() default "";
1310
1311 /**
1312 * Set {@code hidden=true} if this option should not be included in the usage documentation.
1313 * @return whether this option should be excluded from the usage message
1314 */
1315 boolean hidden() default false;
1316 }
1317 /**
1318 * <p>
1319 * Fields annotated with {@code @Parameters} will be initialized with positional parameters. By specifying the
1320 * {@link #index()} attribute you can pick which (or what range) of the positional parameters to apply. If no index
1321 * is specified, the field will get all positional parameters (so it should be an array or a collection).
1322 * </p><p>
1323 * When parsing the command line arguments, picocli first tries to match arguments to {@link Option Options}.
1324 * Positional parameters are the arguments that follow the options, or the arguments that follow a "--" (double
1325 * dash) argument on the command line.
1326 * </p><p>
1327 * For example:
1328 * </p>
1329 * <pre>import static picocli.CommandLine.*;
1330 *
1331 * public class MyCalcParameters {
1332 * @Parameters(type = BigDecimal.class, description = "Any number of input numbers")
1333 * private List<BigDecimal> files = new ArrayList<BigDecimal>();
1334 *
1335 * @Option(names = { "-h", "--help", "-?", "-help"}, help = true, description = "Display this help and exit")
1336 * private boolean help;
1337 * }
1338 * </pre><p>
1339 * A field cannot be annotated with both {@code @Parameters} and {@code @Option} or a {@code ParameterException}
1340 * is thrown.</p>
1341 */
1342 @Retention(RetentionPolicy.RUNTIME)
1343 @Target(ElementType.FIELD)
1344 public @interface Parameters {
1345 /** Specify an index ("0", or "1", etc.) to pick which of the command line arguments should be assigned to this
1346 * field. For array or Collection fields, you can also specify an index range ("0..3", or "2..*", etc.) to assign
1347 * a subset of the command line arguments to this field. The default is "*", meaning all command line arguments.
1348 * @return an index or range specifying which of the command line arguments should be assigned to this field
1349 */
1350 String index() default "*";
1351
1352 /** Description of the parameter(s), used when generating the usage documentation.
1353 * @return the description of the parameter(s)
1354 */
1355 String[] description() default {};
1356
1357 /**
1358 * Specifies the minimum number of required parameters and the maximum number of accepted parameters. If a
1359 * positive arity is declared, and the user specifies an insufficient number of parameters on the command line,
1360 * {@link MissingParameterException} is thrown by the {@link #parse(String...)} method.
1361 * <p>The default depends on the type of the parameter: booleans require no parameters, arrays and Collections
1362 * accept zero to any number of parameters, and any other type accepts one parameter.</p>
1363 * @return the range of minimum and maximum parameters accepted by this command
1364 */
1365 String arity() default "";
1366
1367 /**
1368 * Specify a {@code paramLabel} for the parameter to be used in the usage help message. If omitted,
1369 * picocli uses the field name in fish brackets ({@code '<'} and {@code '>'}) by default. Example:
1370 * <pre>class Example {
1371 * @Parameters(paramLabel="FILE", description="path of the input FILE(s)")
1372 * private File[] inputFiles;
1373 * }</pre>
1374 * <p>By default, the above gives a usage help message like the following:</p><pre>
1375 * Usage: <main class> [FILE...]
1376 * [FILE...] path of the input FILE(s)
1377 * </pre>
1378 * @return name of the positional parameter used in the usage help message
1379 */
1380 String paramLabel() default "";
1381
1382 /**
1383 * <p>
1384 * Optionally specify a {@code type} to control exactly what Class the positional parameter should be converted
1385 * to. This may be useful when the field type is an interface or an abstract class. For example, a field can
1386 * be declared to have type {@code java.lang.Number}, and annotating {@code @Parameters(type=Short.class)}
1387 * ensures that the positional parameter value is converted to a {@code Short} before setting the field value.
1388 * </p><p>
1389 * For array fields whose <em>component</em> type is an interface or abstract class, specify the concrete <em>component</em> type.
1390 * For example, a field with type {@code Number[]} may be annotated with {@code @Parameters(type=Short.class)}
1391 * to ensure that positional parameter values are converted to {@code Short} before adding an element to the array.
1392 * </p><p>
1393 * Picocli will use the {@link ITypeConverter} that is
1394 * {@linkplain #registerConverter(Class, ITypeConverter) registered} for the specified type to convert
1395 * the raw String values before modifying the field value.
1396 * </p><p>
1397 * Prior to 2.0, the {@code type} attribute was necessary for {@code Collection} and {@code Map} fields,
1398 * but starting from 2.0 picocli will infer the component type from the generic type's type arguments.
1399 * For example, for a field of type {@code Map<TimeUnit, Long>} picocli will know the positional parameter
1400 * should be split up in key=value pairs, where the key should be converted to a {@code java.util.concurrent.TimeUnit}
1401 * enum value, and the value should be converted to a {@code Long}. No {@code @Parameters(type=...)} type attribute
1402 * is required for this. For generic types with wildcards, picocli will take the specified upper or lower bound
1403 * as the Class to convert to, unless the {@code @Parameters} annotation specifies an explicit {@code type} attribute.
1404 * </p><p>
1405 * If the field type is a raw collection or a raw map, and you want it to contain other values than Strings,
1406 * or if the generic type's type arguments are interfaces or abstract classes, you may
1407 * specify a {@code type} attribute to control the Class that the positional parameter should be converted to.
1408 * @return the type(s) to convert the raw String values
1409 */
1410 Class<?>[] type() default {};
1411
1412 /**
1413 * Specify a regular expression to use to split positional parameter values before applying them to the field.
1414 * All elements resulting from the split are added to the array or Collection. Ignored for single-value fields.
1415 * @return a regular expression to split operand values or {@code ""} if the value should not be split
1416 * @see String#split(String)
1417 */
1418 String split() default "";
1419
1420 /**
1421 * Set {@code hidden=true} if this parameter should not be included in the usage message.
1422 * @return whether this parameter should be excluded from the usage message
1423 */
1424 boolean hidden() default false;
1425 }
1426
1427 /**
1428 * <p>Annotate your class with {@code @Command} when you want more control over the format of the generated help
1429 * message.
1430 * </p><pre>
1431 * @Command(name = "Encrypt",
1432 * description = "Encrypt FILE(s), or standard input, to standard output or to the output file.",
1433 * footer = "Copyright (c) 2017")
1434 * public class Encrypt {
1435 * @Parameters(paramLabel = "FILE", type = File.class, description = "Any number of input files")
1436 * private List<File> files = new ArrayList<File>();
1437 *
1438 * @Option(names = { "-o", "--out" }, description = "Output file (default: print to console)")
1439 * private File outputFile;
1440 * }</pre>
1441 * <p>
1442 * The structure of a help message looks like this:
1443 * </p><ul>
1444 * <li>[header]</li>
1445 * <li>[synopsis]: {@code Usage: <commandName> [OPTIONS] [FILE...]}</li>
1446 * <li>[description]</li>
1447 * <li>[parameter list]: {@code [FILE...] Any number of input files}</li>
1448 * <li>[option list]: {@code -h, --help prints this help message and exits}</li>
1449 * <li>[footer]</li>
1450 * </ul> */
1451 @Retention(RetentionPolicy.RUNTIME)
1452 @Target({ElementType.TYPE, ElementType.LOCAL_VARIABLE, ElementType.PACKAGE})
1453 public @interface Command {
1454 /** Program name to show in the synopsis. If omitted, {@code "<main class>"} is used.
1455 * For {@linkplain #subcommands() declaratively added} subcommands, this attribute is also used
1456 * by the parser to recognize subcommands in the command line arguments.
1457 * @return the program name to show in the synopsis
1458 * @see Help#commandName */
1459 String name() default "<main class>";
1460
1461 /** A list of classes to instantiate and register as subcommands. When registering subcommands declaratively
1462 * like this, you don't need to call the {@link CommandLine#addSubcommand(String, Object)} method. For example, this:
1463 * <pre>
1464 * @Command(subcommands = {
1465 * GitStatus.class,
1466 * GitCommit.class,
1467 * GitBranch.class })
1468 * public class Git { ... }
1469 *
1470 * CommandLine commandLine = new CommandLine(new Git());
1471 * </pre> is equivalent to this:
1472 * <pre>
1473 * // alternative: programmatically add subcommands.
1474 * // NOTE: in this case there should be no `subcommands` attribute on the @Command annotation.
1475 * @Command public class Git { ... }
1476 *
1477 * CommandLine commandLine = new CommandLine(new Git())
1478 * .addSubcommand("status", new GitStatus())
1479 * .addSubcommand("commit", new GitCommit())
1480 * .addSubcommand("branch", new GitBranch());
1481 * </pre>
1482 * @return the declaratively registered subcommands of this command, or an empty array if none
1483 * @see CommandLine#addSubcommand(String, Object)
1484 * @since 0.9.8
1485 */
1486 Class<?>[] subcommands() default {};
1487
1488 /** String that separates options from option parameters. Default is {@code "="}. Spaces are also accepted.
1489 * @return the string that separates options from option parameters, used both when parsing and when generating usage help
1490 * @see Help#separator
1491 * @see CommandLine#setSeparator(String) */
1492 String separator() default "=";
1493
1494 /** Version information for this command, to print to the console when the user specifies an
1495 * {@linkplain Option#versionHelp() option} to request version help. This is not part of the usage help message.
1496 *
1497 * @return a string or an array of strings with version information about this command.
1498 * @since 0.9.8
1499 * @see CommandLine#printVersionHelp(PrintStream)
1500 */
1501 String[] version() default {};
1502
1503 /** Set the heading preceding the header section. May contain embedded {@linkplain java.util.Formatter format specifiers}.
1504 * @return the heading preceding the header section
1505 * @see Help#headerHeading(Object...) */
1506 String headerHeading() default "";
1507
1508 /** Optional summary description of the command, shown before the synopsis.
1509 * @return summary description of the command
1510 * @see Help#header
1511 * @see Help#header(Object...) */
1512 String[] header() default {};
1513
1514 /** Set the heading preceding the synopsis text. May contain embedded
1515 * {@linkplain java.util.Formatter format specifiers}. The default heading is {@code "Usage: "} (without a line
1516 * break between the heading and the synopsis text).
1517 * @return the heading preceding the synopsis text
1518 * @see Help#synopsisHeading(Object...) */
1519 String synopsisHeading() default "Usage: ";
1520
1521 /** Specify {@code true} to generate an abbreviated synopsis like {@code "<main> [OPTIONS] [PARAMETERS...]"}.
1522 * By default, a detailed synopsis with individual option names and parameters is generated.
1523 * @return whether the synopsis should be abbreviated
1524 * @see Help#abbreviateSynopsis
1525 * @see Help#abbreviatedSynopsis()
1526 * @see Help#detailedSynopsis(Comparator, boolean) */
1527 boolean abbreviateSynopsis() default false;
1528
1529 /** Specify one or more custom synopsis lines to display instead of an auto-generated synopsis.
1530 * @return custom synopsis text to replace the auto-generated synopsis
1531 * @see Help#customSynopsis
1532 * @see Help#customSynopsis(Object...) */
1533 String[] customSynopsis() default {};
1534
1535 /** Set the heading preceding the description section. May contain embedded {@linkplain java.util.Formatter format specifiers}.
1536 * @return the heading preceding the description section
1537 * @see Help#descriptionHeading(Object...) */
1538 String descriptionHeading() default "";
1539
1540 /** Optional text to display between the synopsis line(s) and the list of options.
1541 * @return description of this command
1542 * @see Help#description
1543 * @see Help#description(Object...) */
1544 String[] description() default {};
1545
1546 /** Set the heading preceding the parameters list. May contain embedded {@linkplain java.util.Formatter format specifiers}.
1547 * @return the heading preceding the parameters list
1548 * @see Help#parameterListHeading(Object...) */
1549 String parameterListHeading() default "";
1550
1551 /** Set the heading preceding the options list. May contain embedded {@linkplain java.util.Formatter format specifiers}.
1552 * @return the heading preceding the options list
1553 * @see Help#optionListHeading(Object...) */
1554 String optionListHeading() default "";
1555
1556 /** Specify {@code false} to show Options in declaration order. The default is to sort alphabetically.
1557 * @return whether options should be shown in alphabetic order.
1558 * @see Help#sortOptions */
1559 boolean sortOptions() default true;
1560
1561 /** Prefix required options with this character in the options list. The default is no marker: the synopsis
1562 * indicates which options and parameters are required.
1563 * @return the character to show in the options list to mark required options
1564 * @see Help#requiredOptionMarker */
1565 char requiredOptionMarker() default ' ';
1566
1567 /** Specify {@code true} to show default values in the description column of the options list (except for
1568 * boolean options). False by default.
1569 * @return whether the default values for options and parameters should be shown in the description column
1570 * @see Help#showDefaultValues */
1571 boolean showDefaultValues() default false;
1572
1573 /** Set the heading preceding the subcommands list. May contain embedded {@linkplain java.util.Formatter format specifiers}.
1574 * The default heading is {@code "Commands:%n"} (with a line break at the end).
1575 * @return the heading preceding the subcommands list
1576 * @see Help#commandListHeading(Object...) */
1577 String commandListHeading() default "Commands:%n";
1578
1579 /** Set the heading preceding the footer section. May contain embedded {@linkplain java.util.Formatter format specifiers}.
1580 * @return the heading preceding the footer section
1581 * @see Help#footerHeading(Object...) */
1582 String footerHeading() default "";
1583
1584 /** Optional text to display after the list of options.
1585 * @return text to display after the list of options
1586 * @see Help#footer
1587 * @see Help#footer(Object...) */
1588 String[] footer() default {};
1589 }
1590 /**
1591 * <p>
1592 * When parsing command line arguments and initializing
1593 * fields annotated with {@link Option @Option} or {@link Parameters @Parameters},
1594 * String values can be converted to any type for which a {@code ITypeConverter} is registered.
1595 * </p><p>
1596 * This interface defines the contract for classes that know how to convert a String into some domain object.
1597 * Custom converters can be registered with the {@link #registerConverter(Class, ITypeConverter)} method.
1598 * </p><p>
1599 * Java 8 lambdas make it easy to register custom type converters:
1600 * </p>
1601 * <pre>
1602 * commandLine.registerConverter(java.nio.file.Path.class, s -> java.nio.file.Paths.get(s));
1603 * commandLine.registerConverter(java.time.Duration.class, s -> java.time.Duration.parse(s));</pre>
1604 * <p>
1605 * Built-in type converters are pre-registered for the following java 1.5 types:
1606 * </p>
1607 * <ul>
1608 * <li>all primitive types</li>
1609 * <li>all primitive wrapper types: Boolean, Byte, Character, Double, Float, Integer, Long, Short</li>
1610 * <li>any enum</li>
1611 * <li>java.io.File</li>
1612 * <li>java.math.BigDecimal</li>
1613 * <li>java.math.BigInteger</li>
1614 * <li>java.net.InetAddress</li>
1615 * <li>java.net.URI</li>
1616 * <li>java.net.URL</li>
1617 * <li>java.nio.charset.Charset</li>
1618 * <li>java.sql.Time</li>
1619 * <li>java.util.Date</li>
1620 * <li>java.util.UUID</li>
1621 * <li>java.util.regex.Pattern</li>
1622 * <li>StringBuilder</li>
1623 * <li>CharSequence</li>
1624 * <li>String</li>
1625 * </ul>
1626 * @param <K> the type of the object that is the result of the conversion
1627 */
1628 public interface ITypeConverter<K> {
1629 /**
1630 * Converts the specified command line argument value to some domain object.
1631 * @param value the command line argument String value
1632 * @return the resulting domain object
1633 * @throws Exception an exception detailing what went wrong during the conversion
1634 */
1635 K convert(String value) throws Exception;
1636 }
1637 /** Describes the number of parameters required and accepted by an option or a positional parameter.
1638 * @since 0.9.7
1639 */
1640 public static class Range implements Comparable<Range> {
1641 /** Required number of parameters for an option or positional parameter. */
1642 public final int min;
1643 /** Maximum accepted number of parameters for an option or positional parameter. */
1644 public final int max;
1645 public final boolean isVariable;
1646 private final boolean isUnspecified;
1647 private final String originalValue;
1648
1649 /** Constructs a new Range object with the specified parameters.
1650 * @param min minimum number of required parameters
1651 * @param max maximum number of allowed parameters (or Integer.MAX_VALUE if variable)
1652 * @param variable {@code true} if any number or parameters is allowed, {@code false} otherwise
1653 * @param unspecified {@code true} if no arity was specified on the option/parameter (value is based on type)
1654 * @param originalValue the original value that was specified on the option or parameter
1655 */
1656 public Range(int min, int max, boolean variable, boolean unspecified, String originalValue) {
1657 this.min = min;
1658 this.max = max;
1659 this.isVariable = variable;
1660 this.isUnspecified = unspecified;
1661 this.originalValue = originalValue;
1662 }
1663 /** Returns a new {@code Range} based on the {@link Option#arity()} annotation on the specified field,
1664 * or the field type's default arity if no arity was specified.
1665 * @param field the field whose Option annotation to inspect
1666 * @return a new {@code Range} based on the Option arity annotation on the specified field */
1667 public static Range optionArity(Field field) {
1668 return field.isAnnotationPresent(Option.class)
1669 ? adjustForType(Range.valueOf(field.getAnnotation(Option.class).arity()), field)
1670 : new Range(0, 0, false, true, "0");
1671 }
1672 /** Returns a new {@code Range} based on the {@link Parameters#arity()} annotation on the specified field,
1673 * or the field type's default arity if no arity was specified.
1674 * @param field the field whose Parameters annotation to inspect
1675 * @return a new {@code Range} based on the Parameters arity annotation on the specified field */
1676 public static Range parameterArity(Field field) {
1677 return field.isAnnotationPresent(Parameters.class)
1678 ? adjustForType(Range.valueOf(field.getAnnotation(Parameters.class).arity()), field)
1679 : new Range(0, 0, false, true, "0");
1680 }
1681 /** Returns a new {@code Range} based on the {@link Parameters#index()} annotation on the specified field.
1682 * @param field the field whose Parameters annotation to inspect
1683 * @return a new {@code Range} based on the Parameters index annotation on the specified field */
1684 public static Range parameterIndex(Field field) {
1685 return field.isAnnotationPresent(Parameters.class)
1686 ? Range.valueOf(field.getAnnotation(Parameters.class).index())
1687 : new Range(0, 0, false, true, "0");
1688 }
1689 static Range adjustForType(Range result, Field field) {
1690 return result.isUnspecified ? defaultArity(field) : result;
1691 }
1692 /** Returns the default arity {@code Range}: for {@link Option options} this is 0 for booleans and 1 for
1693 * other types, for {@link Parameters parameters} booleans have arity 0, arrays or Collections have
1694 * arity "0..*", and other types have arity 1.
1695 * @param field the field whose default arity to return
1696 * @return a new {@code Range} indicating the default arity of the specified field
1697 * @since 2.0 */
1698 public static Range defaultArity(Field field) {
1699 Class<?> type = field.getType();
1700 if (field.isAnnotationPresent(Option.class)) {
1701 return defaultArity(type);
1702 }
1703 if (isMultiValue(type)) {
1704 return Range.valueOf("0..1");
1705 }
1706 return Range.valueOf("1");// for single-valued fields (incl. boolean positional parameters)
1707 }
1708 /** Returns the default arity {@code Range} for {@link Option options}: booleans have arity 0, other types have arity 1.
1709 * @param type the type whose default arity to return
1710 * @return a new {@code Range} indicating the default arity of the specified type */
1711 public static Range defaultArity(Class<?> type) {
1712 return isBoolean(type) ? Range.valueOf("0") : Range.valueOf("1");
1713 }
1714 private int size() { return 1 + max - min; }
1715 static Range parameterCapacity(Field field) {
1716 Range arity = parameterArity(field);
1717 if (!isMultiValue(field)) { return arity; }
1718 Range index = parameterIndex(field);
1719 if (arity.max == 0) { return arity; }
1720 if (index.size() == 1) { return arity; }
1721 if (index.isVariable) { return Range.valueOf(arity.min + "..*"); }
1722 if (arity.size() == 1) { return Range.valueOf(arity.min * index.size() + ""); }
1723 if (arity.isVariable) { return Range.valueOf(arity.min * index.size() + "..*"); }
1724 return Range.valueOf(arity.min * index.size() + ".." + arity.max * index.size());
1725 }
1726 /** Leniently parses the specified String as an {@code Range} value and return the result. A range string can
1727 * be a fixed integer value or a range of the form {@code MIN_VALUE + ".." + MAX_VALUE}. If the
1728 * {@code MIN_VALUE} string is not numeric, the minimum is zero. If the {@code MAX_VALUE} is not numeric, the
1729 * range is taken to be variable and the maximum is {@code Integer.MAX_VALUE}.
1730 * @param range the value range string to parse
1731 * @return a new {@code Range} value */
1732 public static Range valueOf(String range) {
1733 range = range.trim();
1734 boolean unspecified = range.length() == 0 || range.startsWith(".."); // || range.endsWith("..");
1735 int min = -1, max = -1;
1736 boolean variable = false;
1737 int dots = -1;
1738 if ((dots = range.indexOf("..")) >= 0) {
1739 min = parseInt(range.substring(0, dots), 0);
1740 max = parseInt(range.substring(dots + 2), Integer.MAX_VALUE);
1741 variable = max == Integer.MAX_VALUE;
1742 } else {
1743 max = parseInt(range, Integer.MAX_VALUE);
1744 variable = max == Integer.MAX_VALUE;
1745 min = variable ? 0 : max;
1746 }
1747 Range result = new Range(min, max, variable, unspecified, range);
1748 return result;
1749 }
1750 private static int parseInt(String str, int defaultValue) {
1751 try {
1752 return Integer.parseInt(str);
1753 } catch (Exception ex) {
1754 return defaultValue;
1755 }
1756 }
1757 /** Returns a new Range object with the {@code min} value replaced by the specified value.
1758 * The {@code max} of the returned Range is guaranteed not to be less than the new {@code min} value.
1759 * @param newMin the {@code min} value of the returned Range object
1760 * @return a new Range object with the specified {@code min} value */
1761 public Range min(int newMin) { return new Range(newMin, Math.max(newMin, max), isVariable, isUnspecified, originalValue); }
1762
1763 /** Returns a new Range object with the {@code max} value replaced by the specified value.
1764 * The {@code min} of the returned Range is guaranteed not to be greater than the new {@code max} value.
1765 * @param newMax the {@code max} value of the returned Range object
1766 * @return a new Range object with the specified {@code max} value */
1767 public Range max(int newMax) { return new Range(Math.min(min, newMax), newMax, isVariable, isUnspecified, originalValue); }
1768
1769 /**
1770 * Returns {@code true} if this Range includes the specified value, {@code false} otherwise.
1771 * @param value the value to check
1772 * @return {@code true} if the specified value is not less than the minimum and not greater than the maximum of this Range
1773 */
1774 public boolean contains(int value) { return min <= value && max >= value; }
1775
1776 public boolean equals(Object object) {
1777 if (!(object instanceof Range)) { return false; }
1778 Range other = (Range) object;
1779 return other.max == this.max && other.min == this.min && other.isVariable == this.isVariable;
1780 }
1781 public int hashCode() {
1782 return ((17 * 37 + max) * 37 + min) * 37 + (isVariable ? 1 : 0);
1783 }
1784 public String toString() {
1785 return min == max ? String.valueOf(min) : min + ".." + (isVariable ? "*" : max);
1786 }
1787 public int compareTo(Range other) {
1788 int result = min - other.min;
1789 return (result == 0) ? max - other.max : result;
1790 }
1791 }
1792 static void init(Class<?> cls,
1793 List<Field> requiredFields,
1794 Map<String, Field> optionName2Field,
1795 Map<Character, Field> singleCharOption2Field,
1796 List<Field> positionalParametersFields) {
1797 Field[] declaredFields = cls.getDeclaredFields();
1798 for (Field field : declaredFields) {
1799 field.setAccessible(true);
1800 if (field.isAnnotationPresent(Option.class)) {
1801 Option option = field.getAnnotation(Option.class);
1802 if (option.required()) {
1803 requiredFields.add(field);
1804 }
1805 for (String name : option.names()) { // cannot be null or empty
1806 Field existing = optionName2Field.put(name, field);
1807 if (existing != null && existing != field) {
1808 throw DuplicateOptionAnnotationsException.create(name, field, existing);
1809 }
1810 if (name.length() == 2 && name.startsWith("-")) {
1811 char flag = name.charAt(1);
1812 Field existing2 = singleCharOption2Field.put(flag, field);
1813 if (existing2 != null && existing2 != field) {
1814 throw DuplicateOptionAnnotationsException.create(name, field, existing2);
1815 }
1816 }
1817 }
1818 }
1819 if (field.isAnnotationPresent(Parameters.class)) {
1820 if (field.isAnnotationPresent(Option.class)) {
1821 throw new DuplicateOptionAnnotationsException("A field can be either @Option or @Parameters, but '"
1822 + field.getName() + "' is both.");
1823 }
1824 positionalParametersFields.add(field);
1825 Range arity = Range.parameterArity(field);
1826 if (arity.min > 0) {
1827 requiredFields.add(field);
1828 }
1829 }
1830 }
1831 }
1832 static void validatePositionalParameters(List<Field> positionalParametersFields) {
1833 int min = 0;
1834 for (Field field : positionalParametersFields) {
1835 Range index = Range.parameterIndex(field);
1836 if (index.min > min) {
1837 throw new ParameterIndexGapException("Missing field annotated with @Parameter(index=" + min +
1838 "). Nearest field '" + field.getName() + "' has index=" + index.min);
1839 }
1840 min = Math.max(min, index.max);
1841 min = min == Integer.MAX_VALUE ? min : min + 1;
1842 }
1843 }
1844 private static <T> Stack<T> reverse(Stack<T> stack) {
1845 Collections.reverse(stack);
1846 return stack;
1847 }
1848 /**
1849 * Helper class responsible for processing command line arguments.
1850 */
1851 private class Interpreter {
1852 private final Map<String, CommandLine> commands = new LinkedHashMap<String, CommandLine>();
1853 private final Map<Class<?>, ITypeConverter<?>> converterRegistry = new HashMap<Class<?>, ITypeConverter<?>>();
1854 private final Map<String, Field> optionName2Field = new HashMap<String, Field>();
1855 private final Map<Character, Field> singleCharOption2Field = new HashMap<Character, Field>();
1856 private final List<Field> requiredFields = new ArrayList<Field>();
1857 private final List<Field> positionalParametersFields = new ArrayList<Field>();
1858 private final Object command;
1859 private boolean isHelpRequested;
1860 private String separator = Help.DEFAULT_SEPARATOR;
1861 private int position;
1862
1863 Interpreter(Object command) {
1864 converterRegistry.put(Path.class, new BuiltIn.PathConverter());
1865 converterRegistry.put(Object.class, new BuiltIn.StringConverter());
1866 converterRegistry.put(String.class, new BuiltIn.StringConverter());
1867 converterRegistry.put(StringBuilder.class, new BuiltIn.StringBuilderConverter());
1868 converterRegistry.put(CharSequence.class, new BuiltIn.CharSequenceConverter());
1869 converterRegistry.put(Byte.class, new BuiltIn.ByteConverter());
1870 converterRegistry.put(Byte.TYPE, new BuiltIn.ByteConverter());
1871 converterRegistry.put(Boolean.class, new BuiltIn.BooleanConverter());
1872 converterRegistry.put(Boolean.TYPE, new BuiltIn.BooleanConverter());
1873 converterRegistry.put(Character.class, new BuiltIn.CharacterConverter());
1874 converterRegistry.put(Character.TYPE, new BuiltIn.CharacterConverter());
1875 converterRegistry.put(Short.class, new BuiltIn.ShortConverter());
1876 converterRegistry.put(Short.TYPE, new BuiltIn.ShortConverter());
1877 converterRegistry.put(Integer.class, new BuiltIn.IntegerConverter());
1878 converterRegistry.put(Integer.TYPE, new BuiltIn.IntegerConverter());
1879 converterRegistry.put(Long.class, new BuiltIn.LongConverter());
1880 converterRegistry.put(Long.TYPE, new BuiltIn.LongConverter());
1881 converterRegistry.put(Float.class, new BuiltIn.FloatConverter());
1882 converterRegistry.put(Float.TYPE, new BuiltIn.FloatConverter());
1883 converterRegistry.put(Double.class, new BuiltIn.DoubleConverter());
1884 converterRegistry.put(Double.TYPE, new BuiltIn.DoubleConverter());
1885 converterRegistry.put(File.class, new BuiltIn.FileConverter());
1886 converterRegistry.put(URI.class, new BuiltIn.URIConverter());
1887 converterRegistry.put(URL.class, new BuiltIn.URLConverter());
1888 converterRegistry.put(Date.class, new BuiltIn.ISO8601DateConverter());
1889 converterRegistry.put(Time.class, new BuiltIn.ISO8601TimeConverter());
1890 converterRegistry.put(BigDecimal.class, new BuiltIn.BigDecimalConverter());
1891 converterRegistry.put(BigInteger.class, new BuiltIn.BigIntegerConverter());
1892 converterRegistry.put(Charset.class, new BuiltIn.CharsetConverter());
1893 converterRegistry.put(InetAddress.class, new BuiltIn.InetAddressConverter());
1894 converterRegistry.put(Pattern.class, new BuiltIn.PatternConverter());
1895 converterRegistry.put(UUID.class, new BuiltIn.UUIDConverter());
1896
1897 this.command = Assert.notNull(command, "command");
1898 Class<?> cls = command.getClass();
1899 String declaredName = null;
1900 String declaredSeparator = null;
1901 boolean hasCommandAnnotation = false;
1902 while (cls != null) {
1903 init(cls, requiredFields, optionName2Field, singleCharOption2Field, positionalParametersFields);
1904 if (cls.isAnnotationPresent(Command.class)) {
1905 hasCommandAnnotation = true;
1906 Command cmd = cls.getAnnotation(Command.class);
1907 declaredSeparator = (declaredSeparator == null) ? cmd.separator() : declaredSeparator;
1908 declaredName = (declaredName == null) ? cmd.name() : declaredName;
1909 CommandLine.this.versionLines.addAll(Arrays.asList(cmd.version()));
1910
1911 for (Class<?> sub : cmd.subcommands()) {
1912 Command subCommand = sub.getAnnotation(Command.class);
1913 if (subCommand == null || Help.DEFAULT_COMMAND_NAME.equals(subCommand.name())) {
1914 throw new InitializationException("Subcommand " + sub.getName() +
1915 " is missing the mandatory @Command annotation with a 'name' attribute");
1916 }
1917 try {
1918 Constructor<?> constructor = sub.getDeclaredConstructor();
1919 constructor.setAccessible(true);
1920 CommandLine commandLine = toCommandLine(constructor.newInstance());
1921 commandLine.parent = CommandLine.this;
1922 commands.put(subCommand.name(), commandLine);
1923 }
1924 catch (InitializationException ex) { throw ex; }
1925 catch (NoSuchMethodException ex) { throw new InitializationException("Cannot instantiate subcommand " +
1926 sub.getName() + ": the class has no constructor", ex); }
1927 catch (Exception ex) {
1928 throw new InitializationException("Could not instantiate and add subcommand " +
1929 sub.getName() + ": " + ex, ex);
1930 }
1931 }
1932 }
1933 cls = cls.getSuperclass();
1934 }
1935 separator = declaredSeparator != null ? declaredSeparator : separator;
1936 CommandLine.this.commandName = declaredName != null ? declaredName : CommandLine.this.commandName;
1937 Collections.sort(positionalParametersFields, new PositionalParametersSorter());
1938 validatePositionalParameters(positionalParametersFields);
1939
1940 if (positionalParametersFields.isEmpty() && optionName2Field.isEmpty() && !hasCommandAnnotation) {
1941 throw new InitializationException(command + " (" + command.getClass() +
1942 ") is not a command: it has no @Command, @Option or @Parameters annotations");
1943 }
1944 }
1945
1946 /**
1947 * Entry point into parsing command line arguments.
1948 * @param args the command line arguments
1949 * @return a list with all commands and subcommands initialized by this method
1950 * @throws ParameterException if the specified command line arguments are invalid
1951 */
1952 List<CommandLine> parse(String... args) {
1953 Assert.notNull(args, "argument array");
1954 if (tracer.isInfo()) {tracer.info("Parsing %d command line args %s%n", args.length, Arrays.toString(args));}
1955 Stack<String> arguments = new Stack<String>();
1956 for (int i = args.length - 1; i >= 0; i--) {
1957 arguments.push(args[i]);
1958 }
1959 List<CommandLine> result = new ArrayList<CommandLine>();
1960 parse(result, arguments, args);
1961 return result;
1962 }
1963
1964 private void parse(List<CommandLine> parsedCommands, Stack<String> argumentStack, String[] originalArgs) {
1965 // first reset any state in case this CommandLine instance is being reused
1966 isHelpRequested = false;
1967 CommandLine.this.versionHelpRequested = false;
1968 CommandLine.this.usageHelpRequested = false;
1969
1970 Class<?> cmdClass = this.command.getClass();
1971 if (tracer.isDebug()) {tracer.debug("Initializing %s: %d options, %d positional parameters, %d required, %d subcommands.%n", cmdClass.getName(), new HashSet<Field>(optionName2Field.values()).size(), positionalParametersFields.size(), requiredFields.size(), commands.size());}
1972 parsedCommands.add(CommandLine.this);
1973 List<Field> required = new ArrayList<Field>(requiredFields);
1974 Set<Field> initialized = new HashSet<Field>();
1975 Collections.sort(required, new PositionalParametersSorter());
1976 try {
1977 processArguments(parsedCommands, argumentStack, required, initialized, originalArgs);
1978 } catch (ParameterException ex) {
1979 throw ex;
1980 } catch (Exception ex) {
1981 int offendingArgIndex = originalArgs.length - argumentStack.size() - 1;
1982 String arg = offendingArgIndex >= 0 && offendingArgIndex < originalArgs.length ? originalArgs[offendingArgIndex] : "?";
1983 throw ParameterException.create(CommandLine.this, ex, arg, offendingArgIndex, originalArgs);
1984 }
1985 if (!isAnyHelpRequested() && !required.isEmpty()) {
1986 for (Field missing : required) {
1987 if (missing.isAnnotationPresent(Option.class)) {
1988 throw MissingParameterException.create(CommandLine.this, required, separator);
1989 } else {
1990 assertNoMissingParameters(missing, Range.parameterArity(missing).min, argumentStack);
1991 }
1992 }
1993 }
1994 if (!unmatchedArguments.isEmpty()) {
1995 if (!isUnmatchedArgumentsAllowed()) { throw new UnmatchedArgumentException(CommandLine.this, unmatchedArguments); }
1996 if (tracer.isWarn()) { tracer.warn("Unmatched arguments: %s%n", unmatchedArguments); }
1997 }
1998 }
1999
2000 private void processArguments(List<CommandLine> parsedCommands,
2001 Stack<String> args,
2002 Collection<Field> required,
2003 Set<Field> initialized,
2004 String[] originalArgs) throws Exception {
2005 // arg must be one of:
2006 // 1. the "--" double dash separating options from positional arguments
2007 // 1. a stand-alone flag, like "-v" or "--verbose": no value required, must map to boolean or Boolean field
2008 // 2. a short option followed by an argument, like "-f file" or "-ffile": may map to any type of field
2009 // 3. a long option followed by an argument, like "-file out.txt" or "-file=out.txt"
2010 // 3. one or more remaining arguments without any associated options. Must be the last in the list.
2011 // 4. a combination of stand-alone options, like "-vxr". Equivalent to "-v -x -r", "-v true -x true -r true"
2012 // 5. a combination of stand-alone options and one option with an argument, like "-vxrffile"
2013
2014 while (!args.isEmpty()) {
2015 String arg = args.pop();
2016 if (tracer.isDebug()) {tracer.debug("Processing argument '%s'. Remainder=%s%n", arg, reverse((Stack<String>) args.clone()));}
2017
2018 // Double-dash separates options from positional arguments.
2019 // If found, then interpret the remaining args as positional parameters.
2020 if ("--".equals(arg)) {
2021 tracer.info("Found end-of-options delimiter '--'. Treating remainder as positional parameters.%n");
2022 processRemainderAsPositionalParameters(required, initialized, args);
2023 return; // we are done
2024 }
2025
2026 // if we find another command, we are done with the current command
2027 if (commands.containsKey(arg)) {
2028 if (!isHelpRequested && !required.isEmpty()) { // ensure current command portion is valid
2029 throw MissingParameterException.create(CommandLine.this, required, separator);
2030 }
2031 if (tracer.isDebug()) {tracer.debug("Found subcommand '%s' (%s)%n", arg, commands.get(arg).interpreter.command.getClass().getName());}
2032 commands.get(arg).interpreter.parse(parsedCommands, args, originalArgs);
2033 return; // remainder done by the command
2034 }
2035
2036 // First try to interpret the argument as a single option (as opposed to a compact group of options).
2037 // A single option may be without option parameters, like "-v" or "--verbose" (a boolean value),
2038 // or an option may have one or more option parameters.
2039 // A parameter may be attached to the option.
2040 boolean paramAttachedToOption = false;
2041 int separatorIndex = arg.indexOf(separator);
2042 if (separatorIndex > 0) {
2043 String key = arg.substring(0, separatorIndex);
2044 // be greedy. Consume the whole arg as an option if possible.
2045 if (optionName2Field.containsKey(key) && !optionName2Field.containsKey(arg)) {
2046 paramAttachedToOption = true;
2047 String optionParam = arg.substring(separatorIndex + separator.length());
2048 args.push(optionParam);
2049 arg = key;
2050 if (tracer.isDebug()) {tracer.debug("Separated '%s' option from '%s' option parameter%n", key, optionParam);}
2051 } else {
2052 if (tracer.isDebug()) {tracer.debug("'%s' contains separator '%s' but '%s' is not a known option%n", arg, separator, key);}
2053 }
2054 } else {
2055 if (tracer.isDebug()) {tracer.debug("'%s' cannot be separated into <option>%s<option-parameter>%n", arg, separator);}
2056 }
2057 if (optionName2Field.containsKey(arg)) {
2058 processStandaloneOption(required, initialized, arg, args, paramAttachedToOption);
2059 }
2060 // Compact (single-letter) options can be grouped with other options or with an argument.
2061 // only single-letter options can be combined with other options or with an argument
2062 else if (arg.length() > 2 && arg.startsWith("-")) {
2063 if (tracer.isDebug()) {tracer.debug("Trying to process '%s' as clustered short options%n", arg, args);}
2064 processClusteredShortOptions(required, initialized, arg, args);
2065 }
2066 // The argument could not be interpreted as an option.
2067 // We take this to mean that the remainder are positional arguments
2068 else {
2069 args.push(arg);
2070 if (tracer.isDebug()) {tracer.debug("Could not find option '%s', deciding whether to treat as unmatched option or positional parameter...%n", arg);}
2071 if (resemblesOption(arg)) { handleUnmatchedArguments(args.pop()); continue; } // #149
2072 if (tracer.isDebug()) {tracer.debug("No option named '%s' found. Processing remainder as positional parameters%n", arg);}
2073 processPositionalParameter(required, initialized, args);
2074 }
2075 }
2076 }
2077 private boolean resemblesOption(String arg) {
2078 int count = 0;
2079 for (String optionName : optionName2Field.keySet()) {
2080 for (int i = 0; i < arg.length(); i++) {
2081 if (optionName.length() > i && arg.charAt(i) == optionName.charAt(i)) { count++; } else { break; }
2082 }
2083 }
2084 boolean result = count > 0 && count * 10 >= optionName2Field.size() * 9; // at least one prefix char in common with 9 out of 10 options
2085 if (tracer.isDebug()) {tracer.debug("%s %s an option: %d matching prefix chars out of %d option names%n", arg, (result ? "resembles" : "doesn't resemble"), count, optionName2Field.size());}
2086 return result;
2087 }
2088 private void handleUnmatchedArguments(String arg) {Stack<String> args = new Stack<String>(); args.add(arg); handleUnmatchedArguments(args);}
2089 private void handleUnmatchedArguments(Stack<String> args) {
2090 while (!args.isEmpty()) { unmatchedArguments.add(args.pop()); } // addAll would give args in reverse order
2091 }
2092
2093 private void processRemainderAsPositionalParameters(Collection<Field> required, Set<Field> initialized, Stack<String> args) throws Exception {
2094 while (!args.empty()) {
2095 processPositionalParameter(required, initialized, args);
2096 }
2097 }
2098 private void processPositionalParameter(Collection<Field> required, Set<Field> initialized, Stack<String> args) throws Exception {
2099 if (tracer.isDebug()) {tracer.debug("Processing next arg as a positional parameter at index=%d. Remainder=%s%n", position, reverse((Stack<String>) args.clone()));}
2100 int consumed = 0;
2101 for (Field positionalParam : positionalParametersFields) {
2102 Range indexRange = Range.parameterIndex(positionalParam);
2103 if (!indexRange.contains(position)) {
2104 continue;
2105 }
2106 @SuppressWarnings("unchecked")
2107 Stack<String> argsCopy = (Stack<String>) args.clone();
2108 Range arity = Range.parameterArity(positionalParam);
2109 if (tracer.isDebug()) {tracer.debug("Position %d is in index range %s. Trying to assign args to %s, arity=%s%n", position, indexRange, positionalParam, arity);}
2110 assertNoMissingParameters(positionalParam, arity.min, argsCopy);
2111 int originalSize = argsCopy.size();
2112 applyOption(positionalParam, Parameters.class, arity, false, argsCopy, initialized, "args[" + indexRange + "] at position " + position);
2113 int count = originalSize - argsCopy.size();
2114 if (count > 0) { required.remove(positionalParam); }
2115 consumed = Math.max(consumed, count);
2116 }
2117 // remove processed args from the stack
2118 for (int i = 0; i < consumed; i++) { args.pop(); }
2119 position += consumed;
2120 if (tracer.isDebug()) {tracer.debug("Consumed %d arguments, moving position to index %d.%n", consumed, position);}
2121 if (consumed == 0 && !args.isEmpty()) {
2122 handleUnmatchedArguments(args.pop());
2123 }
2124 }
2125
2126 private void processStandaloneOption(Collection<Field> required,
2127 Set<Field> initialized,
2128 String arg,
2129 Stack<String> args,
2130 boolean paramAttachedToKey) throws Exception {
2131 Field field = optionName2Field.get(arg);
2132 required.remove(field);
2133 Range arity = Range.optionArity(field);
2134 if (paramAttachedToKey) {
2135 arity = arity.min(Math.max(1, arity.min)); // if key=value, minimum arity is at least 1
2136 }
2137 if (tracer.isDebug()) {tracer.debug("Found option named '%s': field %s, arity=%s%n", arg, field, arity);}
2138 applyOption(field, Option.class, arity, paramAttachedToKey, args, initialized, "option " + arg);
2139 }
2140
2141 private void processClusteredShortOptions(Collection<Field> required,
2142 Set<Field> initialized,
2143 String arg,
2144 Stack<String> args)
2145 throws Exception {
2146 String prefix = arg.substring(0, 1);
2147 String cluster = arg.substring(1);
2148 boolean paramAttachedToOption = true;
2149 do {
2150 if (cluster.length() > 0 && singleCharOption2Field.containsKey(cluster.charAt(0))) {
2151 Field field = singleCharOption2Field.get(cluster.charAt(0));
2152 Range arity = Range.optionArity(field);
2153 String argDescription = "option " + prefix + cluster.charAt(0);
2154 if (tracer.isDebug()) {tracer.debug("Found option '%s%s' in %s: field %s, arity=%s%n", prefix, cluster.charAt(0), arg, field, arity);}
2155 required.remove(field);
2156 cluster = cluster.length() > 0 ? cluster.substring(1) : "";
2157 paramAttachedToOption = cluster.length() > 0;
2158 if (cluster.startsWith(separator)) {// attached with separator, like -f=FILE or -v=true
2159 cluster = cluster.substring(separator.length());
2160 arity = arity.min(Math.max(1, arity.min)); // if key=value, minimum arity is at least 1
2161 }
2162 if (arity.min > 0 && !empty(cluster)) {
2163 if (tracer.isDebug()) {tracer.debug("Trying to process '%s' as option parameter%n", cluster);}
2164 }
2165 // arity may be >= 1, or
2166 // arity <= 0 && !cluster.startsWith(separator)
2167 // e.g., boolean @Option("-v", arity=0, varargs=true); arg "-rvTRUE", remainder cluster="TRUE"
2168 if (!empty(cluster)) {
2169 args.push(cluster); // interpret remainder as option parameter
2170 }
2171 int consumed = applyOption(field, Option.class, arity, paramAttachedToOption, args, initialized, argDescription);
2172 // only return if cluster (and maybe more) was consumed, otherwise continue do-while loop
2173 if (empty(cluster) || consumed > 0 || args.isEmpty()) {
2174 return;
2175 }
2176 cluster = args.pop();
2177 } else { // cluster is empty || cluster.charAt(0) is not a short option key
2178 if (cluster.length() == 0) { // we finished parsing a group of short options like -rxv
2179 return; // return normally and parse the next arg
2180 }
2181 // We get here when the remainder of the cluster group is neither an option,
2182 // nor a parameter that the last option could consume.
2183 if (arg.endsWith(cluster)) {
2184 args.push(paramAttachedToOption ? prefix + cluster : cluster);
2185 if (args.peek().equals(arg)) { // #149 be consistent between unmatched short and long options
2186 if (tracer.isDebug()) {tracer.debug("Could not match any short options in %s, deciding whether to treat as unmatched option or positional parameter...%n", arg);}
2187 if (resemblesOption(arg)) { handleUnmatchedArguments(args.pop()); return; } // #149
2188 processPositionalParameter(required, initialized, args);
2189 return;
2190 }
2191 // remainder was part of a clustered group that could not be completely parsed
2192 if (tracer.isDebug()) {tracer.debug("No option found for %s in %s%n", cluster, arg);}
2193 handleUnmatchedArguments(args.pop());
2194 } else {
2195 args.push(cluster);
2196 if (tracer.isDebug()) {tracer.debug("%s is not an option parameter for %s%n", cluster, arg);}
2197 processPositionalParameter(required, initialized, args);
2198 }
2199 return;
2200 }
2201 } while (true);
2202 }
2203
2204 private int applyOption(Field field,
2205 Class<?> annotation,
2206 Range arity,
2207 boolean valueAttachedToOption,
2208 Stack<String> args,
2209 Set<Field> initialized,
2210 String argDescription) throws Exception {
2211 updateHelpRequested(field);
2212 int length = args.size();
2213 assertNoMissingParameters(field, arity.min, args);
2214
2215 Class<?> cls = field.getType();
2216 if (cls.isArray()) {
2217 return applyValuesToArrayField(field, annotation, arity, args, cls, argDescription);
2218 }
2219 if (Collection.class.isAssignableFrom(cls)) {
2220 return applyValuesToCollectionField(field, annotation, arity, args, cls, argDescription);
2221 }
2222 if (Map.class.isAssignableFrom(cls)) {
2223 return applyValuesToMapField(field, annotation, arity, args, cls, argDescription);
2224 }
2225 cls = getTypeAttribute(field)[0]; // field may be interface/abstract type, use annotation to get concrete type
2226 return applyValueToSingleValuedField(field, arity, args, cls, initialized, argDescription);
2227 }
2228
2229 private int applyValueToSingleValuedField(Field field,
2230 Range arity,
2231 Stack<String> args,
2232 Class<?> cls,
2233 Set<Field> initialized,
2234 String argDescription) throws Exception {
2235 boolean noMoreValues = args.isEmpty();
2236 String value = args.isEmpty() ? null : trim(args.pop()); // unquote the value
2237 int result = arity.min; // the number or args we need to consume
2238
2239 // special logic for booleans: BooleanConverter accepts only "true" or "false".
2240 if ((cls == Boolean.class || cls == Boolean.TYPE) && arity.min <= 0) {
2241
2242 // boolean option with arity = 0..1 or 0..*: value MAY be a param
2243 if (arity.max > 0 && ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))) {
2244 result = 1; // if it is a varargs we only consume 1 argument if it is a boolean value
2245 } else {
2246 if (value != null) {
2247 args.push(value); // we don't consume the value
2248 }
2249 Boolean currentValue = (Boolean) field.get(command);
2250 value = String.valueOf(currentValue == null ? true : !currentValue); // #147 toggle existing boolean value
2251 }
2252 }
2253 if (noMoreValues && value == null) {
2254 return 0;
2255 }
2256 ITypeConverter<?> converter = getTypeConverter(cls, field);
2257 Object newValue = tryConvert(field, -1, converter, value, cls);
2258 Object oldValue = field.get(command);
2259 TraceLevel level = TraceLevel.INFO;
2260 String traceMessage = "Setting %s field '%s.%s' to '%5$s' (was '%4$s') for %6$s%n";
2261 if (initialized != null) {
2262 if (initialized.contains(field)) {
2263 if (!isOverwrittenOptionsAllowed()) {
2264 throw new OverwrittenOptionException(CommandLine.this, optionDescription("", field, 0) + " should be specified only once");
2265 }
2266 level = TraceLevel.WARN;
2267 traceMessage = "Overwriting %s field '%s.%s' value '%s' with '%s' for %s%n";
2268 }
2269 initialized.add(field);
2270 }
2271 if (tracer.level.isEnabled(level)) { level.print(tracer, traceMessage, field.getType().getSimpleName(),
2272 field.getDeclaringClass().getSimpleName(), field.getName(), String.valueOf(oldValue), String.valueOf(newValue), argDescription);
2273 }
2274 field.set(command, newValue);
2275 return result;
2276 }
2277 private int applyValuesToMapField(Field field,
2278 Class<?> annotation,
2279 Range arity,
2280 Stack<String> args,
2281 Class<?> cls,
2282 String argDescription) throws Exception {
2283 Class<?>[] classes = getTypeAttribute(field);
2284 if (classes.length < 2) { throw new ParameterException(CommandLine.this, "Field " + field + " needs two types (one for the map key, one for the value) but only has " + classes.length + " types configured."); }
2285 ITypeConverter<?> keyConverter = getTypeConverter(classes[0], field);
2286 ITypeConverter<?> valueConverter = getTypeConverter(classes[1], field);
2287 Map<Object, Object> result = (Map<Object, Object>) field.get(command);
2288 if (result == null) {
2289 result = createMap(cls);
2290 field.set(command, result);
2291 }
2292 int originalSize = result.size();
2293 consumeMapArguments(field, arity, args, classes, keyConverter, valueConverter, result, argDescription);
2294 return result.size() - originalSize;
2295 }
2296
2297 private void consumeMapArguments(Field field,
2298 Range arity,
2299 Stack<String> args,
2300 Class<?>[] classes,
2301 ITypeConverter<?> keyConverter,
2302 ITypeConverter<?> valueConverter,
2303 Map<Object, Object> result,
2304 String argDescription) throws Exception {
2305 // first do the arity.min mandatory parameters
2306 for (int i = 0; i < arity.min; i++) {
2307 consumeOneMapArgument(field, arity, args, classes, keyConverter, valueConverter, result, i, argDescription);
2308 }
2309 // now process the varargs if any
2310 for (int i = arity.min; i < arity.max && !args.isEmpty(); i++) {
2311 if (!field.isAnnotationPresent(Parameters.class)) {
2312 if (commands.containsKey(args.peek()) || isOption(args.peek())) {
2313 return;
2314 }
2315 }
2316 consumeOneMapArgument(field, arity, args, classes, keyConverter, valueConverter, result, i, argDescription);
2317 }
2318 }
2319
2320 private void consumeOneMapArgument(Field field,
2321 Range arity,
2322 Stack<String> args,
2323 Class<?>[] classes,
2324 ITypeConverter<?> keyConverter, ITypeConverter<?> valueConverter,
2325 Map<Object, Object> result,
2326 int index,
2327 String argDescription) throws Exception {
2328 String[] values = split(trim(args.pop()), field);
2329 for (String value : values) {
2330 String[] keyValue = value.split("=");
2331 if (keyValue.length < 2) {
2332 String splitRegex = splitRegex(field);
2333 if (splitRegex.length() == 0) {
2334 throw new ParameterException(CommandLine.this, "Value for option " + optionDescription("", field,
2335 0) + " should be in KEY=VALUE format but was " + value);
2336 } else {
2337 throw new ParameterException(CommandLine.this, "Value for option " + optionDescription("", field,
2338 0) + " should be in KEY=VALUE[" + splitRegex + "KEY=VALUE]... format but was " + value);
2339 }
2340 }
2341 Object mapKey = tryConvert(field, index, keyConverter, keyValue[0], classes[0]);
2342 Object mapValue = tryConvert(field, index, valueConverter, keyValue[1], classes[1]);
2343 result.put(mapKey, mapValue);
2344 if (tracer.isInfo()) {tracer.info("Putting [%s : %s] in %s<%s, %s> field '%s.%s' for %s%n", String.valueOf(mapKey), String.valueOf(mapValue),
2345 result.getClass().getSimpleName(), classes[0].getSimpleName(), classes[1].getSimpleName(), field.getDeclaringClass().getSimpleName(), field.getName(), argDescription);}
2346 }
2347 }
2348
2349 private void checkMaxArityExceeded(Range arity, int remainder, Field field, String[] values) {
2350 if (values.length <= remainder) { return; }
2351 String desc = arity.max == remainder ? "" + remainder : arity + ", remainder=" + remainder;
2352 throw new MaxValuesforFieldExceededException(CommandLine.this, optionDescription("", field, -1) +
2353 " max number of values (" + arity.max + ") exceeded: remainder is " + remainder + " but " +
2354 values.length + " values were specified: " + Arrays.toString(values));
2355 }
2356
2357 private int applyValuesToArrayField(Field field,
2358 Class<?> annotation,
2359 Range arity,
2360 Stack<String> args,
2361 Class<?> cls,
2362 String argDescription) throws Exception {
2363 Object existing = field.get(command);
2364 int length = existing == null ? 0 : Array.getLength(existing);
2365 Class<?> type = getTypeAttribute(field)[0];
2366 List<Object> converted = consumeArguments(field, annotation, arity, args, type, length, argDescription);
2367 List<Object> newValues = new ArrayList<Object>();
2368 for (int i = 0; i < length; i++) {
2369 newValues.add(Array.get(existing, i));
2370 }
2371 for (Object obj : converted) {
2372 if (obj instanceof Collection<?>) {
2373 newValues.addAll((Collection<?>) obj);
2374 } else {
2375 newValues.add(obj);
2376 }
2377 }
2378 Object array = Array.newInstance(type, newValues.size());
2379 field.set(command, array);
2380 for (int i = 0; i < newValues.size(); i++) {
2381 Array.set(array, i, newValues.get(i));
2382 }
2383 return converted.size(); // return how many args were consumed
2384 }
2385
2386 @SuppressWarnings("unchecked")
2387 private int applyValuesToCollectionField(Field field,
2388 Class<?> annotation,
2389 Range arity,
2390 Stack<String> args,
2391 Class<?> cls,
2392 String argDescription) throws Exception {
2393 Collection<Object> collection = (Collection<Object>) field.get(command);
2394 Class<?> type = getTypeAttribute(field)[0];
2395 int length = collection == null ? 0 : collection.size();
2396 List<Object> converted = consumeArguments(field, annotation, arity, args, type, length, argDescription);
2397 if (collection == null) {
2398 collection = createCollection(cls);
2399 field.set(command, collection);
2400 }
2401 for (Object element : converted) {
2402 if (element instanceof Collection<?>) {
2403 collection.addAll((Collection<?>) element);
2404 } else {
2405 collection.add(element);
2406 }
2407 }
2408 return converted.size();
2409 }
2410
2411 private List<Object> consumeArguments(Field field,
2412 Class<?> annotation,
2413 Range arity,
2414 Stack<String> args,
2415 Class<?> type,
2416 int originalSize,
2417 String argDescription) throws Exception {
2418 List<Object> result = new ArrayList<Object>();
2419
2420 // first do the arity.min mandatory parameters
2421 for (int i = 0; i < arity.min; i++) {
2422 consumeOneArgument(field, arity, args, type, result, i, originalSize, argDescription);
2423 }
2424 // now process the varargs if any
2425 for (int i = arity.min; i < arity.max && !args.isEmpty(); i++) {
2426 if (annotation != Parameters.class) { // for vararg Options, we stop if we encounter '--', a command, or another option
2427 if (commands.containsKey(args.peek()) || isOption(args.peek())) {
2428 return result;
2429 }
2430 }
2431 consumeOneArgument(field, arity, args, type, result, i, originalSize, argDescription);
2432 }
2433 return result;
2434 }
2435
2436 private int consumeOneArgument(Field field,
2437 Range arity,
2438 Stack<String> args,
2439 Class<?> type,
2440 List<Object> result,
2441 int index,
2442 int originalSize,
2443 String argDescription) throws Exception {
2444 String[] values = split(trim(args.pop()), field);
2445 ITypeConverter<?> converter = getTypeConverter(type, field);
2446
2447 for (int j = 0; j < values.length; j++) {
2448 result.add(tryConvert(field, index, converter, values[j], type));
2449 if (tracer.isInfo()) {
2450 if (field.getType().isArray()) {
2451 tracer.info("Adding [%s] to %s[] field '%s.%s' for %s%n", String.valueOf(result.get(result.size() - 1)), type.getSimpleName(), field.getDeclaringClass().getSimpleName(), field.getName(), argDescription);
2452 } else {
2453 tracer.info("Adding [%s] to %s<%s> field '%s.%s' for %s%n", String.valueOf(result.get(result.size() - 1)), field.getType().getSimpleName(), type.getSimpleName(), field.getDeclaringClass().getSimpleName(), field.getName(), argDescription);
2454 }
2455 }
2456 }
2457 //checkMaxArityExceeded(arity, max, field, values);
2458 return ++index;
2459 }
2460
2461 private String splitRegex(Field field) {
2462 if (field.isAnnotationPresent(Option.class)) { return field.getAnnotation(Option.class).split(); }
2463 if (field.isAnnotationPresent(Parameters.class)) { return field.getAnnotation(Parameters.class).split(); }
2464 return "";
2465 }
2466 private String[] split(String value, Field field) {
2467 String regex = splitRegex(field);
2468 return regex.length() == 0 ? new String[] {value} : value.split(regex);
2469 }
2470
2471 /**
2472 * Called when parsing varargs parameters for a multi-value option.
2473 * When an option is encountered, the remainder should not be interpreted as vararg elements.
2474 * @param arg the string to determine whether it is an option or not
2475 * @return true if it is an option, false otherwise
2476 */
2477 private boolean isOption(String arg) {
2478 if ("--".equals(arg)) {
2479 return true;
2480 }
2481 // not just arg prefix: we may be in the middle of parsing -xrvfFILE
2482 if (optionName2Field.containsKey(arg)) { // -v or -f or --file (not attached to param or other option)
2483 return true;
2484 }
2485 int separatorIndex = arg.indexOf(separator);
2486 if (separatorIndex > 0) { // -f=FILE or --file==FILE (attached to param via separator)
2487 if (optionName2Field.containsKey(arg.substring(0, separatorIndex))) {
2488 return true;
2489 }
2490 }
2491 return (arg.length() > 2 && arg.startsWith("-") && singleCharOption2Field.containsKey(arg.charAt(1)));
2492 }
2493 private Object tryConvert(Field field, int index, ITypeConverter<?> converter, String value, Class<?> type)
2494 throws Exception {
2495 try {
2496 return converter.convert(value);
2497 } catch (TypeConversionException ex) {
2498 throw new ParameterException(CommandLine.this, ex.getMessage() + optionDescription(" for ", field, index));
2499 } catch (Exception other) {
2500 String desc = optionDescription(" for ", field, index) + ": " + other;
2501 throw new ParameterException(CommandLine.this, "Could not convert '" + value + "' to " + type.getSimpleName() + desc, other);
2502 }
2503 }
2504
2505 private String optionDescription(String prefix, Field field, int index) {
2506 Help.IParamLabelRenderer labelRenderer = Help.createMinimalParamLabelRenderer();
2507 String desc = "";
2508 if (field.isAnnotationPresent(Option.class)) {
2509 desc = prefix + "option '" + field.getAnnotation(Option.class).names()[0] + "'";
2510 if (index >= 0) {
2511 Range arity = Range.optionArity(field);
2512 if (arity.max > 1) {
2513 desc += " at index " + index;
2514 }
2515 desc += " (" + labelRenderer.renderParameterLabel(field, Help.Ansi.OFF, Collections.<IStyle>emptyList()) + ")";
2516 }
2517 } else if (field.isAnnotationPresent(Parameters.class)) {
2518 Range indexRange = Range.parameterIndex(field);
2519 Text label = labelRenderer.renderParameterLabel(field, Help.Ansi.OFF, Collections.<IStyle>emptyList());
2520 desc = prefix + "positional parameter at index " + indexRange + " (" + label + ")";
2521 }
2522 return desc;
2523 }
2524
2525 private boolean isAnyHelpRequested() { return isHelpRequested || versionHelpRequested || usageHelpRequested; }
2526
2527 private void updateHelpRequested(Field field) {
2528 if (field.isAnnotationPresent(Option.class)) {
2529 isHelpRequested |= is(field, "help", field.getAnnotation(Option.class).help());
2530 CommandLine.this.versionHelpRequested |= is(field, "versionHelp", field.getAnnotation(Option.class).versionHelp());
2531 CommandLine.this.usageHelpRequested |= is(field, "usageHelp", field.getAnnotation(Option.class).usageHelp());
2532 }
2533 }
2534 private boolean is(Field f, String description, boolean value) {
2535 if (value) { if (tracer.isInfo()) {tracer.info("Field '%s.%s' has '%s' annotation: not validating required fields%n", f.getDeclaringClass().getSimpleName(), f.getName(), description); }}
2536 return value;
2537 }
2538 @SuppressWarnings("unchecked")
2539 private Collection<Object> createCollection(Class<?> collectionClass) throws Exception {
2540 if (collectionClass.isInterface()) {
2541 if (List.class.isAssignableFrom(collectionClass)) {
2542 return new ArrayList<Object>();
2543 } else if (SortedSet.class.isAssignableFrom(collectionClass)) {
2544 return new TreeSet<Object>();
2545 } else if (Set.class.isAssignableFrom(collectionClass)) {
2546 return new LinkedHashSet<Object>();
2547 } else if (Queue.class.isAssignableFrom(collectionClass)) {
2548 return new LinkedList<Object>(); // ArrayDeque is only available since 1.6
2549 }
2550 return new ArrayList<Object>();
2551 }
2552 // custom Collection implementation class must have default constructor
2553 return (Collection<Object>) collectionClass.newInstance();
2554 }
2555 private Map<Object, Object> createMap(Class<?> mapClass) throws Exception {
2556 try { // if it is an implementation class, instantiate it
2557 return (Map<Object, Object>) mapClass.newInstance();
2558 } catch (Exception ignored) {}
2559 return new LinkedHashMap<Object, Object>();
2560 }
2561 private ITypeConverter<?> getTypeConverter(final Class<?> type, Field field) {
2562 ITypeConverter<?> result = converterRegistry.get(type);
2563 if (result != null) {
2564 return result;
2565 }
2566 if (type.isEnum()) {
2567 return new ITypeConverter<Object>() {
2568 @SuppressWarnings("unchecked")
2569 public Object convert(String value) throws Exception {
2570 return Enum.valueOf((Class<Enum>) type, value);
2571 }
2572 };
2573 }
2574 throw new MissingTypeConverterException(CommandLine.this, "No TypeConverter registered for " + type.getName() + " of field " + field);
2575 }
2576
2577 private void assertNoMissingParameters(Field field, int arity, Stack<String> args) {
2578 if (arity > args.size()) {
2579 if (arity == 1) {
2580 if (field.isAnnotationPresent(Option.class)) {
2581 throw new MissingParameterException(CommandLine.this, "Missing required parameter for " +
2582 optionDescription("", field, 0));
2583 }
2584 Range indexRange = Range.parameterIndex(field);
2585 Help.IParamLabelRenderer labelRenderer = Help.createMinimalParamLabelRenderer();
2586 String sep = "";
2587 String names = "";
2588 int count = 0;
2589 for (int i = indexRange.min; i < positionalParametersFields.size(); i++) {
2590 if (Range.parameterArity(positionalParametersFields.get(i)).min > 0) {
2591 names += sep + labelRenderer.renderParameterLabel(positionalParametersFields.get(i),
2592 Help.Ansi.OFF, Collections.<IStyle>emptyList());
2593 sep = ", ";
2594 count++;
2595 }
2596 }
2597 String msg = "Missing required parameter";
2598 Range paramArity = Range.parameterArity(field);
2599 if (paramArity.isVariable) {
2600 msg += "s at positions " + indexRange + ": ";
2601 } else {
2602 msg += (count > 1 ? "s: " : ": ");
2603 }
2604 throw new MissingParameterException(CommandLine.this, msg + names);
2605 }
2606 if (args.isEmpty()) {
2607 throw new MissingParameterException(CommandLine.this, optionDescription("", field, 0) +
2608 " requires at least " + arity + " values, but none were specified.");
2609 }
2610 throw new MissingParameterException(CommandLine.this, optionDescription("", field, 0) +
2611 " requires at least " + arity + " values, but only " + args.size() + " were specified: " + reverse(args));
2612 }
2613 }
2614 private String trim(String value) {
2615 return unquote(value);
2616 }
2617
2618 private String unquote(String value) {
2619 return value == null
2620 ? null
2621 : (value.length() > 1 && value.startsWith("\"") && value.endsWith("\""))
2622 ? value.substring(1, value.length() - 1)
2623 : value;
2624 }
2625 }
2626 private static class PositionalParametersSorter implements Comparator<Field> {
2627 public int compare(Field o1, Field o2) {
2628 int result = Range.parameterIndex(o1).compareTo(Range.parameterIndex(o2));
2629 return (result == 0) ? Range.parameterArity(o1).compareTo(Range.parameterArity(o2)) : result;
2630 }
2631 }
2632 /**
2633 * Inner class to group the built-in {@link ITypeConverter} implementations.
2634 */
2635 private static class BuiltIn {
2636 static class PathConverter implements ITypeConverter<Path> {
2637 @Override public Path convert(final String value) { return Paths.get(value); }
2638 }
2639 static class StringConverter implements ITypeConverter<String> {
2640 public String convert(String value) { return value; }
2641 }
2642 static class StringBuilderConverter implements ITypeConverter<StringBuilder> {
2643 public StringBuilder convert(String value) { return new StringBuilder(value); }
2644 }
2645 static class CharSequenceConverter implements ITypeConverter<CharSequence> {
2646 public String convert(String value) { return value; }
2647 }
2648 /** Converts text to a {@code Byte} by delegating to {@link Byte#valueOf(String)}.*/
2649 static class ByteConverter implements ITypeConverter<Byte> {
2650 public Byte convert(String value) { return Byte.valueOf(value); }
2651 }
2652 /** Converts {@code "true"} or {@code "false"} to a {@code Boolean}. Other values result in a ParameterException.*/
2653 static class BooleanConverter implements ITypeConverter<Boolean> {
2654 public Boolean convert(String value) {
2655 if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
2656 return Boolean.parseBoolean(value);
2657 } else {
2658 throw new TypeConversionException("'" + value + "' is not a boolean");
2659 }
2660 }
2661 }
2662 static class CharacterConverter implements ITypeConverter<Character> {
2663 public Character convert(String value) {
2664 if (value.length() > 1) {
2665 throw new TypeConversionException("'" + value + "' is not a single character");
2666 }
2667 return value.charAt(0);
2668 }
2669 }
2670 /** Converts text to a {@code Short} by delegating to {@link Short#valueOf(String)}.*/
2671 static class ShortConverter implements ITypeConverter<Short> {
2672 public Short convert(String value) { return Short.valueOf(value); }
2673 }
2674 /** Converts text to an {@code Integer} by delegating to {@link Integer#valueOf(String)}.*/
2675 static class IntegerConverter implements ITypeConverter<Integer> {
2676 public Integer convert(String value) { return Integer.valueOf(value); }
2677 }
2678 /** Converts text to a {@code Long} by delegating to {@link Long#valueOf(String)}.*/
2679 static class LongConverter implements ITypeConverter<Long> {
2680 public Long convert(String value) { return Long.valueOf(value); }
2681 }
2682 static class FloatConverter implements ITypeConverter<Float> {
2683 public Float convert(String value) { return Float.valueOf(value); }
2684 }
2685 static class DoubleConverter implements ITypeConverter<Double> {
2686 public Double convert(String value) { return Double.valueOf(value); }
2687 }
2688 static class FileConverter implements ITypeConverter<File> {
2689 public File convert(String value) { return new File(value); }
2690 }
2691 static class URLConverter implements ITypeConverter<URL> {
2692 public URL convert(String value) throws MalformedURLException { return new URL(value); }
2693 }
2694 static class URIConverter implements ITypeConverter<URI> {
2695 public URI convert(String value) throws URISyntaxException { return new URI(value); }
2696 }
2697 /** Converts text in {@code yyyy-mm-dd} format to a {@code java.util.Date}. ParameterException on failure. */
2698 static class ISO8601DateConverter implements ITypeConverter<Date> {
2699 public Date convert(String value) {
2700 try {
2701 return new SimpleDateFormat("yyyy-MM-dd").parse(value);
2702 } catch (ParseException e) {
2703 throw new TypeConversionException("'" + value + "' is not a yyyy-MM-dd date");
2704 }
2705 }
2706 }
2707 /** Converts text in any of the following formats to a {@code java.sql.Time}: {@code HH:mm}, {@code HH:mm:ss},
2708 * {@code HH:mm:ss.SSS}, {@code HH:mm:ss,SSS}. Other formats result in a ParameterException. */
2709 static class ISO8601TimeConverter implements ITypeConverter<Time> {
2710 public Time convert(String value) {
2711 try {
2712 if (value.length() <= 5) {
2713 return new Time(new SimpleDateFormat("HH:mm").parse(value).getTime());
2714 } else if (value.length() <= 8) {
2715 return new Time(new SimpleDateFormat("HH:mm:ss").parse(value).getTime());
2716 } else if (value.length() <= 12) {
2717 try {
2718 return new Time(new SimpleDateFormat("HH:mm:ss.SSS").parse(value).getTime());
2719 } catch (ParseException e2) {
2720 return new Time(new SimpleDateFormat("HH:mm:ss,SSS").parse(value).getTime());
2721 }
2722 }
2723 } catch (ParseException ignored) {
2724 // ignored because we throw a ParameterException below
2725 }
2726 throw new TypeConversionException("'" + value + "' is not a HH:mm[:ss[.SSS]] time");
2727 }
2728 }
2729 static class BigDecimalConverter implements ITypeConverter<BigDecimal> {
2730 public BigDecimal convert(String value) { return new BigDecimal(value); }
2731 }
2732 static class BigIntegerConverter implements ITypeConverter<BigInteger> {
2733 public BigInteger convert(String value) { return new BigInteger(value); }
2734 }
2735 static class CharsetConverter implements ITypeConverter<Charset> {
2736 public Charset convert(String s) { return Charset.forName(s); }
2737 }
2738 /** Converts text to a {@code InetAddress} by delegating to {@link InetAddress#getByName(String)}. */
2739 static class InetAddressConverter implements ITypeConverter<InetAddress> {
2740 public InetAddress convert(String s) throws Exception { return InetAddress.getByName(s); }
2741 }
2742 static class PatternConverter implements ITypeConverter<Pattern> {
2743 public Pattern convert(String s) { return Pattern.compile(s); }
2744 }
2745 static class UUIDConverter implements ITypeConverter<UUID> {
2746 public UUID convert(String s) throws Exception { return UUID.fromString(s); }
2747 }
2748 private BuiltIn() {} // private constructor: never instantiate
2749 }
2750
2751 /**
2752 * A collection of methods and inner classes that provide fine-grained control over the contents and layout of
2753 * the usage help message to display to end users when help is requested or invalid input values were specified.
2754 * <h3>Layered API</h3>
2755 * <p>The {@link Command} annotation provides the easiest way to customize usage help messages. See
2756 * the <a href="https://remkop.github.io/picocli/index.html#_usage_help">Manual</a> for details.</p>
2757 * <p>This Help class provides high-level functions to create sections of the usage help message and headings
2758 * for these sections. Instead of calling the {@link CommandLine#usage(PrintStream, CommandLine.Help.ColorScheme)}
2759 * method, application authors may want to create a custom usage help message by reorganizing sections in a
2760 * different order and/or adding custom sections.</p>
2761 * <p>Finally, the Help class contains inner classes and interfaces that can be used to create custom help messages.</p>
2762 * <h4>IOptionRenderer and IParameterRenderer</h4>
2763 * <p>Renders a field annotated with {@link Option} or {@link Parameters} to an array of {@link Text} values.
2764 * By default, these values are</p><ul>
2765 * <li>mandatory marker character (if the option/parameter is {@link Option#required() required})</li>
2766 * <li>short option name (empty for parameters)</li>
2767 * <li>comma or empty (empty for parameters)</li>
2768 * <li>long option names (the parameter {@link IParamLabelRenderer label} for parameters)</li>
2769 * <li>description</li>
2770 * </ul>
2771 * <p>Other components rely on this ordering.</p>
2772 * <h4>Layout</h4>
2773 * <p>Delegates to the renderers to create {@link Text} values for the annotated fields, and uses a
2774 * {@link TextTable} to display these values in tabular format. Layout is responsible for deciding which values
2775 * to display where in the table. By default, Layout shows one option or parameter per table row.</p>
2776 * <h4>TextTable</h4>
2777 * <p>Responsible for spacing out {@link Text} values according to the {@link Column} definitions the table was
2778 * created with. Columns have a width, indentation, and an overflow policy that decides what to do if a value is
2779 * longer than the column's width.</p>
2780 * <h4>Text</h4>
2781 * <p>Encapsulates rich text with styles and colors in a way that other components like {@link TextTable} are
2782 * unaware of the embedded ANSI escape codes.</p>
2783 */
2784 public static class Help {
2785 /** Constant String holding the default program name: {@value} */
2786 protected static final String DEFAULT_COMMAND_NAME = "<main class>";
2787
2788 /** Constant String holding the default string that separates options from option parameters: {@value} */
2789 protected static final String DEFAULT_SEPARATOR = "=";
2790
2791 private final static int usageHelpWidth = 80;
2792 private final static int optionsColumnWidth = 2 + 2 + 1 + 24;
2793 private final Object command;
2794 private final Map<String, Help> commands = new LinkedHashMap<String, Help>();
2795 final ColorScheme colorScheme;
2796
2797 /** Immutable list of fields annotated with {@link Option}, in declaration order. */
2798 public final List<Field> optionFields;
2799
2800 /** Immutable list of fields annotated with {@link Parameters}, or an empty list if no such field exists. */
2801 public final List<Field> positionalParametersFields;
2802
2803 /** The String to use as the separator between options and option parameters. {@code "="} by default,
2804 * initialized from {@link Command#separator()} if defined.
2805 * @see #parameterLabelRenderer */
2806 public String separator;
2807
2808 /** The String to use as the program name in the synopsis line of the help message.
2809 * {@link #DEFAULT_COMMAND_NAME} by default, initialized from {@link Command#name()} if defined. */
2810 public String commandName = DEFAULT_COMMAND_NAME;
2811
2812 /** Optional text lines to use as the description of the help message, displayed between the synopsis and the
2813 * options list. Initialized from {@link Command#description()} if the {@code Command} annotation is present,
2814 * otherwise this is an empty array and the help message has no description.
2815 * Applications may programmatically set this field to create a custom help message. */
2816 public String[] description = {};
2817
2818 /** Optional custom synopsis lines to use instead of the auto-generated synopsis.
2819 * Initialized from {@link Command#customSynopsis()} if the {@code Command} annotation is present,
2820 * otherwise this is an empty array and the synopsis is generated.
2821 * Applications may programmatically set this field to create a custom help message. */
2822 public String[] customSynopsis = {};
2823
2824 /** Optional header lines displayed at the top of the help message. For subcommands, the first header line is
2825 * displayed in the list of commands. Values are initialized from {@link Command#header()}
2826 * if the {@code Command} annotation is present, otherwise this is an empty array and the help message has no
2827 * header. Applications may programmatically set this field to create a custom help message. */
2828 public String[] header = {};
2829
2830 /** Optional footer text lines displayed at the bottom of the help message. Initialized from
2831 * {@link Command#footer()} if the {@code Command} annotation is present, otherwise this is an empty array and
2832 * the help message has no footer.
2833 * Applications may programmatically set this field to create a custom help message. */
2834 public String[] footer = {};
2835
2836 /** Option and positional parameter value label renderer used for the synopsis line(s) and the option list.
2837 * By default initialized to the result of {@link #createDefaultParamLabelRenderer()}, which takes a snapshot
2838 * of the {@link #separator} at construction time. If the separator is modified after Help construction, you
2839 * may need to re-initialize this field by calling {@link #createDefaultParamLabelRenderer()} again. */
2840 public IParamLabelRenderer parameterLabelRenderer;
2841
2842 /** If {@code true}, the synopsis line(s) will show an abbreviated synopsis without detailed option names. */
2843 public Boolean abbreviateSynopsis;
2844
2845 /** If {@code true}, the options list is sorted alphabetically. */
2846 public Boolean sortOptions;
2847
2848 /** If {@code true}, the options list will show default values for all options except booleans. */
2849 public Boolean showDefaultValues;
2850
2851 /** Character used to prefix required options in the options list. */
2852 public Character requiredOptionMarker;
2853
2854 /** Optional heading preceding the header section. Initialized from {@link Command#headerHeading()}, or null. */
2855 public String headerHeading;
2856
2857 /** Optional heading preceding the synopsis. Initialized from {@link Command#synopsisHeading()}, {@code "Usage: "} by default. */
2858 public String synopsisHeading;
2859
2860 /** Optional heading preceding the description section. Initialized from {@link Command#descriptionHeading()}, or null. */
2861 public String descriptionHeading;
2862
2863 /** Optional heading preceding the parameter list. Initialized from {@link Command#parameterListHeading()}, or null. */
2864 public String parameterListHeading;
2865
2866 /** Optional heading preceding the options list. Initialized from {@link Command#optionListHeading()}, or null. */
2867 public String optionListHeading;
2868
2869 /** Optional heading preceding the subcommand list. Initialized from {@link Command#commandListHeading()}. {@code "Commands:%n"} by default. */
2870 public String commandListHeading;
2871
2872 /** Optional heading preceding the footer section. Initialized from {@link Command#footerHeading()}, or null. */
2873 public String footerHeading;
2874
2875 /** Constructs a new {@code Help} instance with a default color scheme, initialized from annotatations
2876 * on the specified class and superclasses.
2877 * @param command the annotated object to create usage help for */
2878 public Help(Object command) {
2879 this(command, Ansi.AUTO);
2880 }
2881
2882 /** Constructs a new {@code Help} instance with a default color scheme, initialized from annotatations
2883 * on the specified class and superclasses.
2884 * @param command the annotated object to create usage help for
2885 * @param ansi whether to emit ANSI escape codes or not */
2886 public Help(Object command, Ansi ansi) {
2887 this(command, defaultColorScheme(ansi));
2888 }
2889
2890 /** Constructs a new {@code Help} instance with the specified color scheme, initialized from annotatations
2891 * on the specified class and superclasses.
2892 * @param command the annotated object to create usage help for
2893 * @param colorScheme the color scheme to use */
2894 public Help(Object command, ColorScheme colorScheme) {
2895 this.command = Assert.notNull(command, "command");
2896 this.colorScheme = Assert.notNull(colorScheme, "colorScheme").applySystemProperties();
2897 List<Field> options = new ArrayList<Field>();
2898 List<Field> operands = new ArrayList<Field>();
2899 Class<?> cls = command.getClass();
2900 while (cls != null) {
2901 for (Field field : cls.getDeclaredFields()) {
2902 field.setAccessible(true);
2903 if (field.isAnnotationPresent(Option.class)) {
2904 Option option = field.getAnnotation(Option.class);
2905 if (!option.hidden()) { // hidden options should not appear in usage help
2906 // TODO remember longest concatenated option string length (issue #45)
2907 options.add(field);
2908 }
2909 }
2910 if (field.isAnnotationPresent(Parameters.class)) {
2911 operands.add(field);
2912 }
2913 }
2914 // superclass values should not overwrite values if both class and superclass have a @Command annotation
2915 if (cls.isAnnotationPresent(Command.class)) {
2916 Command cmd = cls.getAnnotation(Command.class);
2917 if (DEFAULT_COMMAND_NAME.equals(commandName)) {
2918 commandName = cmd.name();
2919 }
2920 separator = (separator == null) ? cmd.separator() : separator;
2921 abbreviateSynopsis = (abbreviateSynopsis == null) ? cmd.abbreviateSynopsis() : abbreviateSynopsis;
2922 sortOptions = (sortOptions == null) ? cmd.sortOptions() : sortOptions;
2923 requiredOptionMarker = (requiredOptionMarker == null) ? cmd.requiredOptionMarker() : requiredOptionMarker;
2924 showDefaultValues = (showDefaultValues == null) ? cmd.showDefaultValues() : showDefaultValues;
2925 customSynopsis = empty(customSynopsis) ? cmd.customSynopsis() : customSynopsis;
2926 description = empty(description) ? cmd.description() : description;
2927 header = empty(header) ? cmd.header() : header;
2928 footer = empty(footer) ? cmd.footer() : footer;
2929 headerHeading = empty(headerHeading) ? cmd.headerHeading() : headerHeading;
2930 synopsisHeading = empty(synopsisHeading) || "Usage: ".equals(synopsisHeading) ? cmd.synopsisHeading() : synopsisHeading;
2931 descriptionHeading = empty(descriptionHeading) ? cmd.descriptionHeading() : descriptionHeading;
2932 parameterListHeading = empty(parameterListHeading) ? cmd.parameterListHeading() : parameterListHeading;
2933 optionListHeading = empty(optionListHeading) ? cmd.optionListHeading() : optionListHeading;
2934 commandListHeading = empty(commandListHeading) || "Commands:%n".equals(commandListHeading) ? cmd.commandListHeading() : commandListHeading;
2935 footerHeading = empty(footerHeading) ? cmd.footerHeading() : footerHeading;
2936 }
2937 cls = cls.getSuperclass();
2938 }
2939 sortOptions = (sortOptions == null) ? true : sortOptions;
2940 abbreviateSynopsis = (abbreviateSynopsis == null) ? false : abbreviateSynopsis;
2941 requiredOptionMarker = (requiredOptionMarker == null) ? ' ' : requiredOptionMarker;
2942 showDefaultValues = (showDefaultValues == null) ? false : showDefaultValues;
2943 synopsisHeading = (synopsisHeading == null) ? "Usage: " : synopsisHeading;
2944 commandListHeading = (commandListHeading == null) ? "Commands:%n" : commandListHeading;
2945 separator = (separator == null) ? DEFAULT_SEPARATOR : separator;
2946 parameterLabelRenderer = createDefaultParamLabelRenderer(); // uses help separator
2947 Collections.sort(operands, new PositionalParametersSorter());
2948 positionalParametersFields = Collections.unmodifiableList(operands);
2949 optionFields = Collections.unmodifiableList(options);
2950 }
2951
2952 /** Registers all specified subcommands with this Help.
2953 * @param commands maps the command names to the associated CommandLine object
2954 * @return this Help instance (for method chaining)
2955 * @see CommandLine#getSubcommands()
2956 */
2957 public Help addAllSubcommands(Map<String, CommandLine> commands) {
2958 if (commands != null) {
2959 for (Map.Entry<String, CommandLine> entry : commands.entrySet()) {
2960 addSubcommand(entry.getKey(), entry.getValue().getCommand());
2961 }
2962 }
2963 return this;
2964 }
2965
2966 /** Registers the specified subcommand with this Help.
2967 * @param commandName the name of the subcommand to display in the usage message
2968 * @param command the annotated object to get more information from
2969 * @return this Help instance (for method chaining)
2970 */
2971 public Help addSubcommand(String commandName, Object command) {
2972 commands.put(commandName, new Help(command));
2973 return this;
2974 }
2975
2976 /** Returns a synopsis for the command without reserving space for the synopsis heading.
2977 * @return a synopsis
2978 * @see #abbreviatedSynopsis()
2979 * @see #detailedSynopsis(Comparator, boolean)
2980 * @deprecated use {@link #synopsis(int)} instead
2981 */
2982 public String synopsis() { return synopsis(0); }
2983
2984 /**
2985 * Returns a synopsis for the command, reserving the specified space for the synopsis heading.
2986 * @param synopsisHeadingLength the length of the synopsis heading that will be displayed on the same line
2987 * @return a synopsis
2988 * @see #abbreviatedSynopsis()
2989 * @see #detailedSynopsis(Comparator, boolean)
2990 * @see #synopsisHeading
2991 */
2992 public String synopsis(int synopsisHeadingLength) {
2993 if (!empty(customSynopsis)) { return customSynopsis(); }
2994 return abbreviateSynopsis ? abbreviatedSynopsis()
2995 : detailedSynopsis(synopsisHeadingLength, createShortOptionArityAndNameComparator(), true);
2996 }
2997
2998 /** Generates a generic synopsis like {@code <command name> [OPTIONS] [PARAM1 [PARAM2]...]}, omitting parts
2999 * that don't apply to the command (e.g., does not show [OPTIONS] if the command has no options).
3000 * @return a generic synopsis */
3001 public String abbreviatedSynopsis() {
3002 StringBuilder sb = new StringBuilder();
3003 if (!optionFields.isEmpty()) { // only show if annotated object actually has options
3004 sb.append(" [OPTIONS]");
3005 }
3006 // sb.append(" [--] "); // implied
3007 for (Field positionalParam : positionalParametersFields) {
3008 if (!positionalParam.getAnnotation(Parameters.class).hidden()) {
3009 sb.append(' ').append(parameterLabelRenderer.renderParameterLabel(positionalParam, ansi(), colorScheme.parameterStyles));
3010 }
3011 }
3012 return colorScheme.commandText(commandName).toString()
3013 + (sb.toString()) + System.getProperty("line.separator");
3014 }
3015 /** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of
3016 * showing optional options and parameters in square brackets ({@code [ ]}).
3017 * @param optionSort comparator to sort options or {@code null} if options should not be sorted
3018 * @param clusterBooleanOptions {@code true} if boolean short options should be clustered into a single string
3019 * @return a detailed synopsis
3020 * @deprecated use {@link #detailedSynopsis(int, Comparator, boolean)} instead. */
3021 public String detailedSynopsis(Comparator<Field> optionSort, boolean clusterBooleanOptions) {
3022 return detailedSynopsis(0, optionSort, clusterBooleanOptions);
3023 }
3024
3025 /** Generates a detailed synopsis message showing all options and parameters. Follows the unix convention of
3026 * showing optional options and parameters in square brackets ({@code [ ]}).
3027 * @param synopsisHeadingLength the length of the synopsis heading that will be displayed on the same line
3028 * @param optionSort comparator to sort options or {@code null} if options should not be sorted
3029 * @param clusterBooleanOptions {@code true} if boolean short options should be clustered into a single string
3030 * @return a detailed synopsis */
3031 public String detailedSynopsis(int synopsisHeadingLength, Comparator<Field> optionSort, boolean clusterBooleanOptions) {
3032 Text optionText = ansi().new Text(0);
3033 List<Field> fields = new ArrayList<Field>(optionFields); // iterate in declaration order
3034 if (optionSort != null) {
3035 Collections.sort(fields, optionSort);// iterate in specified sort order
3036 }
3037 if (clusterBooleanOptions) { // cluster all short boolean options into a single string
3038 List<Field> booleanOptions = new ArrayList<Field>();
3039 StringBuilder clusteredRequired = new StringBuilder("-");
3040 StringBuilder clusteredOptional = new StringBuilder("-");
3041 for (Field field : fields) {
3042 if (field.getType() == boolean.class || field.getType() == Boolean.class) {
3043 Option option = field.getAnnotation(Option.class);
3044 String shortestName = ShortestFirst.sort(option.names())[0];
3045 if (shortestName.length() == 2 && shortestName.startsWith("-")) {
3046 booleanOptions.add(field);
3047 if (option.required()) {
3048 clusteredRequired.append(shortestName.substring(1));
3049 } else {
3050 clusteredOptional.append(shortestName.substring(1));
3051 }
3052 }
3053 }
3054 }
3055 fields.removeAll(booleanOptions);
3056 if (clusteredRequired.length() > 1) { // initial length was 1
3057 optionText = optionText.append(" ").append(colorScheme.optionText(clusteredRequired.toString()));
3058 }
3059 if (clusteredOptional.length() > 1) { // initial length was 1
3060 optionText = optionText.append(" [").append(colorScheme.optionText(clusteredOptional.toString())).append("]");
3061 }
3062 }
3063 for (Field field : fields) {
3064 Option option = field.getAnnotation(Option.class);
3065 if (!option.hidden()) {
3066 if (option.required()) {
3067 optionText = appendOptionSynopsis(optionText, field, ShortestFirst.sort(option.names())[0], " ", "");
3068 if (isMultiValue(field)) {
3069 optionText = appendOptionSynopsis(optionText, field, ShortestFirst.sort(option.names())[0], " [", "]...");
3070 }
3071 } else {
3072 optionText = appendOptionSynopsis(optionText, field, ShortestFirst.sort(option.names())[0], " [", "]");
3073 if (isMultiValue(field)) {
3074 optionText = optionText.append("...");
3075 }
3076 }
3077 }
3078 }
3079 for (Field positionalParam : positionalParametersFields) {
3080 if (!positionalParam.getAnnotation(Parameters.class).hidden()) {
3081 optionText = optionText.append(" ");
3082 Text label = parameterLabelRenderer.renderParameterLabel(positionalParam, colorScheme.ansi(), colorScheme.parameterStyles);
3083 optionText = optionText.append(label);
3084 }
3085 }
3086 // Fix for #142: first line of synopsis overshoots max. characters
3087 int firstColumnLength = commandName.length() + synopsisHeadingLength;
3088
3089 // synopsis heading ("Usage: ") may be on the same line, so adjust column width
3090 TextTable textTable = new TextTable(ansi(), firstColumnLength, usageHelpWidth - firstColumnLength);
3091 textTable.indentWrappedLines = 1; // don't worry about first line: options (2nd column) always start with a space
3092
3093 // right-adjust the command name by length of synopsis heading
3094 Text PADDING = Ansi.OFF.new Text(stringOf('X', synopsisHeadingLength));
3095 textTable.addRowValues(new Text[] {PADDING.append(colorScheme.commandText(commandName)), optionText});
3096 return textTable.toString().substring(synopsisHeadingLength); // cut off leading synopsis heading spaces
3097 }
3098
3099 private Text appendOptionSynopsis(Text optionText, Field field, String optionName, String prefix, String suffix) {
3100 Text optionParamText = parameterLabelRenderer.renderParameterLabel(field, colorScheme.ansi(), colorScheme.optionParamStyles);
3101 return optionText.append(prefix)
3102 .append(colorScheme.optionText(optionName))
3103 .append(optionParamText)
3104 .append(suffix);
3105 }
3106
3107 /** Returns the number of characters the synopsis heading will take on the same line as the synopsis.
3108 * @return the number of characters the synopsis heading will take on the same line as the synopsis.
3109 * @see #detailedSynopsis(int, Comparator, boolean)
3110 */
3111 public int synopsisHeadingLength() {
3112 String[] lines = Ansi.OFF.new Text(synopsisHeading).toString().split("\\r?\\n|\\r|%n", -1);
3113 return lines[lines.length - 1].length();
3114 }
3115 /**
3116 * <p>Returns a description of the {@linkplain Option options} supported by the application.
3117 * This implementation {@linkplain #createShortOptionNameComparator() sorts options alphabetically}, and shows
3118 * only the {@linkplain Option#hidden() non-hidden} options in a {@linkplain TextTable tabular format}
3119 * using the {@linkplain #createDefaultOptionRenderer() default renderer} and {@linkplain Layout default layout}.</p>
3120 * @return the fully formatted option list
3121 * @see #optionList(Layout, Comparator, IParamLabelRenderer)
3122 */
3123 public String optionList() {
3124 Comparator<Field> sortOrder = sortOptions == null || sortOptions.booleanValue()
3125 ? createShortOptionNameComparator()
3126 : null;
3127 return optionList(createDefaultLayout(), sortOrder, parameterLabelRenderer);
3128 }
3129
3130 /** Sorts all {@code Options} with the specified {@code comparator} (if the comparator is non-{@code null}),
3131 * then {@linkplain Layout#addOption(Field, CommandLine.Help.IParamLabelRenderer) adds} all non-hidden options to the
3132 * specified TextTable and returns the result of TextTable.toString().
3133 * @param layout responsible for rendering the option list
3134 * @param optionSort determines in what order {@code Options} should be listed. Declared order if {@code null}
3135 * @param valueLabelRenderer used for options with a parameter
3136 * @return the fully formatted option list
3137 */
3138 public String optionList(Layout layout, Comparator<Field> optionSort, IParamLabelRenderer valueLabelRenderer) {
3139 List<Field> fields = new ArrayList<Field>(optionFields); // options are stored in order of declaration
3140 if (optionSort != null) {
3141 Collections.sort(fields, optionSort); // default: sort options ABC
3142 }
3143 layout.addOptions(fields, valueLabelRenderer);
3144 return layout.toString();
3145 }
3146
3147 /**
3148 * Returns the section of the usage help message that lists the parameters with their descriptions.
3149 * @return the section of the usage help message that lists the parameters
3150 */
3151 public String parameterList() {
3152 return parameterList(createDefaultLayout(), parameterLabelRenderer);
3153 }
3154 /**
3155 * Returns the section of the usage help message that lists the parameters with their descriptions.
3156 * @param layout the layout to use
3157 * @param paramLabelRenderer for rendering parameter names
3158 * @return the section of the usage help message that lists the parameters
3159 */
3160 public String parameterList(Layout layout, IParamLabelRenderer paramLabelRenderer) {
3161 layout.addPositionalParameters(positionalParametersFields, paramLabelRenderer);
3162 return layout.toString();
3163 }
3164
3165 private static String heading(Ansi ansi, String values, Object... params) {
3166 StringBuilder sb = join(ansi, new String[] {values}, new StringBuilder(), params);
3167 String result = sb.toString();
3168 result = result.endsWith(System.getProperty("line.separator"))
3169 ? result.substring(0, result.length() - System.getProperty("line.separator").length()) : result;
3170 return result + new String(spaces(countTrailingSpaces(values)));
3171 }
3172 private static char[] spaces(int length) { char[] result = new char[length]; Arrays.fill(result, ' '); return result; }
3173 private static int countTrailingSpaces(String str) {
3174 if (str == null) {return 0;}
3175 int trailingSpaces = 0;
3176 for (int i = str.length() - 1; i >= 0 && str.charAt(i) == ' '; i--) { trailingSpaces++; }
3177 return trailingSpaces;
3178 }
3179
3180 /** Formats each of the specified values and appends it to the specified StringBuilder.
3181 * @param ansi whether the result should contain ANSI escape codes or not
3182 * @param values the values to format and append to the StringBuilder
3183 * @param sb the StringBuilder to collect the formatted strings
3184 * @param params the parameters to pass to the format method when formatting each value
3185 * @return the specified StringBuilder */
3186 public static StringBuilder join(Ansi ansi, String[] values, StringBuilder sb, Object... params) {
3187 if (values != null) {
3188 TextTable table = new TextTable(ansi, usageHelpWidth);
3189 table.indentWrappedLines = 0;
3190 for (String summaryLine : values) {
3191 Text[] lines = ansi.new Text(format(summaryLine, params)).splitLines();
3192 for (Text line : lines) { table.addRowValues(line); }
3193 }
3194 table.toString(sb);
3195 }
3196 return sb;
3197 }
3198 private static String format(String formatString, Object... params) {
3199 return formatString == null ? "" : String.format(formatString, params);
3200 }
3201 /** Returns command custom synopsis as a string. A custom synopsis can be zero or more lines, and can be
3202 * specified declaratively with the {@link Command#customSynopsis()} annotation attribute or programmatically
3203 * by setting the Help instance's {@link Help#customSynopsis} field.
3204 * @param params Arguments referenced by the format specifiers in the synopsis strings
3205 * @return the custom synopsis lines combined into a single String (which may be empty)
3206 */
3207 public String customSynopsis(Object... params) {
3208 return join(ansi(), customSynopsis, new StringBuilder(), params).toString();
3209 }
3210 /** Returns command description text as a string. Description text can be zero or more lines, and can be specified
3211 * declaratively with the {@link Command#description()} annotation attribute or programmatically by
3212 * setting the Help instance's {@link Help#description} field.
3213 * @param params Arguments referenced by the format specifiers in the description strings
3214 * @return the description lines combined into a single String (which may be empty)
3215 */
3216 public String description(Object... params) {
3217 return join(ansi(), description, new StringBuilder(), params).toString();
3218 }
3219 /** Returns the command header text as a string. Header text can be zero or more lines, and can be specified
3220 * declaratively with the {@link Command#header()} annotation attribute or programmatically by
3221 * setting the Help instance's {@link Help#header} field.
3222 * @param params Arguments referenced by the format specifiers in the header strings
3223 * @return the header lines combined into a single String (which may be empty)
3224 */
3225 public String header(Object... params) {
3226 return join(ansi(), header, new StringBuilder(), params).toString();
3227 }
3228 /** Returns command footer text as a string. Footer text can be zero or more lines, and can be specified
3229 * declaratively with the {@link Command#footer()} annotation attribute or programmatically by
3230 * setting the Help instance's {@link Help#footer} field.
3231 * @param params Arguments referenced by the format specifiers in the footer strings
3232 * @return the footer lines combined into a single String (which may be empty)
3233 */
3234 public String footer(Object... params) {
3235 return join(ansi(), footer, new StringBuilder(), params).toString();
3236 }
3237
3238 /** Returns the text displayed before the header text; the result of {@code String.format(headerHeading, params)}.
3239 * @param params the parameters to use to format the header heading
3240 * @return the formatted header heading */
3241 public String headerHeading(Object... params) {
3242 return heading(ansi(), headerHeading, params);
3243 }
3244
3245 /** Returns the text displayed before the synopsis text; the result of {@code String.format(synopsisHeading, params)}.
3246 * @param params the parameters to use to format the synopsis heading
3247 * @return the formatted synopsis heading */
3248 public String synopsisHeading(Object... params) {
3249 return heading(ansi(), synopsisHeading, params);
3250 }
3251
3252 /** Returns the text displayed before the description text; an empty string if there is no description,
3253 * otherwise the result of {@code String.format(descriptionHeading, params)}.
3254 * @param params the parameters to use to format the description heading
3255 * @return the formatted description heading */
3256 public String descriptionHeading(Object... params) {
3257 return empty(descriptionHeading) ? "" : heading(ansi(), descriptionHeading, params);
3258 }
3259
3260 /** Returns the text displayed before the positional parameter list; an empty string if there are no positional
3261 * parameters, otherwise the result of {@code String.format(parameterListHeading, params)}.
3262 * @param params the parameters to use to format the parameter list heading
3263 * @return the formatted parameter list heading */
3264 public String parameterListHeading(Object... params) {
3265 return positionalParametersFields.isEmpty() ? "" : heading(ansi(), parameterListHeading, params);
3266 }
3267
3268 /** Returns the text displayed before the option list; an empty string if there are no options,
3269 * otherwise the result of {@code String.format(optionListHeading, params)}.
3270 * @param params the parameters to use to format the option list heading
3271 * @return the formatted option list heading */
3272 public String optionListHeading(Object... params) {
3273 return optionFields.isEmpty() ? "" : heading(ansi(), optionListHeading, params);
3274 }
3275
3276 /** Returns the text displayed before the command list; an empty string if there are no commands,
3277 * otherwise the result of {@code String.format(commandListHeading, params)}.
3278 * @param params the parameters to use to format the command list heading
3279 * @return the formatted command list heading */
3280 public String commandListHeading(Object... params) {
3281 return commands.isEmpty() ? "" : heading(ansi(), commandListHeading, params);
3282 }
3283
3284 /** Returns the text displayed before the footer text; the result of {@code String.format(footerHeading, params)}.
3285 * @param params the parameters to use to format the footer heading
3286 * @return the formatted footer heading */
3287 public String footerHeading(Object... params) {
3288 return heading(ansi(), footerHeading, params);
3289 }
3290 /** Returns a 2-column list with command names and the first line of their header or (if absent) description.
3291 * @return a usage help section describing the added commands */
3292 public String commandList() {
3293 if (commands.isEmpty()) { return ""; }
3294 int commandLength = maxLength(commands.keySet());
3295 Help.TextTable textTable = new Help.TextTable(ansi(),
3296 new Help.Column(commandLength + 2, 2, Help.Column.Overflow.SPAN),
3297 new Help.Column(usageHelpWidth - (commandLength + 2), 2, Help.Column.Overflow.WRAP));
3298
3299 for (Map.Entry<String, Help> entry : commands.entrySet()) {
3300 Help command = entry.getValue();
3301 String header = command.header != null && command.header.length > 0 ? command.header[0]
3302 : (command.description != null && command.description.length > 0 ? command.description[0] : "");
3303 textTable.addRowValues(colorScheme.commandText(entry.getKey()), ansi().new Text(header));
3304 }
3305 return textTable.toString();
3306 }
3307 private static int maxLength(Collection<String> any) {
3308 List<String> strings = new ArrayList<String>(any);
3309 Collections.sort(strings, Collections.reverseOrder(Help.shortestFirst()));
3310 return strings.get(0).length();
3311 }
3312 private static String join(String[] names, int offset, int length, String separator) {
3313 if (names == null) { return ""; }
3314 StringBuilder result = new StringBuilder();
3315 for (int i = offset; i < offset + length; i++) {
3316 result.append((i > offset) ? separator : "").append(names[i]);
3317 }
3318 return result.toString();
3319 }
3320 private static String stringOf(char chr, int length) {
3321 char[] buff = new char[length];
3322 Arrays.fill(buff, chr);
3323 return new String(buff);
3324 }
3325
3326 /** Returns a {@code Layout} instance configured with the user preferences captured in this Help instance.
3327 * @return a Layout */
3328 public Layout createDefaultLayout() {
3329 return new Layout(colorScheme, new TextTable(colorScheme.ansi()), createDefaultOptionRenderer(), createDefaultParameterRenderer());
3330 }
3331 /** Returns a new default OptionRenderer which converts {@link Option Options} to five columns of text to match
3332 * the default {@linkplain TextTable TextTable} column layout. The first row of values looks like this:
3333 * <ol>
3334 * <li>the required option marker</li>
3335 * <li>2-character short option name (or empty string if no short option exists)</li>
3336 * <li>comma separator (only if both short option and long option exist, empty string otherwise)</li>
3337 * <li>comma-separated string with long option name(s)</li>
3338 * <li>first element of the {@link Option#description()} array</li>
3339 * </ol>
3340 * <p>Following this, there will be one row for each of the remaining elements of the {@link
3341 * Option#description()} array, and these rows look like {@code {"", "", "", "", option.description()[i]}}.</p>
3342 * <p>If configured, this option renderer adds an additional row to display the default field value.</p>
3343 * @return a new default OptionRenderer
3344 */
3345 public IOptionRenderer createDefaultOptionRenderer() {
3346 DefaultOptionRenderer result = new DefaultOptionRenderer();
3347 result.requiredMarker = String.valueOf(requiredOptionMarker);
3348 if (showDefaultValues != null && showDefaultValues.booleanValue()) {
3349 result.command = this.command;
3350 }
3351 return result;
3352 }
3353 /** Returns a new minimal OptionRenderer which converts {@link Option Options} to a single row with two columns
3354 * of text: an option name and a description. If multiple names or descriptions exist, the first value is used.
3355 * @return a new minimal OptionRenderer */
3356 public static IOptionRenderer createMinimalOptionRenderer() {
3357 return new MinimalOptionRenderer();
3358 }
3359
3360 /** Returns a new default ParameterRenderer which converts {@link Parameters Parameters} to four columns of
3361 * text to match the default {@linkplain TextTable TextTable} column layout. The first row of values looks like this:
3362 * <ol>
3363 * <li>empty string </li>
3364 * <li>empty string </li>
3365 * <li>parameter(s) label as rendered by the {@link IParamLabelRenderer}</li>
3366 * <li>first element of the {@link Parameters#description()} array</li>
3367 * </ol>
3368 * <p>Following this, there will be one row for each of the remaining elements of the {@link
3369 * Parameters#description()} array, and these rows look like {@code {"", "", "", param.description()[i]}}.</p>
3370 * <p>If configured, this parameter renderer adds an additional row to display the default field value.</p>
3371 * @return a new default ParameterRenderer
3372 */
3373 public IParameterRenderer createDefaultParameterRenderer() {
3374 DefaultParameterRenderer result = new DefaultParameterRenderer();
3375 result.requiredMarker = String.valueOf(requiredOptionMarker);
3376 return result;
3377 }
3378 /** Returns a new minimal ParameterRenderer which converts {@link Parameters Parameters} to a single row with
3379 * two columns of text: an option name and a description. If multiple descriptions exist, the first value is used.
3380 * @return a new minimal ParameterRenderer */
3381 public static IParameterRenderer createMinimalParameterRenderer() {
3382 return new MinimalParameterRenderer();
3383 }
3384
3385 /** Returns a value renderer that returns the {@code paramLabel} if defined or the field name otherwise.
3386 * @return a new minimal ParamLabelRenderer */
3387 public static IParamLabelRenderer createMinimalParamLabelRenderer() {
3388 return new IParamLabelRenderer() {
3389 public Text renderParameterLabel(Field field, Ansi ansi, List<IStyle> styles) {
3390 String text = DefaultParamLabelRenderer.renderParameterName(field);
3391 return ansi.apply(text, styles);
3392 }
3393 public String separator() { return ""; }
3394 };
3395 }
3396 /** Returns a new default value renderer that separates option parameters from their {@linkplain Option
3397 * options} with the specified separator string, surrounds optional parameters with {@code '['} and {@code ']'}
3398 * characters and uses ellipses ("...") to indicate that any number of a parameter are allowed.
3399 * @return a new default ParamLabelRenderer
3400 */
3401 public IParamLabelRenderer createDefaultParamLabelRenderer() {
3402 return new DefaultParamLabelRenderer(separator);
3403 }
3404 /** Sorts Fields annotated with {@code Option} by their option name in case-insensitive alphabetic order. If an
3405 * Option has multiple names, the shortest name is used for the sorting. Help options follow non-help options.
3406 * @return a comparator that sorts fields by their option name in case-insensitive alphabetic order */
3407 public static Comparator<Field> createShortOptionNameComparator() {
3408 return new SortByShortestOptionNameAlphabetically();
3409 }
3410 /** Sorts Fields annotated with {@code Option} by their option {@linkplain Range#max max arity} first, by
3411 * {@linkplain Range#min min arity} next, and by {@linkplain #createShortOptionNameComparator() option name} last.
3412 * @return a comparator that sorts fields by arity first, then their option name */
3413 public static Comparator<Field> createShortOptionArityAndNameComparator() {
3414 return new SortByOptionArityAndNameAlphabetically();
3415 }
3416 /** Sorts short strings before longer strings.
3417 * @return a comparators that sorts short strings before longer strings */
3418 public static Comparator<String> shortestFirst() {
3419 return new ShortestFirst();
3420 }
3421
3422 /** Returns whether ANSI escape codes are enabled or not.
3423 * @return whether ANSI escape codes are enabled or not
3424 */
3425 public Ansi ansi() {
3426 return colorScheme.ansi;
3427 }
3428
3429 /** When customizing online help for {@link Option Option} details, a custom {@code IOptionRenderer} can be
3430 * used to create textual representation of an Option in a tabular format: one or more rows, each containing
3431 * one or more columns. The {@link Layout Layout} is responsible for placing these text values in the
3432 * {@link TextTable TextTable}. */
3433 public interface IOptionRenderer {
3434 /**
3435 * Returns a text representation of the specified Option and the Field that captures the option value.
3436 * @param option the command line option to show online usage help for
3437 * @param field the field that will hold the value for the command line option
3438 * @param parameterLabelRenderer responsible for rendering option parameters to text
3439 * @param scheme color scheme for applying ansi color styles to options and option parameters
3440 * @return a 2-dimensional array of text values: one or more rows, each containing one or more columns
3441 */
3442 Text[][] render(Option option, Field field, IParamLabelRenderer parameterLabelRenderer, ColorScheme scheme);
3443 }
3444 /** The DefaultOptionRenderer converts {@link Option Options} to five columns of text to match the default
3445 * {@linkplain TextTable TextTable} column layout. The first row of values looks like this:
3446 * <ol>
3447 * <li>the required option marker (if the option is required)</li>
3448 * <li>2-character short option name (or empty string if no short option exists)</li>
3449 * <li>comma separator (only if both short option and long option exist, empty string otherwise)</li>
3450 * <li>comma-separated string with long option name(s)</li>
3451 * <li>first element of the {@link Option#description()} array</li>
3452 * </ol>
3453 * <p>Following this, there will be one row for each of the remaining elements of the {@link
3454 * Option#description()} array, and these rows look like {@code {"", "", "", option.description()[i]}}.</p>
3455 */
3456 static class DefaultOptionRenderer implements IOptionRenderer {
3457 public String requiredMarker = " ";
3458 public Object command;
3459 private String sep;
3460 private boolean showDefault;
3461 public Text[][] render(Option option, Field field, IParamLabelRenderer paramLabelRenderer, ColorScheme scheme) {
3462 String[] names = ShortestFirst.sort(option.names());
3463 int shortOptionCount = names[0].length() == 2 ? 1 : 0;
3464 String shortOption = shortOptionCount > 0 ? names[0] : "";
3465 sep = shortOptionCount > 0 && names.length > 1 ? "," : "";
3466
3467 String longOption = join(names, shortOptionCount, names.length - shortOptionCount, ", ");
3468 Text longOptionText = createLongOptionText(field, paramLabelRenderer, scheme, longOption);
3469
3470 showDefault = command != null && !option.help() && !isBoolean(field.getType());
3471 Object defaultValue = createDefaultValue(field);
3472
3473 String requiredOption = option.required() ? requiredMarker : "";
3474 return renderDescriptionLines(option, scheme, requiredOption, shortOption, longOptionText, defaultValue);
3475 }
3476
3477 private Object createDefaultValue(Field field) {
3478 Object defaultValue = null;
3479 try {
3480 defaultValue = field.get(command);
3481 if (defaultValue == null) { showDefault = false; } // #201 don't show null default values
3482 else if (field.getType().isArray()) {
3483 StringBuilder sb = new StringBuilder();
3484 for (int i = 0; i < Array.getLength(defaultValue); i++) {
3485 sb.append(i > 0 ? ", " : "").append(Array.get(defaultValue, i));
3486 }
3487 defaultValue = sb.insert(0, "[").append("]").toString();
3488 }
3489 } catch (Exception ex) {
3490 showDefault = false;
3491 }
3492 return defaultValue;
3493 }
3494
3495 private Text createLongOptionText(Field field, IParamLabelRenderer renderer, ColorScheme scheme, String longOption) {
3496 Text paramLabelText = renderer.renderParameterLabel(field, scheme.ansi(), scheme.optionParamStyles);
3497
3498 // if no long option, fill in the space between the short option name and the param label value
3499 if (paramLabelText.length > 0 && longOption.length() == 0) {
3500 sep = renderer.separator();
3501 // #181 paramLabelText may be =LABEL or [=LABEL...]
3502 int sepStart = paramLabelText.plainString().indexOf(sep);
3503 Text prefix = paramLabelText.substring(0, sepStart);
3504 paramLabelText = prefix.append(paramLabelText.substring(sepStart + sep.length()));
3505 }
3506 Text longOptionText = scheme.optionText(longOption);
3507 longOptionText = longOptionText.append(paramLabelText);
3508 return longOptionText;
3509 }
3510
3511 private Text[][] renderDescriptionLines(Option option,
3512 ColorScheme scheme,
3513 String requiredOption,
3514 String shortOption,
3515 Text longOptionText,
3516 Object defaultValue) {
3517 Text EMPTY = Ansi.EMPTY_TEXT;
3518 List<Text[]> result = new ArrayList<Text[]>();
3519 Text[] descriptionFirstLines = scheme.ansi().new Text(str(option.description(), 0)).splitLines();
3520 if (descriptionFirstLines.length == 0) {
3521 if (showDefault) {
3522 descriptionFirstLines = new Text[]{scheme.ansi().new Text(" Default: " + defaultValue)};
3523 showDefault = false; // don't show the default value twice
3524 } else {
3525 descriptionFirstLines = new Text[]{ EMPTY };
3526 }
3527 }
3528 result.add(new Text[] { scheme.optionText(requiredOption), scheme.optionText(shortOption),
3529 scheme.ansi().new Text(sep), longOptionText, descriptionFirstLines[0] });
3530 for (int i = 1; i < descriptionFirstLines.length; i++) {
3531 result.add(new Text[] { EMPTY, EMPTY, EMPTY, EMPTY, descriptionFirstLines[i] });
3532 }
3533 for (int i = 1; i < option.description().length; i++) {
3534 Text[] descriptionNextLines = scheme.ansi().new Text(option.description()[i]).splitLines();
3535 for (Text line : descriptionNextLines) {
3536 result.add(new Text[] { EMPTY, EMPTY, EMPTY, EMPTY, line });
3537 }
3538 }
3539 if (showDefault) {
3540 result.add(new Text[] { EMPTY, EMPTY, EMPTY, EMPTY, scheme.ansi().new Text(" Default: " + defaultValue) });
3541 }
3542 return result.toArray(new Text[result.size()][]);
3543 }
3544 }
3545 /** The MinimalOptionRenderer converts {@link Option Options} to a single row with two columns of text: an
3546 * option name and a description. If multiple names or description lines exist, the first value is used. */
3547 static class MinimalOptionRenderer implements IOptionRenderer {
3548 public Text[][] render(Option option, Field field, IParamLabelRenderer parameterLabelRenderer, ColorScheme scheme) {
3549 Text optionText = scheme.optionText(option.names()[0]);
3550 Text paramLabelText = parameterLabelRenderer.renderParameterLabel(field, scheme.ansi(), scheme.optionParamStyles);
3551 optionText = optionText.append(paramLabelText);
3552 return new Text[][] {{ optionText,
3553 scheme.ansi().new Text(option.description().length == 0 ? "" : option.description()[0]) }};
3554 }
3555 }
3556 /** The MinimalParameterRenderer converts {@link Parameters Parameters} to a single row with two columns of
3557 * text: the parameters label and a description. If multiple description lines exist, the first value is used. */
3558 static class MinimalParameterRenderer implements IParameterRenderer {
3559 public Text[][] render(Parameters param, Field field, IParamLabelRenderer parameterLabelRenderer, ColorScheme scheme) {
3560 return new Text[][] {{ parameterLabelRenderer.renderParameterLabel(field, scheme.ansi(), scheme.parameterStyles),
3561 scheme.ansi().new Text(param.description().length == 0 ? "" : param.description()[0]) }};
3562 }
3563 }
3564 /** When customizing online help for {@link Parameters Parameters} details, a custom {@code IParameterRenderer}
3565 * can be used to create textual representation of a Parameters field in a tabular format: one or more rows,
3566 * each containing one or more columns. The {@link Layout Layout} is responsible for placing these text
3567 * values in the {@link TextTable TextTable}. */
3568 public interface IParameterRenderer {
3569 /**
3570 * Returns a text representation of the specified Parameters and the Field that captures the parameter values.
3571 * @param parameters the command line parameters to show online usage help for
3572 * @param field the field that will hold the value for the command line parameters
3573 * @param parameterLabelRenderer responsible for rendering parameter labels to text
3574 * @param scheme color scheme for applying ansi color styles to positional parameters
3575 * @return a 2-dimensional array of text values: one or more rows, each containing one or more columns
3576 */
3577 Text[][] render(Parameters parameters, Field field, IParamLabelRenderer parameterLabelRenderer, ColorScheme scheme);
3578 }
3579 /** The DefaultParameterRenderer converts {@link Parameters Parameters} to five columns of text to match the
3580 * default {@linkplain TextTable TextTable} column layout. The first row of values looks like this:
3581 * <ol>
3582 * <li>the required option marker (if the parameter's arity is to have at least one value)</li>
3583 * <li>empty string </li>
3584 * <li>empty string </li>
3585 * <li>parameter(s) label as rendered by the {@link IParamLabelRenderer}</li>
3586 * <li>first element of the {@link Parameters#description()} array</li>
3587 * </ol>
3588 * <p>Following this, there will be one row for each of the remaining elements of the {@link
3589 * Parameters#description()} array, and these rows look like {@code {"", "", "", param.description()[i]}}.</p>
3590 */
3591 static class DefaultParameterRenderer implements IParameterRenderer {
3592 public String requiredMarker = " ";
3593 public Text[][] render(Parameters params, Field field, IParamLabelRenderer paramLabelRenderer, ColorScheme scheme) {
3594 Text label = paramLabelRenderer.renderParameterLabel(field, scheme.ansi(), scheme.parameterStyles);
3595 Text requiredParameter = scheme.parameterText(Range.parameterArity(field).min > 0 ? requiredMarker : "");
3596
3597 Text EMPTY = Ansi.EMPTY_TEXT;
3598 List<Text[]> result = new ArrayList<Text[]>();
3599 Text[] descriptionFirstLines = scheme.ansi().new Text(str(params.description(), 0)).splitLines();
3600 if (descriptionFirstLines.length == 0) { descriptionFirstLines = new Text[]{ EMPTY }; }
3601 result.add(new Text[] { requiredParameter, EMPTY, EMPTY, label, descriptionFirstLines[0] });
3602 for (int i = 1; i < descriptionFirstLines.length; i++) {
3603 result.add(new Text[] { EMPTY, EMPTY, EMPTY, EMPTY, descriptionFirstLines[i] });
3604 }
3605 for (int i = 1; i < params.description().length; i++) {
3606 Text[] descriptionNextLines = scheme.ansi().new Text(params.description()[i]).splitLines();
3607 for (Text line : descriptionNextLines) {
3608 result.add(new Text[] { EMPTY, EMPTY, EMPTY, EMPTY, line });
3609 }
3610 }
3611 return result.toArray(new Text[result.size()][]);
3612 }
3613 }
3614 /** When customizing online usage help for an option parameter or a positional parameter, a custom
3615 * {@code IParamLabelRenderer} can be used to render the parameter name or label to a String. */
3616 public interface IParamLabelRenderer {
3617
3618 /** Returns a text rendering of the Option parameter or positional parameter; returns an empty string
3619 * {@code ""} if the option is a boolean and does not take a parameter.
3620 * @param field the annotated field with a parameter label
3621 * @param ansi determines whether ANSI escape codes should be emitted or not
3622 * @param styles the styles to apply to the parameter label
3623 * @return a text rendering of the Option parameter or positional parameter */
3624 Text renderParameterLabel(Field field, Ansi ansi, List<IStyle> styles);
3625
3626 /** Returns the separator between option name and param label.
3627 * @return the separator between option name and param label */
3628 String separator();
3629 }
3630 /**
3631 * DefaultParamLabelRenderer separates option parameters from their {@linkplain Option options} with a
3632 * {@linkplain DefaultParamLabelRenderer#separator separator} string, surrounds optional values
3633 * with {@code '['} and {@code ']'} characters and uses ellipses ("...") to indicate that any number of
3634 * values is allowed for options or parameters with variable arity.
3635 */
3636 static class DefaultParamLabelRenderer implements IParamLabelRenderer {
3637 /** The string to use to separate option parameters from their options. */
3638 public final String separator;
3639 /** Constructs a new DefaultParamLabelRenderer with the specified separator string. */
3640 public DefaultParamLabelRenderer(String separator) {
3641 this.separator = Assert.notNull(separator, "separator");
3642 }
3643 public String separator() { return separator; }
3644 public Text renderParameterLabel(Field field, Ansi ansi, List<IStyle> styles) {
3645 boolean isOptionParameter = field.isAnnotationPresent(Option.class);
3646 Range arity = isOptionParameter ? Range.optionArity(field) : Range.parameterCapacity(field);
3647 String split = isOptionParameter ? field.getAnnotation(Option.class).split() : field.getAnnotation(Parameters.class).split();
3648 Text result = ansi.new Text("");
3649 String sep = isOptionParameter ? separator : "";
3650 Text paramName = ansi.apply(renderParameterName(field), styles);
3651 if (!empty(split)) { paramName = paramName.append("[" + split).append(paramName).append("]..."); } // #194
3652 for (int i = 0; i < arity.min; i++) {
3653 result = result.append(sep).append(paramName);
3654 sep = " ";
3655 }
3656 if (arity.isVariable) {
3657 if (result.length == 0) { // arity="*" or arity="0..*"
3658 result = result.append(sep + "[").append(paramName).append("]...");
3659 } else if (!result.plainString().endsWith("...")) { // split param may already end with "..."
3660 result = result.append("...");
3661 }
3662 } else {
3663 sep = result.length == 0 ? (isOptionParameter ? separator : "") : " ";
3664 for (int i = arity.min; i < arity.max; i++) {
3665 if (sep.trim().length() == 0) {
3666 result = result.append(sep + "[").append(paramName);
3667 } else {
3668 result = result.append("[" + sep).append(paramName);
3669 }
3670 sep = " ";
3671 }
3672 for (int i = arity.min; i < arity.max; i++) { result = result.append("]"); }
3673 }
3674 return result;
3675 }
3676 private static String renderParameterName(Field field) {
3677 String result = null;
3678 if (field.isAnnotationPresent(Option.class)) {
3679 result = field.getAnnotation(Option.class).paramLabel();
3680 } else if (field.isAnnotationPresent(Parameters.class)) {
3681 result = field.getAnnotation(Parameters.class).paramLabel();
3682 }
3683 if (result != null && result.trim().length() > 0) {
3684 return result.trim();
3685 }
3686 String name = field.getName();
3687 if (Map.class.isAssignableFrom(field.getType())) { // #195 better param labels for map fields
3688 Class<?>[] paramTypes = getTypeAttribute(field);
3689 if (paramTypes.length < 2 || paramTypes[0] == null || paramTypes[1] == null) {
3690 name = "String=String";
3691 } else { name = paramTypes[0].getSimpleName() + "=" + paramTypes[1].getSimpleName(); }
3692 }
3693 return "<" + name + ">";
3694 }
3695 }
3696 /** Use a Layout to format usage help text for options and parameters in tabular format.
3697 * <p>Delegates to the renderers to create {@link Text} values for the annotated fields, and uses a
3698 * {@link TextTable} to display these values in tabular format. Layout is responsible for deciding which values
3699 * to display where in the table. By default, Layout shows one option or parameter per table row.</p>
3700 * <p>Customize by overriding the {@link #layout(Field, CommandLine.Help.Ansi.Text[][])} method.</p>
3701 * @see IOptionRenderer rendering options to text
3702 * @see IParameterRenderer rendering parameters to text
3703 * @see TextTable showing values in a tabular format
3704 */
3705 public static class Layout {
3706 protected final ColorScheme colorScheme;
3707 protected final TextTable table;
3708 protected IOptionRenderer optionRenderer;
3709 protected IParameterRenderer parameterRenderer;
3710
3711 /** Constructs a Layout with the specified color scheme, a new default TextTable, the
3712 * {@linkplain Help#createDefaultOptionRenderer() default option renderer}, and the
3713 * {@linkplain Help#createDefaultParameterRenderer() default parameter renderer}.
3714 * @param colorScheme the color scheme to use for common, auto-generated parts of the usage help message */
3715 public Layout(ColorScheme colorScheme) { this(colorScheme, new TextTable(colorScheme.ansi())); }
3716
3717 /** Constructs a Layout with the specified color scheme, the specified TextTable, the
3718 * {@linkplain Help#createDefaultOptionRenderer() default option renderer}, and the
3719 * {@linkplain Help#createDefaultParameterRenderer() default parameter renderer}.
3720 * @param colorScheme the color scheme to use for common, auto-generated parts of the usage help message
3721 * @param textTable the TextTable to lay out parts of the usage help message in tabular format */
3722 public Layout(ColorScheme colorScheme, TextTable textTable) {
3723 this(colorScheme, textTable, new DefaultOptionRenderer(), new DefaultParameterRenderer());
3724 }
3725 /** Constructs a Layout with the specified color scheme, the specified TextTable, the
3726 * specified option renderer and the specified parameter renderer.
3727 * @param colorScheme the color scheme to use for common, auto-generated parts of the usage help message
3728 * @param optionRenderer the object responsible for rendering Options to Text
3729 * @param parameterRenderer the object responsible for rendering Parameters to Text
3730 * @param textTable the TextTable to lay out parts of the usage help message in tabular format */
3731 public Layout(ColorScheme colorScheme, TextTable textTable, IOptionRenderer optionRenderer, IParameterRenderer parameterRenderer) {
3732 this.colorScheme = Assert.notNull(colorScheme, "colorScheme");
3733 this.table = Assert.notNull(textTable, "textTable");
3734 this.optionRenderer = Assert.notNull(optionRenderer, "optionRenderer");
3735 this.parameterRenderer = Assert.notNull(parameterRenderer, "parameterRenderer");
3736 }
3737 /**
3738 * Copies the specified text values into the correct cells in the {@link TextTable}. This implementation
3739 * delegates to {@link TextTable#addRowValues(CommandLine.Help.Ansi.Text...)} for each row of values.
3740 * <p>Subclasses may override.</p>
3741 * @param field the field annotated with the specified Option or Parameters
3742 * @param cellValues the text values representing the Option/Parameters, to be displayed in tabular form
3743 */
3744 public void layout(Field field, Text[][] cellValues) {
3745 for (Text[] oneRow : cellValues) {
3746 table.addRowValues(oneRow);
3747 }
3748 }
3749 /** Calls {@link #addOption(Field, CommandLine.Help.IParamLabelRenderer)} for all non-hidden Options in the list.
3750 * @param fields fields annotated with {@link Option} to add usage descriptions for
3751 * @param paramLabelRenderer object that knows how to render option parameters */
3752 public void addOptions(List<Field> fields, IParamLabelRenderer paramLabelRenderer) {
3753 for (Field field : fields) {
3754 Option option = field.getAnnotation(Option.class);
3755 if (!option.hidden()) {
3756 addOption(field, paramLabelRenderer);
3757 }
3758 }
3759 }
3760 /**
3761 * Delegates to the {@link #optionRenderer option renderer} of this layout to obtain
3762 * text values for the specified {@link Option}, and then calls the {@link #layout(Field, CommandLine.Help.Ansi.Text[][])}
3763 * method to write these text values into the correct cells in the TextTable.
3764 * @param field the field annotated with the specified Option
3765 * @param paramLabelRenderer knows how to render option parameters
3766 */
3767 public void addOption(Field field, IParamLabelRenderer paramLabelRenderer) {
3768 Option option = field.getAnnotation(Option.class);
3769 Text[][] values = optionRenderer.render(option, field, paramLabelRenderer, colorScheme);
3770 layout(field, values);
3771 }
3772 /** Calls {@link #addPositionalParameter(Field, CommandLine.Help.IParamLabelRenderer)} for all non-hidden Parameters in the list.
3773 * @param fields fields annotated with {@link Parameters} to add usage descriptions for
3774 * @param paramLabelRenderer knows how to render option parameters */
3775 public void addPositionalParameters(List<Field> fields, IParamLabelRenderer paramLabelRenderer) {
3776 for (Field field : fields) {
3777 Parameters parameters = field.getAnnotation(Parameters.class);
3778 if (!parameters.hidden()) {
3779 addPositionalParameter(field, paramLabelRenderer);
3780 }
3781 }
3782 }
3783 /**
3784 * Delegates to the {@link #parameterRenderer parameter renderer} of this layout
3785 * to obtain text values for the specified {@link Parameters}, and then calls
3786 * {@link #layout(Field, CommandLine.Help.Ansi.Text[][])} to write these text values into the correct cells in the TextTable.
3787 * @param field the field annotated with the specified Parameters
3788 * @param paramLabelRenderer knows how to render option parameters
3789 */
3790 public void addPositionalParameter(Field field, IParamLabelRenderer paramLabelRenderer) {
3791 Parameters option = field.getAnnotation(Parameters.class);
3792 Text[][] values = parameterRenderer.render(option, field, paramLabelRenderer, colorScheme);
3793 layout(field, values);
3794 }
3795 /** Returns the section of the usage help message accumulated in the TextTable owned by this layout. */
3796 @Override public String toString() {
3797 return table.toString();
3798 }
3799 }
3800 /** Sorts short strings before longer strings. */
3801 static class ShortestFirst implements Comparator<String> {
3802 public int compare(String o1, String o2) {
3803 return o1.length() - o2.length();
3804 }
3805 /** Sorts the specified array of Strings shortest-first and returns it. */
3806 public static String[] sort(String[] names) {
3807 Arrays.sort(names, new ShortestFirst());
3808 return names;
3809 }
3810 }
3811 /** Sorts {@code Option} instances by their name in case-insensitive alphabetic order. If an Option has
3812 * multiple names, the shortest name is used for the sorting. Help options follow non-help options. */
3813 static class SortByShortestOptionNameAlphabetically implements Comparator<Field> {
3814 public int compare(Field f1, Field f2) {
3815 Option o1 = f1.getAnnotation(Option.class);
3816 Option o2 = f2.getAnnotation(Option.class);
3817 if (o1 == null) { return 1; } else if (o2 == null) { return -1; } // options before params
3818 String[] names1 = ShortestFirst.sort(o1.names());
3819 String[] names2 = ShortestFirst.sort(o2.names());
3820 int result = names1[0].toUpperCase().compareTo(names2[0].toUpperCase()); // case insensitive sort
3821 result = result == 0 ? -names1[0].compareTo(names2[0]) : result; // lower case before upper case
3822 return o1.help() == o2.help() ? result : o2.help() ? -1 : 1; // help options come last
3823 }
3824 }
3825 /** Sorts {@code Option} instances by their max arity first, then their min arity, then delegates to super class. */
3826 static class SortByOptionArityAndNameAlphabetically extends SortByShortestOptionNameAlphabetically {
3827 public int compare(Field f1, Field f2) {
3828 Option o1 = f1.getAnnotation(Option.class);
3829 Option o2 = f2.getAnnotation(Option.class);
3830 Range arity1 = Range.optionArity(f1);
3831 Range arity2 = Range.optionArity(f2);
3832 int result = arity1.max - arity2.max;
3833 if (result == 0) {
3834 result = arity1.min - arity2.min;
3835 }
3836 if (result == 0) { // arity is same
3837 if (isMultiValue(f1) && !isMultiValue(f2)) { result = 1; } // f1 > f2
3838 if (!isMultiValue(f1) && isMultiValue(f2)) { result = -1; } // f1 < f2
3839 }
3840 return result == 0 ? super.compare(f1, f2) : result;
3841 }
3842 }
3843 /**
3844 * <p>Responsible for spacing out {@link Text} values according to the {@link Column} definitions the table was
3845 * created with. Columns have a width, indentation, and an overflow policy that decides what to do if a value is
3846 * longer than the column's width.</p>
3847 */
3848 public static class TextTable {
3849 /**
3850 * Helper class to index positions in a {@code Help.TextTable}.
3851 * @since 2.0
3852 */
3853 public static class Cell {
3854 /** Table column index (zero based). */
3855 public final int column;
3856 /** Table row index (zero based). */
3857 public final int row;
3858 /** Constructs a new Cell with the specified coordinates in the table.
3859 * @param column the zero-based table column
3860 * @param row the zero-based table row */
3861 public Cell(int column, int row) { this.column = column; this.row = row; }
3862 }
3863
3864 /** The column definitions of this table. */
3865 public final Column[] columns;
3866
3867 /** The {@code char[]} slots of the {@code TextTable} to copy text values into. */
3868 protected final List<Text> columnValues = new ArrayList<Text>();
3869
3870 /** By default, indent wrapped lines by 2 spaces. */
3871 public int indentWrappedLines = 2;
3872
3873 private final Ansi ansi;
3874
3875 /** Constructs a TextTable with five columns as follows:
3876 * <ol>
3877 * <li>required option/parameter marker (width: 2, indent: 0, TRUNCATE on overflow)</li>
3878 * <li>short option name (width: 2, indent: 0, TRUNCATE on overflow)</li>
3879 * <li>comma separator (width: 1, indent: 0, TRUNCATE on overflow)</li>
3880 * <li>long option name(s) (width: 24, indent: 1, SPAN multiple columns on overflow)</li>
3881 * <li>description line(s) (width: 51, indent: 1, WRAP to next row on overflow)</li>
3882 * </ol>
3883 * @param ansi whether to emit ANSI escape codes or not
3884 */
3885 public TextTable(Ansi ansi) {
3886 // "* -c, --create Creates a ...."
3887 this(ansi, new Column[] {
3888 new Column(2, 0, TRUNCATE), // "*"
3889 new Column(2, 0, TRUNCATE), // "-c"
3890 new Column(1, 0, TRUNCATE), // ","
3891 new Column(optionsColumnWidth - 2 - 2 - 1 , 1, SPAN), // " --create"
3892 new Column(usageHelpWidth - optionsColumnWidth, 1, WRAP) // " Creates a ..."
3893 });
3894 }
3895
3896 /** Constructs a new TextTable with columns with the specified width, all SPANning multiple columns on
3897 * overflow except the last column which WRAPS to the next row.
3898 * @param ansi whether to emit ANSI escape codes or not
3899 * @param columnWidths the width of the table columns (all columns have zero indent)
3900 */
3901 public TextTable(Ansi ansi, int... columnWidths) {
3902 this.ansi = Assert.notNull(ansi, "ansi");
3903 columns = new Column[columnWidths.length];
3904 for (int i = 0; i < columnWidths.length; i++) {
3905 columns[i] = new Column(columnWidths[i], 0, i == columnWidths.length - 1 ? SPAN: WRAP);
3906 }
3907 }
3908 /** Constructs a {@code TextTable} with the specified columns.
3909 * @param ansi whether to emit ANSI escape codes or not
3910 * @param columns columns to construct this TextTable with */
3911 public TextTable(Ansi ansi, Column... columns) {
3912 this.ansi = Assert.notNull(ansi, "ansi");
3913 this.columns = Assert.notNull(columns, "columns");
3914 if (columns.length == 0) { throw new IllegalArgumentException("At least one column is required"); }
3915 }
3916 /** Returns the {@code Text} slot at the specified row and column to write a text value into.
3917 * @param row the row of the cell whose Text to return
3918 * @param col the column of the cell whose Text to return
3919 * @return the Text object at the specified row and column
3920 * @since 2.0 */
3921 public Text textAt(int row, int col) { return columnValues.get(col + (row * columns.length)); }
3922
3923 /** Returns the {@code Text} slot at the specified row and column to write a text value into.
3924 * @param row the row of the cell whose Text to return
3925 * @param col the column of the cell whose Text to return
3926 * @return the Text object at the specified row and column
3927 * @deprecated use {@link #textAt(int, int)} instead */
3928 public Text cellAt(int row, int col) { return textAt(row, col); }
3929
3930 /** Returns the current number of rows of this {@code TextTable}.
3931 * @return the current number of rows in this TextTable */
3932 public int rowCount() { return columnValues.size() / columns.length; }
3933
3934 /** Adds the required {@code char[]} slots for a new row to the {@link #columnValues} field. */
3935 public void addEmptyRow() {
3936 for (int i = 0; i < columns.length; i++) {
3937 columnValues.add(ansi.new Text(columns[i].width));
3938 }
3939 }
3940
3941 /** Delegates to {@link #addRowValues(CommandLine.Help.Ansi.Text...)}.
3942 * @param values the text values to display in each column of the current row */
3943 public void addRowValues(String... values) {
3944 Text[] array = new Text[values.length];
3945 for (int i = 0; i < array.length; i++) {
3946 array[i] = values[i] == null ? Ansi.EMPTY_TEXT : ansi.new Text(values[i]);
3947 }
3948 addRowValues(array);
3949 }
3950 /**
3951 * Adds a new {@linkplain TextTable#addEmptyRow() empty row}, then calls {@link
3952 * TextTable#putValue(int, int, CommandLine.Help.Ansi.Text) putValue} for each of the specified values, adding more empty rows
3953 * if the return value indicates that the value spanned multiple columns or was wrapped to multiple rows.
3954 * @param values the values to write into a new row in this TextTable
3955 * @throws IllegalArgumentException if the number of values exceeds the number of Columns in this table
3956 */
3957 public void addRowValues(Text... values) {
3958 if (values.length > columns.length) {
3959 throw new IllegalArgumentException(values.length + " values don't fit in " +
3960 columns.length + " columns");
3961 }
3962 addEmptyRow();
3963 for (int col = 0; col < values.length; col++) {
3964 int row = rowCount() - 1;// write to last row: previous value may have wrapped to next row
3965 Cell cell = putValue(row, col, values[col]);
3966
3967 // add row if a value spanned/wrapped and there are still remaining values
3968 if ((cell.row != row || cell.column != col) && col != values.length - 1) {
3969 addEmptyRow();
3970 }
3971 }
3972 }
3973 /**
3974 * Writes the specified value into the cell at the specified row and column and returns the last row and
3975 * column written to. Depending on the Column's {@link Column#overflow Overflow} policy, the value may span
3976 * multiple columns or wrap to multiple rows when larger than the column width.
3977 * @param row the target row in the table
3978 * @param col the target column in the table to write to
3979 * @param value the value to write
3980 * @return a Cell indicating the position in the table that was last written to (since 2.0)
3981 * @throws IllegalArgumentException if the specified row exceeds the table's {@linkplain
3982 * TextTable#rowCount() row count}
3983 * @since 2.0 (previous versions returned a {@code java.awt.Point} object)
3984 */
3985 public Cell putValue(int row, int col, Text value) {
3986 if (row > rowCount() - 1) {
3987 throw new IllegalArgumentException("Cannot write to row " + row + ": rowCount=" + rowCount());
3988 }
3989 if (value == null || value.plain.length() == 0) { return new Cell(col, row); }
3990 Column column = columns[col];
3991 int indent = column.indent;
3992 switch (column.overflow) {
3993 case TRUNCATE:
3994 copy(value, textAt(row, col), indent);
3995 return new Cell(col, row);
3996 case SPAN:
3997 int startColumn = col;
3998 do {
3999 boolean lastColumn = col == columns.length - 1;
4000 int charsWritten = lastColumn
4001 ? copy(BreakIterator.getLineInstance(), value, textAt(row, col), indent)
4002 : copy(value, textAt(row, col), indent);
4003 value = value.substring(charsWritten);
4004 indent = 0;
4005 if (value.length > 0) { // value did not fit in column
4006 ++col; // write remainder of value in next column
4007 }
4008 if (value.length > 0 && col >= columns.length) { // we filled up all columns on this row
4009 addEmptyRow();
4010 row++;
4011 col = startColumn;
4012 indent = column.indent + indentWrappedLines;
4013 }
4014 } while (value.length > 0);
4015 return new Cell(col, row);
4016 case WRAP:
4017 BreakIterator lineBreakIterator = BreakIterator.getLineInstance();
4018 do {
4019 int charsWritten = copy(lineBreakIterator, value, textAt(row, col), indent);
4020 value = value.substring(charsWritten);
4021 indent = column.indent + indentWrappedLines;
4022 if (value.length > 0) { // value did not fit in column
4023 ++row; // write remainder of value in next row
4024 addEmptyRow();
4025 }
4026 } while (value.length > 0);
4027 return new Cell(col, row);
4028 }
4029 throw new IllegalStateException(column.overflow.toString());
4030 }
4031 private static int length(Text str) {
4032 return str.length; // TODO count some characters as double length
4033 }
4034
4035 private int copy(BreakIterator line, Text text, Text columnValue, int offset) {
4036 // Deceive the BreakIterator to ensure no line breaks after '-' character
4037 line.setText(text.plainString().replace("-", "\u00ff"));
4038 int done = 0;
4039 for (int start = line.first(), end = line.next(); end != BreakIterator.DONE; start = end, end = line.next()) {
4040 Text word = text.substring(start, end); //.replace("\u00ff", "-"); // not needed
4041 if (columnValue.maxLength >= offset + done + length(word)) {
4042 done += copy(word, columnValue, offset + done); // TODO localized length
4043 } else {
4044 break;
4045 }
4046 }
4047 if (done == 0 && length(text) > columnValue.maxLength) {
4048 // The value is a single word that is too big to be written to the column. Write as much as we can.
4049 done = copy(text, columnValue, offset);
4050 }
4051 return done;
4052 }
4053 private static int copy(Text value, Text destination, int offset) {
4054 int length = Math.min(value.length, destination.maxLength - offset);
4055 value.getStyledChars(value.from, length, destination, offset);
4056 return length;
4057 }
4058
4059 /** Copies the text representation that we built up from the options into the specified StringBuilder.
4060 * @param text the StringBuilder to write into
4061 * @return the specified StringBuilder object (to allow method chaining and a more fluid API) */
4062 public StringBuilder toString(StringBuilder text) {
4063 int columnCount = this.columns.length;
4064 StringBuilder row = new StringBuilder(usageHelpWidth);
4065 for (int i = 0; i < columnValues.size(); i++) {
4066 Text column = columnValues.get(i);
4067 row.append(column.toString());
4068 row.append(new String(spaces(columns[i % columnCount].width - column.length)));
4069 if (i % columnCount == columnCount - 1) {
4070 int lastChar = row.length() - 1;
4071 while (lastChar >= 0 && row.charAt(lastChar) == ' ') {lastChar--;} // rtrim
4072 row.setLength(lastChar + 1);
4073 text.append(row.toString()).append(System.getProperty("line.separator"));
4074 row.setLength(0);
4075 }
4076 }
4077 //if (Ansi.enabled()) { text.append(Style.reset.off()); }
4078 return text;
4079 }
4080 public String toString() { return toString(new StringBuilder()).toString(); }
4081 }
4082 /** Columns define the width, indent (leading number of spaces in a column before the value) and
4083 * {@linkplain Overflow Overflow} policy of a column in a {@linkplain TextTable TextTable}. */
4084 public static class Column {
4085
4086 /** Policy for handling text that is longer than the column width:
4087 * span multiple columns, wrap to the next row, or simply truncate the portion that doesn't fit. */
4088 public enum Overflow { TRUNCATE, SPAN, WRAP }
4089
4090 /** Column width in characters */
4091 public final int width;
4092
4093 /** Indent (number of empty spaces at the start of the column preceding the text value) */
4094 public final int indent;
4095
4096 /** Policy that determines how to handle values larger than the column width. */
4097 public final Overflow overflow;
4098 public Column(int width, int indent, Overflow overflow) {
4099 this.width = width;
4100 this.indent = indent;
4101 this.overflow = Assert.notNull(overflow, "overflow");
4102 }
4103 }
4104
4105 /** All usage help message are generated with a color scheme that assigns certain styles and colors to common
4106 * parts of a usage message: the command name, options, positional parameters and option parameters.
4107 * Users may customize these styles by creating Help with a custom color scheme.
4108 * <p>Note that these options and styles may not be rendered if ANSI escape codes are not
4109 * {@linkplain Ansi#enabled() enabled}.</p>
4110 * @see Help#defaultColorScheme(Ansi)
4111 */
4112 public static class ColorScheme {
4113 public final List<IStyle> commandStyles = new ArrayList<IStyle>();
4114 public final List<IStyle> optionStyles = new ArrayList<IStyle>();
4115 public final List<IStyle> parameterStyles = new ArrayList<IStyle>();
4116 public final List<IStyle> optionParamStyles = new ArrayList<IStyle>();
4117 private final Ansi ansi;
4118
4119 /** Constructs a new ColorScheme with {@link Help.Ansi#AUTO}. */
4120 public ColorScheme() { this(Ansi.AUTO); }
4121
4122 /** Constructs a new ColorScheme with the specified Ansi enabled mode.
4123 * @param ansi whether to emit ANSI escape codes or not
4124 */
4125 public ColorScheme(Ansi ansi) {this.ansi = Assert.notNull(ansi, "ansi"); }
4126
4127 /** Adds the specified styles to the registered styles for commands in this color scheme and returns this color scheme.
4128 * @param styles the styles to add to the registered styles for commands in this color scheme
4129 * @return this color scheme to enable method chaining for a more fluent API */
4130 public ColorScheme commands(IStyle... styles) { return addAll(commandStyles, styles); }
4131 /** Adds the specified styles to the registered styles for options in this color scheme and returns this color scheme.
4132 * @param styles the styles to add to registered the styles for options in this color scheme
4133 * @return this color scheme to enable method chaining for a more fluent API */
4134 public ColorScheme options(IStyle... styles) { return addAll(optionStyles, styles);}
4135 /** Adds the specified styles to the registered styles for positional parameters in this color scheme and returns this color scheme.
4136 * @param styles the styles to add to registered the styles for parameters in this color scheme
4137 * @return this color scheme to enable method chaining for a more fluent API */
4138 public ColorScheme parameters(IStyle... styles) { return addAll(parameterStyles, styles);}
4139 /** Adds the specified styles to the registered styles for option parameters in this color scheme and returns this color scheme.
4140 * @param styles the styles to add to the registered styles for option parameters in this color scheme
4141 * @return this color scheme to enable method chaining for a more fluent API */
4142 public ColorScheme optionParams(IStyle... styles) { return addAll(optionParamStyles, styles);}
4143 /** Returns a Text with all command styles applied to the specified command string.
4144 * @param command the command string to apply the registered command styles to
4145 * @return a Text with all command styles applied to the specified command string */
4146 public Ansi.Text commandText(String command) { return ansi().apply(command, commandStyles); }
4147 /** Returns a Text with all option styles applied to the specified option string.
4148 * @param option the option string to apply the registered option styles to
4149 * @return a Text with all option styles applied to the specified option string */
4150 public Ansi.Text optionText(String option) { return ansi().apply(option, optionStyles); }
4151 /** Returns a Text with all parameter styles applied to the specified parameter string.
4152 * @param parameter the parameter string to apply the registered parameter styles to
4153 * @return a Text with all parameter styles applied to the specified parameter string */
4154 public Ansi.Text parameterText(String parameter) { return ansi().apply(parameter, parameterStyles); }
4155 /** Returns a Text with all optionParam styles applied to the specified optionParam string.
4156 * @param optionParam the option parameter string to apply the registered option parameter styles to
4157 * @return a Text with all option parameter styles applied to the specified option parameter string */
4158 public Ansi.Text optionParamText(String optionParam) { return ansi().apply(optionParam, optionParamStyles); }
4159
4160 /** Replaces colors and styles in this scheme with ones specified in system properties, and returns this scheme.
4161 * Supported property names:<ul>
4162 * <li>{@code picocli.color.commands}</li>
4163 * <li>{@code picocli.color.options}</li>
4164 * <li>{@code picocli.color.parameters}</li>
4165 * <li>{@code picocli.color.optionParams}</li>
4166 * </ul><p>Property values can be anything that {@link Help.Ansi.Style#parse(String)} can handle.</p>
4167 * @return this ColorScheme
4168 */
4169 public ColorScheme applySystemProperties() {
4170 replace(commandStyles, System.getProperty("picocli.color.commands"));
4171 replace(optionStyles, System.getProperty("picocli.color.options"));
4172 replace(parameterStyles, System.getProperty("picocli.color.parameters"));
4173 replace(optionParamStyles, System.getProperty("picocli.color.optionParams"));
4174 return this;
4175 }
4176 private void replace(List<IStyle> styles, String property) {
4177 if (property != null) {
4178 styles.clear();
4179 addAll(styles, Style.parse(property));
4180 }
4181 }
4182 private ColorScheme addAll(List<IStyle> styles, IStyle... add) {
4183 styles.addAll(Arrays.asList(add));
4184 return this;
4185 }
4186
4187 public Ansi ansi() {
4188 return ansi;
4189 }
4190 }
4191
4192 /** Creates and returns a new {@link ColorScheme} initialized with picocli default values: commands are bold,
4193 * options and parameters use a yellow foreground, and option parameters use italic.
4194 * @param ansi whether the usage help message should contain ANSI escape codes or not
4195 * @return a new default color scheme
4196 */
4197 public static ColorScheme defaultColorScheme(Ansi ansi) {
4198 return new ColorScheme(ansi)
4199 .commands(Style.bold)
4200 .options(Style.fg_yellow)
4201 .parameters(Style.fg_yellow)
4202 .optionParams(Style.italic);
4203 }
4204
4205 /** Provides methods and inner classes to support using ANSI escape codes in usage help messages. */
4206 public enum Ansi {
4207 /** Only emit ANSI escape codes if the platform supports it and system property {@code "picocli.ansi"}
4208 * is not set to any value other than {@code "true"} (case insensitive). */
4209 AUTO,
4210 /** Forced ON: always emit ANSI escape code regardless of the platform. */
4211 ON,
4212 /** Forced OFF: never emit ANSI escape code regardless of the platform. */
4213 OFF;
4214 static Text EMPTY_TEXT = OFF.new Text(0);
4215 static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
4216 static final boolean isXterm = System.getenv("TERM") != null && System.getenv("TERM").startsWith("xterm");
4217 static final boolean ISATTY = calcTTY();
4218
4219 // http://stackoverflow.com/questions/1403772/how-can-i-check-if-a-java-programs-input-output-streams-are-connected-to-a-term
4220 static final boolean calcTTY() {
4221 if (isWindows && isXterm) { return true; } // Cygwin uses pseudo-tty and console is always null...
4222 try { return System.class.getDeclaredMethod("console").invoke(null) != null; }
4223 catch (Throwable reflectionFailed) { return true; }
4224 }
4225 private static boolean ansiPossible() { return ISATTY && (!isWindows || isXterm); }
4226
4227 /** Returns {@code true} if ANSI escape codes should be emitted, {@code false} otherwise.
4228 * @return ON: {@code true}, OFF: {@code false}, AUTO: if system property {@code "picocli.ansi"} is
4229 * defined then return its boolean value, otherwise return whether the platform supports ANSI escape codes */
4230 public boolean enabled() {
4231 if (this == ON) { return true; }
4232 if (this == OFF) { return false; }
4233 return (System.getProperty("picocli.ansi") == null ? ansiPossible() : Boolean.getBoolean("picocli.ansi"));
4234 }
4235
4236 /** Defines the interface for an ANSI escape sequence. */
4237 public interface IStyle {
4238
4239 /** The Control Sequence Introducer (CSI) escape sequence {@value}. */
4240 String CSI = "\u001B[";
4241
4242 /** Returns the ANSI escape code for turning this style on.
4243 * @return the ANSI escape code for turning this style on */
4244 String on();
4245
4246 /** Returns the ANSI escape code for turning this style off.
4247 * @return the ANSI escape code for turning this style off */
4248 String off();
4249 }
4250
4251 /**
4252 * A set of pre-defined ANSI escape code styles and colors, and a set of convenience methods for parsing
4253 * text with embedded markup style names, as well as convenience methods for converting
4254 * styles to strings with embedded escape codes.
4255 */
4256 public enum Style implements IStyle {
4257 reset(0, 0), bold(1, 21), faint(2, 22), italic(3, 23), underline(4, 24), blink(5, 25), reverse(7, 27),
4258 fg_black(30, 39), fg_red(31, 39), fg_green(32, 39), fg_yellow(33, 39), fg_blue(34, 39), fg_magenta(35, 39), fg_cyan(36, 39), fg_white(37, 39),
4259 bg_black(40, 49), bg_red(41, 49), bg_green(42, 49), bg_yellow(43, 49), bg_blue(44, 49), bg_magenta(45, 49), bg_cyan(46, 49), bg_white(47, 49),
4260 ;
4261 private final int startCode;
4262 private final int endCode;
4263
4264 Style(int startCode, int endCode) {this.startCode = startCode; this.endCode = endCode; }
4265 public String on() { return CSI + startCode + "m"; }
4266 public String off() { return CSI + endCode + "m"; }
4267
4268 /** Returns the concatenated ANSI escape codes for turning all specified styles on.
4269 * @param styles the styles to generate ANSI escape codes for
4270 * @return the concatenated ANSI escape codes for turning all specified styles on */
4271 public static String on(IStyle... styles) {
4272 StringBuilder result = new StringBuilder();
4273 for (IStyle style : styles) {
4274 result.append(style.on());
4275 }
4276 return result.toString();
4277 }
4278 /** Returns the concatenated ANSI escape codes for turning all specified styles off.
4279 * @param styles the styles to generate ANSI escape codes for
4280 * @return the concatenated ANSI escape codes for turning all specified styles off */
4281 public static String off(IStyle... styles) {
4282 StringBuilder result = new StringBuilder();
4283 for (IStyle style : styles) {
4284 result.append(style.off());
4285 }
4286 return result.toString();
4287 }
4288 /** Parses the specified style markup and returns the associated style.
4289 * The markup may be one of the Style enum value names, or it may be one of the Style enum value
4290 * names when {@code "fg_"} is prepended, or it may be one of the indexed colors in the 256 color palette.
4291 * @param str the case-insensitive style markup to convert, e.g. {@code "blue"} or {@code "fg_blue"},
4292 * or {@code "46"} (indexed color) or {@code "0;5;0"} (RGB components of an indexed color)
4293 * @return the IStyle for the specified converter
4294 */
4295 public static IStyle fg(String str) {
4296 try { return Style.valueOf(str.toLowerCase(ENGLISH)); } catch (Exception ignored) {}
4297 try { return Style.valueOf("fg_" + str.toLowerCase(ENGLISH)); } catch (Exception ignored) {}
4298 return new Palette256Color(true, str);
4299 }
4300 /** Parses the specified style markup and returns the associated style.
4301 * The markup may be one of the Style enum value names, or it may be one of the Style enum value
4302 * names when {@code "bg_"} is prepended, or it may be one of the indexed colors in the 256 color palette.
4303 * @param str the case-insensitive style markup to convert, e.g. {@code "blue"} or {@code "bg_blue"},
4304 * or {@code "46"} (indexed color) or {@code "0;5;0"} (RGB components of an indexed color)
4305 * @return the IStyle for the specified converter
4306 */
4307 public static IStyle bg(String str) {
4308 try { return Style.valueOf(str.toLowerCase(ENGLISH)); } catch (Exception ignored) {}
4309 try { return Style.valueOf("bg_" + str.toLowerCase(ENGLISH)); } catch (Exception ignored) {}
4310 return new Palette256Color(false, str);
4311 }
4312 /** Parses the specified comma-separated sequence of style descriptors and returns the associated
4313 * styles. For each markup, strings starting with {@code "bg("} are delegated to
4314 * {@link #bg(String)}, others are delegated to {@link #bg(String)}.
4315 * @param commaSeparatedCodes one or more descriptors, e.g. {@code "bg(blue),underline,red"}
4316 * @return an array with all styles for the specified descriptors
4317 */
4318 public static IStyle[] parse(String commaSeparatedCodes) {
4319 String[] codes = commaSeparatedCodes.split(",");
4320 IStyle[] styles = new IStyle[codes.length];
4321 for(int i = 0; i < codes.length; ++i) {
4322 if (codes[i].toLowerCase(ENGLISH).startsWith("fg(")) {
4323 int end = codes[i].indexOf(')');
4324 styles[i] = Style.fg(codes[i].substring(3, end < 0 ? codes[i].length() : end));
4325 } else if (codes[i].toLowerCase(ENGLISH).startsWith("bg(")) {
4326 int end = codes[i].indexOf(')');
4327 styles[i] = Style.bg(codes[i].substring(3, end < 0 ? codes[i].length() : end));
4328 } else {
4329 styles[i] = Style.fg(codes[i]);
4330 }
4331 }
4332 return styles;
4333 }
4334 }
4335
4336 /** Defines a palette map of 216 colors: 6 * 6 * 6 cube (216 colors):
4337 * 16 + 36 * r + 6 * g + b (0 <= r, g, b <= 5). */
4338 static class Palette256Color implements IStyle {
4339 private final int fgbg;
4340 private final int color;
4341
4342 Palette256Color(boolean foreground, String color) {
4343 this.fgbg = foreground ? 38 : 48;
4344 String[] rgb = color.split(";");
4345 if (rgb.length == 3) {
4346 this.color = 16 + 36 * Integer.decode(rgb[0]) + 6 * Integer.decode(rgb[1]) + Integer.decode(rgb[2]);
4347 } else {
4348 this.color = Integer.decode(color);
4349 }
4350 }
4351 public String on() { return String.format(CSI + "%d;5;%dm", fgbg, color); }
4352 public String off() { return CSI + (fgbg + 1) + "m"; }
4353 }
4354 private static class StyledSection {
4355 int startIndex, length;
4356 String startStyles, endStyles;
4357 StyledSection(int start, int len, String style1, String style2) {
4358 startIndex = start; length = len; startStyles = style1; endStyles = style2;
4359 }
4360 StyledSection withStartIndex(int newStart) {
4361 return new StyledSection(newStart, length, startStyles, endStyles);
4362 }
4363 }
4364
4365 /**
4366 * Returns a new Text object where all the specified styles are applied to the full length of the
4367 * specified plain text.
4368 * @param plainText the string to apply all styles to. Must not contain markup!
4369 * @param styles the styles to apply to the full plain text
4370 * @return a new Text object
4371 */
4372 public Text apply(String plainText, List<IStyle> styles) {
4373 if (plainText.length() == 0) { return new Text(0); }
4374 Text result = new Text(plainText.length());
4375 IStyle[] all = styles.toArray(new IStyle[styles.size()]);
4376 result.sections.add(new StyledSection(
4377 0, plainText.length(), Style.on(all), Style.off(reverse(all)) + Style.reset.off()));
4378 result.plain.append(plainText);
4379 result.length = result.plain.length();
4380 return result;
4381 }
4382
4383 private static <T> T[] reverse(T[] all) {
4384 for (int i = 0; i < all.length / 2; i++) {
4385 T temp = all[i];
4386 all[i] = all[all.length - i - 1];
4387 all[all.length - i - 1] = temp;
4388 }
4389 return all;
4390 }
4391 /** Encapsulates rich text with styles and colors. Text objects may be constructed with Strings containing
4392 * markup like {@code @|bg(red),white,underline some text|@}, and this class converts the markup to ANSI
4393 * escape codes.
4394 * <p>
4395 * Internally keeps both an enriched and a plain text representation to allow layout components to calculate
4396 * text width while remaining unaware of the embedded ANSI escape codes.</p> */
4397 public class Text implements Cloneable {
4398 private final int maxLength;
4399 private int from;
4400 private int length;
4401 private StringBuilder plain = new StringBuilder();
4402 private List<StyledSection> sections = new ArrayList<StyledSection>();
4403
4404 /** Constructs a Text with the specified max length (for use in a TextTable Column).
4405 * @param maxLength max length of this text */
4406 public Text(int maxLength) { this.maxLength = maxLength; }
4407
4408 /**
4409 * Constructs a Text with the specified String, which may contain markup like
4410 * {@code @|bg(red),white,underline some text|@}.
4411 * @param input the string with markup to parse
4412 */
4413 public Text(String input) {
4414 maxLength = -1;
4415 plain.setLength(0);
4416 int i = 0;
4417
4418 while (true) {
4419 int j = input.indexOf("@|", i);
4420 if (j == -1) {
4421 if (i == 0) {
4422 plain.append(input);
4423 length = plain.length();
4424 return;
4425 }
4426 plain.append(input.substring(i, input.length()));
4427 length = plain.length();
4428 return;
4429 }
4430 plain.append(input.substring(i, j));
4431 int k = input.indexOf("|@", j);
4432 if (k == -1) {
4433 plain.append(input);
4434 length = plain.length();
4435 return;
4436 }
4437
4438 j += 2;
4439 String spec = input.substring(j, k);
4440 String[] items = spec.split(" ", 2);
4441 if (items.length == 1) {
4442 plain.append(input);
4443 length = plain.length();
4444 return;
4445 }
4446
4447 IStyle[] styles = Style.parse(items[0]);
4448 addStyledSection(plain.length(), items[1].length(),
4449 Style.on(styles), Style.off(reverse(styles)) + Style.reset.off());
4450 plain.append(items[1]);
4451 i = k + 2;
4452 }
4453 }
4454 private void addStyledSection(int start, int length, String startStyle, String endStyle) {
4455 sections.add(new StyledSection(start, length, startStyle, endStyle));
4456 }
4457 public Object clone() {
4458 try { return super.clone(); } catch (CloneNotSupportedException e) { throw new IllegalStateException(e); }
4459 }
4460
4461 public Text[] splitLines() {
4462 List<Text> result = new ArrayList<Text>();
4463 boolean trailingEmptyString = false;
4464 int start = 0, end = 0;
4465 for (int i = 0; i < plain.length(); i++, end = i) {
4466 char c = plain.charAt(i);
4467 boolean eol = c == '\n';
4468 eol |= (c == '\r' && i + 1 < plain.length() && plain.charAt(i + 1) == '\n' && ++i > 0); // \r\n
4469 eol |= c == '\r';
4470 if (eol) {
4471 result.add(this.substring(start, end));
4472 trailingEmptyString = i == plain.length() - 1;
4473 start = i + 1;
4474 }
4475 }
4476 if (start < plain.length() || trailingEmptyString) {
4477 result.add(this.substring(start, plain.length()));
4478 }
4479 return result.toArray(new Text[result.size()]);
4480 }
4481
4482 /** Returns a new {@code Text} instance that is a substring of this Text. Does not modify this instance!
4483 * @param start index in the plain text where to start the substring
4484 * @return a new Text instance that is a substring of this Text */
4485 public Text substring(int start) {
4486 return substring(start, length);
4487 }
4488
4489 /** Returns a new {@code Text} instance that is a substring of this Text. Does not modify this instance!
4490 * @param start index in the plain text where to start the substring
4491 * @param end index in the plain text where to end the substring
4492 * @return a new Text instance that is a substring of this Text */
4493 public Text substring(int start, int end) {
4494 Text result = (Text) clone();
4495 result.from = from + start;
4496 result.length = end - start;
4497 return result;
4498 }
4499 /** Returns a new {@code Text} instance with the specified text appended. Does not modify this instance!
4500 * @param string the text to append
4501 * @return a new Text instance */
4502 public Text append(String string) {
4503 return append(new Text(string));
4504 }
4505
4506 /** Returns a new {@code Text} instance with the specified text appended. Does not modify this instance!
4507 * @param other the text to append
4508 * @return a new Text instance */
4509 public Text append(Text other) {
4510 Text result = (Text) clone();
4511 result.plain = new StringBuilder(plain.toString().substring(from, from + length));
4512 result.from = 0;
4513 result.sections = new ArrayList<StyledSection>();
4514 for (StyledSection section : sections) {
4515 result.sections.add(section.withStartIndex(section.startIndex - from));
4516 }
4517 result.plain.append(other.plain.toString().substring(other.from, other.from + other.length));
4518 for (StyledSection section : other.sections) {
4519 int index = result.length + section.startIndex - other.from;
4520 result.sections.add(section.withStartIndex(index));
4521 }
4522 result.length = result.plain.length();
4523 return result;
4524 }
4525
4526 /**
4527 * Copies the specified substring of this Text into the specified destination, preserving the markup.
4528 * @param from start of the substring
4529 * @param length length of the substring
4530 * @param destination destination Text to modify
4531 * @param offset indentation (padding)
4532 */
4533 public void getStyledChars(int from, int length, Text destination, int offset) {
4534 if (destination.length < offset) {
4535 for (int i = destination.length; i < offset; i++) {
4536 destination.plain.append(' ');
4537 }
4538 destination.length = offset;
4539 }
4540 for (StyledSection section : sections) {
4541 destination.sections.add(section.withStartIndex(section.startIndex - from + destination.length));
4542 }
4543 destination.plain.append(plain.toString().substring(from, from + length));
4544 destination.length = destination.plain.length();
4545 }
4546 /** Returns the plain text without any formatting.
4547 * @return the plain text without any formatting */
4548 public String plainString() { return plain.toString().substring(from, from + length); }
4549
4550 public boolean equals(Object obj) { return toString().equals(String.valueOf(obj)); }
4551 public int hashCode() { return toString().hashCode(); }
4552
4553 /** Returns a String representation of the text with ANSI escape codes embedded, unless ANSI is
4554 * {@linkplain Ansi#enabled()} not enabled}, in which case the plain text is returned.
4555 * @return a String representation of the text with ANSI escape codes embedded (if enabled) */
4556 public String toString() {
4557 if (!Ansi.this.enabled()) {
4558 return plain.toString().substring(from, from + length);
4559 }
4560 if (length == 0) { return ""; }
4561 StringBuilder sb = new StringBuilder(plain.length() + 20 * sections.size());
4562 StyledSection current = null;
4563 int end = Math.min(from + length, plain.length());
4564 for (int i = from; i < end; i++) {
4565 StyledSection section = findSectionContaining(i);
4566 if (section != current) {
4567 if (current != null) { sb.append(current.endStyles); }
4568 if (section != null) { sb.append(section.startStyles); }
4569 current = section;
4570 }
4571 sb.append(plain.charAt(i));
4572 }
4573 if (current != null) { sb.append(current.endStyles); }
4574 return sb.toString();
4575 }
4576
4577 private StyledSection findSectionContaining(int index) {
4578 for (StyledSection section : sections) {
4579 if (index >= section.startIndex && index < section.startIndex + section.length) {
4580 return section;
4581 }
4582 }
4583 return null;
4584 }
4585 }
4586 }
4587 }
4588
4589 /**
4590 * Utility class providing some defensive coding convenience methods.
4591 */
4592 private static final class Assert {
4593 /**
4594 * Throws a NullPointerException if the specified object is null.
4595 * @param object the object to verify
4596 * @param description error message
4597 * @param <T> type of the object to check
4598 * @return the verified object
4599 */
4600 static <T> T notNull(T object, String description) {
4601 if (object == null) {
4602 throw new NullPointerException(description);
4603 }
4604 return object;
4605 }
4606 private Assert() {} // private constructor: never instantiate
4607 }
4608 private enum TraceLevel { OFF, WARN, INFO, DEBUG;
4609 public boolean isEnabled(TraceLevel other) { return ordinal() >= other.ordinal(); }
4610 private void print(Tracer tracer, String msg, Object... params) {
4611 if (tracer.level.isEnabled(this)) { tracer.stream.printf(prefix(msg), params); }
4612 }
4613 private String prefix(String msg) { return "[picocli " + this + "] " + msg; }
4614 static TraceLevel lookup(String key) { return key == null ? WARN : empty(key) || "true".equalsIgnoreCase(key) ? INFO : valueOf(key); }
4615 }
4616 private static class Tracer {
4617 TraceLevel level = TraceLevel.lookup(System.getProperty("picocli.trace"));
4618 PrintStream stream = System.err;
4619 void warn (String msg, Object... params) { TraceLevel.WARN.print(this, msg, params); }
4620 void info (String msg, Object... params) { TraceLevel.INFO.print(this, msg, params); }
4621 void debug(String msg, Object... params) { TraceLevel.DEBUG.print(this, msg, params); }
4622 boolean isWarn() { return level.isEnabled(TraceLevel.WARN); }
4623 boolean isInfo() { return level.isEnabled(TraceLevel.INFO); }
4624 boolean isDebug() { return level.isEnabled(TraceLevel.DEBUG); }
4625 }
4626 /** Base class of all exceptions thrown by {@code picocli.CommandLine}.
4627 * @since 2.0 */
4628 public static class PicocliException extends RuntimeException {
4629 private static final long serialVersionUID = -2574128880125050818L;
4630 public PicocliException(String msg) { super(msg); }
4631 public PicocliException(String msg, Exception ex) { super(msg, ex); }
4632 }
4633 /** Exception indicating a problem during {@code CommandLine} initialization.
4634 * @since 2.0 */
4635 public static class InitializationException extends PicocliException {
4636 private static final long serialVersionUID = 8423014001666638895L;
4637 public InitializationException(String msg) { super(msg); }
4638 public InitializationException(String msg, Exception ex) { super(msg, ex); }
4639 }
4640 /** Exception indicating a problem while invoking a command or subcommand.
4641 * @since 2.0 */
4642 public static class ExecutionException extends PicocliException {
4643 private static final long serialVersionUID = 7764539594267007998L;
4644 private final CommandLine commandLine;
4645 public ExecutionException(CommandLine commandLine, String msg) {
4646 super(msg);
4647 this.commandLine = Assert.notNull(commandLine, "commandLine");
4648 }
4649 public ExecutionException(CommandLine commandLine, String msg, Exception ex) {
4650 super(msg, ex);
4651 this.commandLine = Assert.notNull(commandLine, "commandLine");
4652 }
4653 /** Returns the {@code CommandLine} object for the (sub)command that could not be invoked.
4654 * @return the {@code CommandLine} object for the (sub)command where invocation failed.
4655 */
4656 public CommandLine getCommandLine() { return commandLine; }
4657 }
4658
4659 /** Exception thrown by {@link ITypeConverter} implementations to indicate a String could not be converted. */
4660 public static class TypeConversionException extends PicocliException {
4661 private static final long serialVersionUID = 4251973913816346114L;
4662 public TypeConversionException(String msg) { super(msg); }
4663 }
4664 /** Exception indicating something went wrong while parsing command line options. */
4665 public static class ParameterException extends PicocliException {
4666 private static final long serialVersionUID = 1477112829129763139L;
4667 private final CommandLine commandLine;
4668
4669 /** Constructs a new ParameterException with the specified CommandLine and error message.
4670 * @param commandLine the command or subcommand whose input was invalid
4671 * @param msg describes the problem
4672 * @since 2.0 */
4673 public ParameterException(CommandLine commandLine, String msg) {
4674 super(msg);
4675 this.commandLine = Assert.notNull(commandLine, "commandLine");
4676 }
4677 /** Constructs a new ParameterException with the specified CommandLine and error message.
4678 * @param commandLine the command or subcommand whose input was invalid
4679 * @param msg describes the problem
4680 * @param ex the exception that caused this ParameterException
4681 * @since 2.0 */
4682 public ParameterException(CommandLine commandLine, String msg, Exception ex) {
4683 super(msg, ex);
4684 this.commandLine = Assert.notNull(commandLine, "commandLine");
4685 }
4686
4687 /** Returns the {@code CommandLine} object for the (sub)command whose input could not be parsed.
4688 * @return the {@code CommandLine} object for the (sub)command where parsing failed.
4689 * @since 2.0
4690 */
4691 public CommandLine getCommandLine() { return commandLine; }
4692
4693 private static ParameterException create(CommandLine cmd, Exception ex, String arg, int i, String[] args) {
4694 String msg = ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage()
4695 + " while processing argument at or before arg[" + i + "] '" + arg + "' in " + Arrays.toString(args) + ": " + ex.toString();
4696 return new ParameterException(cmd, msg, ex);
4697 }
4698 }
4699 /**
4700 * Exception indicating that a required parameter was not specified.
4701 */
4702 public static class MissingParameterException extends ParameterException {
4703 private static final long serialVersionUID = 5075678535706338753L;
4704 public MissingParameterException(CommandLine commandLine, String msg) {
4705 super(commandLine, msg);
4706 }
4707
4708 private static MissingParameterException create(CommandLine cmd, Collection<Field> missing, String separator) {
4709 if (missing.size() == 1) {
4710 return new MissingParameterException(cmd, "Missing required option '"
4711 + describe(missing.iterator().next(), separator) + "'");
4712 }
4713 List<String> names = new ArrayList<String>(missing.size());
4714 for (Field field : missing) {
4715 names.add(describe(field, separator));
4716 }
4717 return new MissingParameterException(cmd, "Missing required options " + names.toString());
4718 }
4719 private static String describe(Field field, String separator) {
4720 String prefix = (field.isAnnotationPresent(Option.class))
4721 ? field.getAnnotation(Option.class).names()[0] + separator
4722 : "params[" + field.getAnnotation(Parameters.class).index() + "]" + separator;
4723 return prefix + Help.DefaultParamLabelRenderer.renderParameterName(field);
4724 }
4725 }
4726
4727 /**
4728 * Exception indicating that multiple fields have been annotated with the same Option name.
4729 */
4730 public static class DuplicateOptionAnnotationsException extends InitializationException {
4731 private static final long serialVersionUID = -3355128012575075641L;
4732 public DuplicateOptionAnnotationsException(String msg) { super(msg); }
4733
4734 private static DuplicateOptionAnnotationsException create(String name, Field field1, Field field2) {
4735 return new DuplicateOptionAnnotationsException("Option name '" + name + "' is used by both " +
4736 field1.getDeclaringClass().getName() + "." + field1.getName() + " and " +
4737 field2.getDeclaringClass().getName() + "." + field2.getName());
4738 }
4739 }
4740 /** Exception indicating that there was a gap in the indices of the fields annotated with {@link Parameters}. */
4741 public static class ParameterIndexGapException extends InitializationException {
4742 private static final long serialVersionUID = -1520981133257618319L;
4743 public ParameterIndexGapException(String msg) { super(msg); }
4744 }
4745 /** Exception indicating that a command line argument could not be mapped to any of the fields annotated with
4746 * {@link Option} or {@link Parameters}. */
4747 public static class UnmatchedArgumentException extends ParameterException {
4748 private static final long serialVersionUID = -8700426380701452440L;
4749 public UnmatchedArgumentException(CommandLine commandLine, String msg) { super(commandLine, msg); }
4750 public UnmatchedArgumentException(CommandLine commandLine, Stack<String> args) { this(commandLine, new ArrayList<String>(reverse(args))); }
4751 public UnmatchedArgumentException(CommandLine commandLine, List<String> args) { this(commandLine, "Unmatched argument" + (args.size() == 1 ? " " : "s ") + args); }
4752 }
4753 /** Exception indicating that more values were specified for an option or parameter than its {@link Option#arity() arity} allows. */
4754 public static class MaxValuesforFieldExceededException extends ParameterException {
4755 private static final long serialVersionUID = 6536145439570100641L;
4756 public MaxValuesforFieldExceededException(CommandLine commandLine, String msg) { super(commandLine, msg); }
4757 }
4758 /** Exception indicating that an option for a single-value option field has been specified multiple times on the command line. */
4759 public static class OverwrittenOptionException extends ParameterException {
4760 private static final long serialVersionUID = 1338029208271055776L;
4761 public OverwrittenOptionException(CommandLine commandLine, String msg) { super(commandLine, msg); }
4762 }
4763 /**
4764 * Exception indicating that an annotated field had a type for which no {@link ITypeConverter} was
4765 * {@linkplain #registerConverter(Class, ITypeConverter) registered}.
4766 */
4767 public static class MissingTypeConverterException extends ParameterException {
4768 private static final long serialVersionUID = -6050931703233083760L;
4769 public MissingTypeConverterException(CommandLine commandLine, String msg) { super(commandLine, msg); }
4770 }
4771 }