From d619e83a234b42e155016253fe124edcb5467a0c Mon Sep 17 00:00:00 2001 From: Yevgeniy Melnichuk Date: Sun, 25 Jun 2017 22:55:11 +0200 Subject: [PATCH 1/2] make console configurable by JCommander.Builder --- .../java/com/beust/jcommander/JCommander.java | 16 +++++++++++----- .../beust/jcommander/ParameterDescription.java | 13 ++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index 09d1e7de..d3e16b5f 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -147,7 +147,7 @@ public void addValue(Object convertedValue) { private List unknownArgs = Lists.newArrayList(); - private static Console console; + private Console console; private final Options options; @@ -265,12 +265,14 @@ public void setExpandAtSign(boolean expandAtSign) { options.expandAtSign = expandAtSign; } - public static synchronized Console getConsole() { + public void setConsole(Console console) { this.console = console; } + + public synchronized Console getConsole() { if (console == null) { try { Method consoleMethod = System.class.getDeclaredMethod("console"); Object console = consoleMethod.invoke(null); - JCommander.console = new JDK6Console(console); + this.console = new JDK6Console(console); } catch (Throwable t) { console = new DefaultConsole(); } @@ -762,8 +764,7 @@ else if (commands.isEmpty()) { for(final Class validator : mainParameter.annotation.validateWith() ) { - ParameterDescription.validateParameter(mainParameter.description, - validator, + mainParameter.description.validateParameter(validator, "Default", value); } @@ -1116,6 +1117,11 @@ public Builder args(String[] args) { return this; } + public Builder console(Console console) { + jCommander.setConsole(console); + return this; + } + /** * Disables expanding {@code @file}. * diff --git a/src/main/java/com/beust/jcommander/ParameterDescription.java b/src/main/java/com/beust/jcommander/ParameterDescription.java index bed5ba16..f4dba4e5 100644 --- a/src/main/java/com/beust/jcommander/ParameterDescription.java +++ b/src/main/java/com/beust/jcommander/ParameterDescription.java @@ -330,7 +330,7 @@ private void validateParameter(String name, String value) { final Class validators[] = wrappedParameter.validateWith(); if (validators != null && validators.length > 0) { for(final Class validator: validators) { - validateParameter(this, validator, name, value); + validateParameter(validator, name, value); } } } @@ -344,7 +344,7 @@ void validateValueParameter(String name, Object value) { } } - public static void validateValueParameter(Class validator, + public void validateValueParameter(Class validator, String name, Object value) { try { if (validator != NoValueValidator.class) { @@ -356,8 +356,7 @@ public static void validateValueParameter(Class valid } } - public static void validateParameter(ParameterDescription pd, - Class validator, + public void validateParameter(Class validator, String name, String value) { try { @@ -367,7 +366,7 @@ public static void validateParameter(ParameterDescription pd, validator.newInstance().validate(name, value); if (IParameterValidator2.class.isAssignableFrom(validator)) { IParameterValidator2 instance = (IParameterValidator2) validator.newInstance(); - instance.validate(name, value, pd); + instance.validate(name, value, this); } } catch (InstantiationException | IllegalAccessException e) { throw new ParameterException("Can't instantiate validator:" + e); @@ -404,9 +403,9 @@ private boolean fieldIsSetForTheFirstTime(boolean isDefault) { return (!isDefault && !assigned); } - private static void p(String string) { + private void p(String string) { if (System.getProperty(JCommander.DEBUG_PROPERTY) != null) { - JCommander.getConsole().println("[ParameterDescription] " + string); + jCommander.getConsole().println("[ParameterDescription] " + string); } } From 656a86f1d2843504bce5c72589ebaf48b7d2d607 Mon Sep 17 00:00:00 2001 From: Yevgeniy Melnichuk Date: Sun, 25 Jun 2017 22:56:19 +0200 Subject: [PATCH 2/2] allow System.out to be replaced by something else (f.e. System.err) --- .../beust/jcommander/internal/DefaultConsole.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/beust/jcommander/internal/DefaultConsole.java b/src/main/java/com/beust/jcommander/internal/DefaultConsole.java index 8fd7d6d7..cc234f41 100644 --- a/src/main/java/com/beust/jcommander/internal/DefaultConsole.java +++ b/src/main/java/com/beust/jcommander/internal/DefaultConsole.java @@ -5,15 +5,25 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.PrintStream; public class DefaultConsole implements Console { + private final PrintStream target; + + public DefaultConsole(PrintStream target) { + this.target = target; + } + + public DefaultConsole() { + this.target = System.out; + } public void print(String msg) { - System.out.print(msg); + target.print(msg); } public void println(String msg) { - System.out.println(msg); + target.println(msg); } public char[] readPassword(boolean echoInput) {