How do you make flags for command line arguments in Java? [closed]

為{幸葍}努か 提交于 2019-12-01 02:00:54

Even though this might not directly answer your question, I strongly suggest that you use a library for that. Some widely used options:

  1. Commons CLI - http://commons.apache.org/proper/commons-cli/
  2. JCommander - http://jcommander.org/
  3. JOpt-Simple - http://pholser.github.io/jopt-simple/
  4. ArgParse4J - http://argparse4j.sourceforge.net/

After all, "life is too short to parse command line parameters". As an example (using JCommander), you just need to define your parameters through annotations:

//class MyOptions
@Parameter(names = { "-d", "--outputDirectory" }, description = "Directory")
private String outputDirectory;

And then parse your bean using:

public static void main(String[] args){
    MyOptions options = new MyOptions();
    new JCommander(options, args);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!