Skip to content

Commit 4efb6a3

Browse files
committed
Updated implementation of the getPassword() method and added support for "fuzzy" arg lookup in the getValue() method in the Console class
1 parent d3d897d commit 4efb6a3

File tree

1 file changed

+12
-41
lines changed

1 file changed

+12
-41
lines changed

src/javaxt/utils/Console.java

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -203,46 +203,7 @@ public static String getUserName(String prompt){
203203
* entered.
204204
*/
205205
public static String getPassword(String prompt) {
206-
207-
String password = "";
208-
ConsoleEraser consoleEraser = new ConsoleEraser();
209-
System.out.print(prompt);
210-
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
211-
consoleEraser.start();
212-
try {
213-
password = in.readLine();
214-
}
215-
catch (IOException e){
216-
System.out.println("Error trying to read your password!");
217-
//System.exit(1);
218-
}
219-
220-
consoleEraser.halt();
221-
System.out.print("\b");
222-
223-
return password;
224-
}
225-
226-
227-
//**************************************************************************
228-
//** ConsoleEraser Class
229-
//**************************************************************************
230-
private static class ConsoleEraser extends Thread {
231-
private boolean running = true;
232-
public void run() {
233-
while (running) {
234-
System.out.print("\b ");
235-
try {
236-
Thread.currentThread().sleep(1);
237-
}
238-
catch(InterruptedException e) {
239-
break;
240-
}
241-
}
242-
}
243-
public synchronized void halt() {
244-
running = false;
245-
}
206+
return new String(System.console().readPassword(prompt));
246207
}
247208

248209

@@ -282,13 +243,23 @@ public static HashMap<String, String> parseArgs(String[] args){
282243
//**************************************************************************
283244
/** Returns a value for a given set of command line arguments
284245
* @param args HashMap of command line inputs generated by parseArgs()
285-
* @param keys One or more keywords to search (e.g. "-threads", "-t")
246+
* @param keys One or more keywords to search (e.g. "--threads", "-t")
286247
*/
287248
public static Value getValue(HashMap<String, String> args, String ...keys){
288249
for (String key : keys){
289250
if (args.containsKey(key)){
290251
return new Value(args.get(key));
291252
}
253+
else{
254+
255+
//Fuzzy match (user passed one hyphen instead of two)
256+
if (key.startsWith("-")){
257+
String k = key.substring(1);
258+
if (k.startsWith("-") && args.containsKey(k)){
259+
return new Value(args.get(k));
260+
}
261+
}
262+
}
292263
}
293264
return new Value(null);
294265
}

0 commit comments

Comments
 (0)