Issue with OptionParser#printHelpOn()

13 views
Skip to first unread message

Dheeraj Khardwal

unread,
Apr 6, 2016, 10:31:21 AM4/6/16
to jopt-simple
Pardon me if this is naive. 
I'm getting an issue - which I'm not getting quite properly - while printing help multiple times based on user input. 
Please see below snippet for my implementation and output as well.

import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

public class CommandLine {

private OptionParser parser;
private Random random;

private void init() throws IOException {
random = new Random();
parser = new OptionParser() {
{
accepts("n", "next random number");
accepts("q", "quit");
accepts("h", "show help").isForHelp();
}
};
parser.printHelpOn(System.out);
}
private boolean handle(String[] args) throws IOException {
OptionSet opts = parser.parse(args);
if(opts.has("n")) {
System.out.println(random.nextInt());
}else if(opts.has("h")) {
parser.printHelpOn(System.out);
} else if(opts.has("q")) {
System.out.println("Bye.");
return true;
}
return false;
}

public static void main(String[] args) throws IOException {
CommandLine commander = new CommandLine();
commander.init();
Scanner sc = null;
try {
sc = new Scanner(System.in);
for (prompt(); sc.hasNextLine(); prompt()) {
String line = sc.nextLine().replaceAll("\n", "");
// return pressed
if (line.length() == 0) {
System.out.println("No option selected. Use -h to get help.");
continue;
}

// split line into arguments
boolean exit = commander.handle(line.split(" "));
if(exit)
break;
}
} finally {
sc.close();
}
}
private static void prompt() {
System.out.print(">> ");
}

}

And the output:

Option  Description       
------  -----------       
-h      show help         
-n      next random number
-q      quit              
>> -n
-1307682825
>> -n
-282704935
>> -h
Option  Description       
------  -----------       
-h      show help         
-n      next random number
-q      quit              
Option  Description       
------  -----------       
-h      show help         
-n      next random number
-q      quit              
>> -h
Option  Description       
------  -----------       
-h      show help         
-n      next random number
-q      quit              
Option  Description       
------  -----------       
-h      show help         
-n      next random number
-q      quit              
Option  Description       
------  -----------       
-h      show help         
-n      next random number
-q      quit              
>> -q
Bye.

Please tell me how can I get around this.

Paul Holser

unread,
Apr 6, 2016, 11:41:18 AM4/6/16
to jopt-simple
Hi Dheeraj,

Thanks for your interest in jopt-simple!

It appears that BuiltinHelpFormatter.format() wasn't ever expecting to be called more than once on a given instance. It's building up its output cumulatively on every call to format().

I'll add a GitHub issue for this, so that every time OptionParser.printHelpOn() is called and delegates to a BuiltinHelpFormatter, its cumulative output is cleared before building it up fresh.

Appreciate it!

--p

Dheeraj Khardwal

unread,
Apr 6, 2016, 2:26:52 PM4/6/16
to jopt-simple
Hi Paul,

First of all, many thanks for creating this library. :)
And thanks for taking this issue up.
Yes, I just looked up the issue on github (sorry I posted here instead of raising the issue there).
Looking forward for contributing to jopts. :)

Regards,
Dheeraj
Reply all
Reply to author
Forward
0 new messages