Skip to content

Commit

Permalink
Merge pull request #119 from bigwheels16/master
Browse files Browse the repository at this point in the history
added echoInput option to @parameter to control whether passwords are displayed when entered at the prompt
  • Loading branch information
cbeust committed May 30, 2012
2 parents bc214ea + f8f8310 commit 76dfe99
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 10 deletions.
10 changes: 10 additions & 0 deletions doc/index.html
Expand Up @@ -157,6 +157,16 @@ <h4>Password</h4>

You will need to type the value at this point before JCommander resumes.

<h4>Echo Input</h4>

In Java 6, by default, you will not be able to see what you type for passwords entered at the prompt (Java 5 and lower will always show the password). However, you can override this by setting <tt>echoInput</tt> to "true" (default is "false" and this setting only has an effect when <tt>password</tt> is "true"):
<pre class="brush: java">
public class ArgsPassword {
@Parameter(names = "-password", description = "Connection password", password = true, echoInput = true)
private String password;
}
</pre>

<h2><a class="section" name="Custom_types">Custom types</a></h2>

<h3>By annotation</h3>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/beust/jcommander/JCommander.java
Expand Up @@ -623,7 +623,7 @@ private void parseValues(String[] args) {
//
// Password option, use the Console to retrieve the password
//
char[] password = readPassword(pd.getDescription());
char[] password = readPassword(pd.getDescription(), pd.getParameter().echoInput());
pd.addValue(new String(password));
m_requiredFields.remove(pd.getField());
} else {
Expand Down Expand Up @@ -801,9 +801,9 @@ private int processFixedArity(String[] args, int originalIndex, ParameterDescrip
* Invoke Console.readPassword through reflection to avoid depending
* on Java 6.
*/
private char[] readPassword(String description) {
private char[] readPassword(String description, boolean echoInput) {
getConsole().print(description + ": ");
return getConsole().readPassword();
return getConsole().readPassword(echoInput);
}

private String[] subArray(String[] args, int index) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/beust/jcommander/Parameter.java
Expand Up @@ -100,4 +100,10 @@
* a comma separated splitter will be used.
*/
Class<? extends IParameterSplitter> splitter() default CommaParameterSplitter.class;

/**
* If true, console will not echo typed input
* Used in conjunction with password = true
*/
boolean echoInput() default false;
}
4 changes: 4 additions & 0 deletions src/main/java/com/beust/jcommander/WrappedParameter.java
Expand Up @@ -46,6 +46,10 @@ public boolean variableArity() {
public Class<? extends IParameterValidator> validateWith() {
return m_parameter != null ? m_parameter.validateWith() : m_dynamicParameter.validateWith();
}

public boolean echoInput() {
return m_parameter != null ? m_parameter.echoInput() : false;
}

public void addValue(Field field, Object object, Object value)
throws IllegalArgumentException, IllegalAccessException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/beust/jcommander/internal/Console.java
Expand Up @@ -6,5 +6,5 @@ public interface Console {

void println(String msg);

char[] readPassword();
char[] readPassword(boolean echoInput);
}
Expand Up @@ -16,7 +16,7 @@ public void println(String msg) {
System.out.println(msg);
}

public char[] readPassword() {
public char[] readPassword(boolean echoInput) {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/beust/jcommander/internal/JDK6Console.java
Expand Up @@ -25,15 +25,21 @@ public void println(String msg) {
writer.println(msg);
}

public char[] readPassword() {
public char[] readPassword(boolean echoInput) {
try {
writer.flush();
Method readPasswordMethod = console.getClass().getDeclaredMethod("readPassword", new Class<?>[0]);
return (char[]) readPasswordMethod.invoke(console, new Object[0]);
Method method;
if (echoInput) {
method = console.getClass().getDeclaredMethod("readLine", new Class<?>[0]);
return ((String) method.invoke(console, new Object[0])).toCharArray();
} else {
method = console.getClass().getDeclaredMethod("readPassword", new Class<?>[0]);
return (char[]) method.invoke(console, new Object[0]);
}
}
catch (Exception e) {
throw new ParameterException(e);
}
}

}
}
2 changes: 1 addition & 1 deletion src/test/java/com/beust/jcommander/JCommanderTest.java
Expand Up @@ -666,7 +666,7 @@ class A {
Assert.assertEquals(a.password, "password");
} finally {
System.setIn(stdin);
}
}
}

public void dynamicParameters() {
Expand Down

0 comments on commit 76dfe99

Please sign in to comment.