How do I parse command line arguments in Java?

前端 未结 19 2013
终归单人心
终归单人心 2020-11-22 00:16

What is a good way of parsing command line arguments in Java?

19条回答
  •  滥情空心
    2020-11-22 00:47

    If you want something lightweight (jar size ~ 20 kb) and simple to use, you can try argument-parser. It can be used in most of the use cases, supports specifying arrays in the argument and has no dependency on any other library. It works for Java 1.5 or above. Below excerpt shows an example on how to use it:

    public static void main(String[] args) {
        String usage = "--day|-d day --mon|-m month [--year|-y year][--dir|-ds directoriesToSearch]";
        ArgumentParser argParser = new ArgumentParser(usage, InputData.class);
        InputData inputData = (InputData) argParser.parse(args);
        showData(inputData);
    
        new StatsGenerator().generateStats(inputData);
    }
    

    More examples can be found here

提交回复
热议问题