Reputation: 1721
FYI: I'm running this Java program on a Windows machine. I'm sure that it's important.
Basically, I am having a problem specifying a value to to options with double-hyphens. Single-hyphen options work fine.
import org.apache.commons.cli.*;
public class Test {
public static void main(String[] args) throws ParseException {
String[] arguments = new String[] { "--input file.txt" };
// create the Options
Options options = new Options();
options.addOption( "i", "input", false, "Specify the input file." );
// Create the parser
CommandLineParser parser = new GnuParser();
// Parse the command line
CommandLine cmd = parser.parse(options, arguments);
}
}
The program fails with error:
Exception in thread "main" org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: --input file.txt
If I specify the argument as -i file.txt
, no errors. If the argument is --input
, also no errors. Why won't the double-hyphen option accept any values? Does it have something to do with the parser or that I'm running it on a Windows machine?
What am I doing wrong here? Any help would be appreciated.
Thank you very much.
Upvotes: 0
Views: 3928
Reputation: 15890
The problem is how you specify tha arguments in your sample, instead of
String[] arguments = new String[] { "--input file.txt" };
You need to specify
String[] arguments = new String[] { "--input", "file.txt" };
The parser wants to get the arguments the way they are provided by java in the main() method, separated at the blank, otherwise you instruct the parser to handle the option "--input file.txt", i.e. it sees the whole string as the name of the option.
Upvotes: 1