How do I parse command line arguments in Java?

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

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

19条回答
  •  灰色年华
    2020-11-22 00:35

    As one of the comments mentioned earlier (https://github.com/pcj/google-options) would be a good choice to start with.

    One thing I want to add-on is:

    1) If you run into some parser reflection error, please try use a newer version of the guava. in my case:

    maven_jar(
        name = "com_google_guava_guava",
        artifact = "com.google.guava:guava:19.0",
        server = "maven2_server",
    )
    
    maven_jar(
        name = "com_github_pcj_google_options",
        artifact = "com.github.pcj:google-options:jar:1.0.0",
        server = "maven2_server",
    )
    
    maven_server(
        name = "maven2_server",
        url = "http://central.maven.org/maven2/",
    )
    

    2) When running the commandline:

    bazel run path/to/your:project -- --var1 something --var2 something -v something
    

    3) When you need the usage help, just type:

    bazel run path/to/your:project -- --help
    

提交回复
热议问题