首页 > 解决方案 > 为什么我无法使用 Apache CLI 解析选项

问题描述

我的问题是当我创建Options实例并在那里设置数据时,它无法从我的静态字段中读取值"ITEM_COUNT"。不明白这是什么问题?

public class Main {

    public static final String ITEM_COUNT = "100";

    public static void main( String [] args ) throws ParseException, FileNotFoundException {


        Options options = new Options();
        options.addOption(ITEM_COUNT, true, "The number of items being simulated" );

        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = parser.parse( options, args );

// there I have NumberFormatException:null, the value from ITEM_COUNT is null

        Integer item_count = Integer.valueOf(cmd.getOptionValue(ITEM_COUNT)); 

        Runner mockServer = new Runner();
        mockServer.initialize(item_count);
        mockServer.start();

    }
}

标签: javacommand-line-interfaceapache-commonsnumberformatexception

解决方案


您可以运行Run -> Edit Configuration -> Program arguments -> -name_of_arg value_of_arg


推荐阅读