Skip to content

Commit

Permalink
[#516] CommandLine::usage and CommandLine::getUsageMessage should use…
Browse files Browse the repository at this point in the history
… configured ColorScheme

also refactored some duplicate code
  • Loading branch information
remkop committed Apr 27, 2019
1 parent 54e1c64 commit ad2ca02
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2066,17 +2066,17 @@ public static void usage(Object command, PrintStream out, Help.ColorScheme color
}

/**
* Delegates to {@link #usage(PrintStream, Help.Ansi)} with the {@linkplain Help.Ansi#AUTO platform default}.
* Delegates to {@link #usage(PrintStream, Help.ColorScheme)} with the {@linkplain #getColorScheme() configured} color scheme.
* @param out the printStream to print to
* @see #usage(PrintStream, Help.ColorScheme)
*/
public void usage(PrintStream out) { usage(out, Help.Ansi.AUTO); }
public void usage(PrintStream out) { usage(out, getColorScheme()); }
/**
* Delegates to {@link #usage(PrintWriter, Help.Ansi)} with the {@linkplain Help.Ansi#AUTO platform default}.
* Delegates to {@link #usage(PrintWriter, Help.ColorScheme)} with the {@linkplain #getColorScheme() configured} color scheme.
* @param writer the PrintWriter to print to
* @see #usage(PrintWriter, Help.ColorScheme)
* @since 3.0 */
public void usage(PrintWriter writer) { usage(writer, Help.Ansi.AUTO); }
public void usage(PrintWriter writer) { usage(writer, getColorScheme()); }

/**
* Delegates to {@link #usage(PrintStream, Help.ColorScheme)} with the {@linkplain Help#defaultColorScheme(CommandLine.Help.Ansi) default color scheme}.
Expand Down Expand Up @@ -2126,7 +2126,7 @@ public void usage(PrintWriter writer, Help.ColorScheme colorScheme) {
/** Similar to {@link #usage(PrintStream)}, but returns the usage help message as a String instead of printing it to the {@code PrintStream}.
* @since 3.2 */
public String getUsageMessage() {
return usage(new StringBuilder(), getHelpFactory().create(getCommandSpec(), Help.defaultColorScheme(Help.Ansi.AUTO))).toString();
return usage(new StringBuilder(), getHelpFactory().create(getCommandSpec(), getColorScheme())).toString();
}
/** Similar to {@link #usage(PrintStream, Help.Ansi)}, but returns the usage help message as a String instead of printing it to the {@code PrintStream}.
* @since 3.2 */
Expand Down Expand Up @@ -2230,8 +2230,7 @@ public void printVersionHelp(PrintWriter out, Help.Ansi ansi, Object... params)
public static <C extends Callable<T>, T> T call(C callable, String... args) {
CommandLine cmd = new CommandLine(callable);
List<Object> results = cmd.parseWithHandler(new RunLast(), args);
@SuppressWarnings("unchecked") T result = (results == null || results.isEmpty()) ? null : (T) results.get(0);
return result;
return firstElement(results);
}

/**
Expand Down Expand Up @@ -2300,8 +2299,7 @@ public static <C extends Callable<T>, T> T call(C callable, String... args) {
@Deprecated public static <C extends Callable<T>, T> T call(C callable, PrintStream out, PrintStream err, Help.Ansi ansi, String... args) {
CommandLine cmd = new CommandLine(callable);
List<Object> results = cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args);
@SuppressWarnings("unchecked") T result = (results == null || results.isEmpty()) ? null : (T) results.get(0);
return result;
return firstElement(results);
}
/**
* Equivalent to {@code new CommandLine(callableClass, factory).execute(args)}, except for the return value.
Expand All @@ -2319,8 +2317,7 @@ public static <C extends Callable<T>, T> T call(C callable, String... args) {
public static <C extends Callable<T>, T> T call(Class<C> callableClass, IFactory factory, String... args) {
CommandLine cmd = new CommandLine(callableClass, factory);
List<Object> results = cmd.parseWithHandler(new RunLast(), args);
@SuppressWarnings("unchecked") T result = (results == null || results.isEmpty()) ? null : (T) results.get(0);
return result;
return firstElement(results);
}
/**
* Delegates to {@link #call(Class, IFactory, PrintStream, PrintStream, Help.Ansi, String...)} with
Expand Down Expand Up @@ -2396,8 +2393,11 @@ public static <C extends Callable<T>, T> T call(Class<C> callableClass, IFactory
@Deprecated public static <C extends Callable<T>, T> T call(Class<C> callableClass, IFactory factory, PrintStream out, PrintStream err, Help.Ansi ansi, String... args) {
CommandLine cmd = new CommandLine(callableClass, factory);
List<Object> results = cmd.parseWithHandlers(new RunLast().useOut(out).useAnsi(ansi), new DefaultExceptionHandler<List<Object>>().useErr(err).useAnsi(ansi), args);
@SuppressWarnings("unchecked") T result = (results == null || results.isEmpty()) ? null : (T) results.get(0);
return result;
return firstElement(results);
}

@SuppressWarnings("unchecked") private static <T> T firstElement(List<Object> results) {
return (results == null || results.isEmpty()) ? null : (T) results.get(0);
}

/**
Expand Down

0 comments on commit ad2ca02

Please sign in to comment.