Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 339: allow using stderr instead of stdout #376

Merged
merged 2 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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