私は次のような状況に立ち往生しました。オプション値を取得しようとすると、nullが返されます。ここでオプション値を取得すると常に「null」が返されます
は、コード断片である:
public static Options configureOptions() {
Option groupOption = Option.builder("g")
.longOpt("group")
.required(false)
.desc("The group of the user.")
.build();
Options allOptions = new Options();
allOptions.addOption(taskOption);
return allOptions;
}
public static void main(String[] args) throws ParseException {
Options options = configureOptions();
CommandLineParser parser = new DefaultParser();
CommandLine commands = parser.parse(options, args);
if (commands.hasOption("group")) {
System.out.println("group: " + commands.getOptionValue("group"));
}
}
そしてオプション-gスタッフで実行し、出力は常にnull
です。 Option.Builder
を使用して
java -classpath rsa-1.0.0-SNAPSHOT.jar;c:\Users\user.m2\repository\commons-cli\commons-cli\1.3.1\commons-cli-1.3.1.jar Main -g staff
'-g staff'出力はnullにさせる以外に何をしますか? – dorukayhan
javadocを読んでください:https://commons.apache.org/proper/commons-cli/javadocs/api-release/index.html:* opt - ** Short **オプションの名前* –