How do I parse command line arguments in Java?

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

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

相关标签:
19条回答
  • 2020-11-22 00:32

    I've used JOpt and found it quite handy: http://jopt-simple.sourceforge.net/

    The front page also provides a list of about 8 alternative libraries, check them out and pick the one that most suits your needs.

    0 讨论(0)
  • 2020-11-22 00:33

    Take a look at the Commons CLI project, lots of good stuff in there.

    0 讨论(0)
  • 2020-11-22 00:34

    airline @ Github looks good. It is based on annotation and is trying to emulate Git command line structures.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 00:36

    I have been trying to maintain a list of Java CLI parsers.

    • Airline
      • Active Fork: https://github.com/rvesse/airline
    • argparse4j
    • argparser
    • args4j
    • clajr
    • cli-parser
    • CmdLn
    • Commandline
    • DocOpt.java
    • dolphin getopt
    • DPML CLI (Jakarta Commons CLI2 fork)
    • Dr. Matthias Laux
    • Jakarta Commons CLI
    • jargo
    • jargp
    • jargs
    • java-getopt
    • jbock
    • JCLAP
    • jcmdline
    • jcommander
    • jcommando
    • jewelcli (written by me)
    • JOpt simple
    • jsap
    • naturalcli
    • Object Mentor CLI article (more about refactoring and TDD)
    • parse-cmd
    • ritopt
    • Rop
    • TE-Code Command
    • picocli has ANSI colorized usage help and autocomplete
    0 讨论(0)
  • 2020-11-22 00:37

    Argparse4j is best I have found. It mimics Python's argparse libary which is very convenient and powerful.

    0 讨论(0)
提交回复
热议问题