Skip to content

Commit

Permalink
Merge pull request #376 from yevmel/issue-339
Browse files Browse the repository at this point in the history
Issue 339: allow using stderr instead of stdout
  • Loading branch information
cbeust authored Aug 21, 2017
2 parents 161d310 + 656a86f commit 0483213
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void addValue(Object convertedValue) {

private List<String> unknownArgs = Lists.newArrayList();

private static Console console;
private Console console;

private final Options options;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -762,8 +764,7 @@ else if (commands.isEmpty()) {

for(final Class<? extends IParameterValidator> validator : mainParameter.annotation.validateWith()
) {
ParameterDescription.validateParameter(mainParameter.description,
validator,
mainParameter.description.validateParameter(validator,
"Default", value);
}

Expand Down Expand Up @@ -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}.
*
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/beust/jcommander/ParameterDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private void validateParameter(String name, String value) {
final Class<? extends IParameterValidator> validators[] = wrappedParameter.validateWith();
if (validators != null && validators.length > 0) {
for(final Class<? extends IParameterValidator> validator: validators) {
validateParameter(this, validator, name, value);
validateParameter(validator, name, value);
}
}
}
Expand All @@ -344,7 +344,7 @@ void validateValueParameter(String name, Object value) {
}
}

public static void validateValueParameter(Class<? extends IValueValidator> validator,
public void validateValueParameter(Class<? extends IValueValidator> validator,
String name, Object value) {
try {
if (validator != NoValueValidator.class) {
Expand All @@ -356,8 +356,7 @@ public static void validateValueParameter(Class<? extends IValueValidator> valid
}
}

public static void validateParameter(ParameterDescription pd,
Class<? extends IParameterValidator> validator,
public void validateParameter(Class<? extends IParameterValidator> validator,
String name, String value) {
try {

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/beust/jcommander/internal/DefaultConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0483213

Please sign in to comment.