I have been trying to put my head around this. There are no errors but I am not seeing the desired answer:
public class Clopts {
private static Options options = null;
private static final String InputDir = "i";
private static final String OutputDir = "o";
private String input;
private CommandLine cmd = null;
static{
options = new Options();
options.addOption(InputDir, false, "Input Directory");
options.addOption(OutputDir, false, "Output Directory. " + OutputDir );
}
public static void main(String[] args) {
Clopts cliProg = new Clopts();
cliProg.loadArgs(args);
}
private void loadArgs(String[] args){
CommandLineParser parser = new PosixParser();
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.err.println("Error parsing arguments");
e.printStackTrace();
System.exit(1);
}
if (cmd.hasOption(InputDir)){
input = cmd.getOptionValue(InputDir);
System.out.println(input); // This is always null :(
}
}
}
While I am passing the argument -i foo -o bar
But I am not seeing the foo or bar every time i see is null.
Also I want to println in the main module. How do i get the options from command line and then print out what the options are.
When you define your options with:
The
falsemeans they don’t take arguments. If you want an option value you must specifytruehere.