我一直在試圖將我的頭圍繞此。有沒有錯誤,但我沒有看到想要的答案:無法檢索命令行選項
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 :(
}
}
}
雖然我傳遞參數-i foo -o bar
但我沒有看到富或酒吧每次我看到的是空的時間。
另外我想println在主模塊中。我如何從命令行獲得選項,然後打印出選項。
我正在使用現有的庫..我正在使用apache commons cli庫嗎? – Fraz
當你'System.out.println(args);'?會發生什麼? –
按照Java命名約定,你應該寫'private static final String INPUT_DIR =「i」;' – jlordo