Java 为命令行分析器添加新选项:无法识别的选项错误

Java 为命令行分析器添加新选项:无法识别的选项错误,java,command-line,Java,Command Line,因此,我从包含需要命令行参数的java文件的github存储库中派生并压缩了一些代码 这是我的问题的相关代码- Options options = new Options(); options.addOption("f", false, "force processing of text file"); options.addOption("printHTML", false, "print HTML file for inspection"); options.addO

因此,我从包含需要命令行参数的java文件的github存储库中派生并压缩了一些代码

这是我的问题的相关代码-

Options options = new Options();
    options.addOption("f", false, "force processing of text file");
    options.addOption("printHTML", false, "print HTML file for inspection");
    options.addOption("w", true, "coreference weight file");
    options.addOption("doc", true, "text document to process");
    options.addOption("tok", true, "processed text document");
    options.addOption("docId", true, "text document ID to process");
    options.addOption("p", true, "output directory");
    options.addOption("id", true, "book ID");
    options.addOption("d", false, "dump pronoun and quotes for annotation")

    CommandLine cmd = null;
    try {
        CommandLineParser parser = new BasicParser();
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        e.printStackTrace();
    }
我根据自己的要求添加了一个额外的选项“quoteAttr”

Options options = new Options();
    options.addOption("f", false, "force processing of text file");
    options.addOption("printHTML", false, "print HTML file for inspection");
    options.addOption("quoteAttr", false, "print quote ids and their attributions");//Here
    options.addOption("w", true, "coreference weight file");
    options.addOption("doc", true, "text document to process");
    options.addOption("tok", true, "processed text document");
    options.addOption("docId", true, "text document ID to process");
    options.addOption("p", true, "output directory");
    options.addOption("id", true, "book ID");
    options.addOption("d", false, "dump pronoun and quotes for annotation");

    CommandLine cmd = null;
    try {
        CommandLineParser parser = new BasicParser();
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        e.printStackTrace();
    }
将此选项添加到代码本身不会影响程序的正常运行

如图所示-

./runjava novels/BookNLP -doc data/originalTexts/dickens.oliver.pg730.txt -printHTML -p data/output/dickens -tok data/tokens/dickens.oliver.tokens -f
Adding annotator tokenize
Adding annotator ssplit
Adding annotator pos
Reading POS tagger model from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [1.6 sec].
Adding annotator lemma
Adding annotator ner
Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... done [4.6 sec].
Loading classifier from edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz ... 
但是,当我在命令行上使用“quoteAttr”选项时-

./runjava novels/BookNLP -doc data/originalTexts/dickens.oliver.pg730.txt -printHTML -quoteAttr -p data/output/dickens -tok data/tokens/dickens.oliver.tokens -f
我得到这个错误-

org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: -quoteAttr
at org.apache.commons.cli.Parser.processOption(Parser.java:363)
at org.apache.commons.cli.Parser.parse(Parser.java:199)
at org.apache.commons.cli.Parser.parse(Parser.java:85)
at novels.BookNLP.main(BookNLP.java:98)
Exception in thread "main" java.lang.NullPointerException
at novels.BookNLP.main(BookNLP.java:106)

为什么会发生这种情况?

您确定在测试之前重建了项目吗?它似乎“能够”解析您的选项,因为它会打印出来,但没有注册要执行的类或某些逻辑。可能在某个地方有一个巨大的“Select case”或等效工厂,它返回要执行的逻辑。搜索字符串“Unrecognized”,看看它在哪里找不到新选项,而不是
BasicParser
,我将使用
new DefaultParser()
,因为
BasicParser
已被弃用。@KevinWallis否我没有这样做。。。我只是在做了更改后直接运行命令。我想这就是问题的根源。github页面上说,如果我想编译所有内容,我需要使用“ant”…@KevinWallis我也尝试了DefaultParser