仅供参考:我正在Windows机器上运行这个Java程序。我相信这很重要。
基本上,我在为带有双连字符的选项指定值时遇到了问题。单连字符选项工作正常。
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);
}
}
程序失败并出现错误:
线程"main"org.apache.commons.cli.UnrecognizedOptionException:无法识别的选项:--input file.txt
如果我将参数指定为-i file.txt
,则没有错误。如果参数是--input
,也没有错误。为什么双连字符选项不接受任何值?它是与解析器有关,还是我在Windows机器上运行它?
我在这里做错了什么?如有任何帮助,我们将不胜感激。
非常感谢。
问题是如何在样本中指定tha参数,而不是
String[] arguments = new String[] { "--input file.txt" };
您需要指定
String[] arguments = new String[] { "--input", "file.txt" };
解析器希望以java在main()方法中提供的方式获取参数,这些参数在空白处分开,否则您将指示解析器处理选项"--输入文件.txt";,即它将整个字符串视为选项的名称。