How do I parse command line arguments in Java?

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

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

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

    If you are familiar with gnu getopt, there is a Java port at: http://www.urbanophile.com/arenn/hacking/download.htm.

    There appears to be a some classes that do this:

    • http://docs.sun.com/source/816-5618-10/netscape/ldap/util/GetOpt.html
    • http://xml.apache.org/xalan-j/apidocs/org/apache/xalan/xsltc/cmdline/getopt/GetOpt.html
    0 讨论(0)
  • 2020-11-22 00:45

    I know most people here are going to find 10 million reasons why they dislike my way, but nevermind. I like to keep things simple, so I just separate the key from the value using a '=' and store them in a HashMap like this:

    Map<String, String> argsMap = new HashMap<>();
    for (String arg: args) {
        String[] parts = arg.split("=");
        argsMap.put(parts[0], parts[1]);
    } 
    

    You could always maintain a list with the arguments you are expecting, to help the user in case he forgot an argument or used a wrong one... However, if you want too many features this solution is not for you anyway.

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

    This is Google's command line parsing library open-sourced as part of the Bazel project. Personally I think it's the best one out there, and far easier than Apache CLI.

    https://github.com/pcj/google-options

    Installation

    Bazel

    maven_jar(
        name = "com_github_pcj_google_options",
        artifact = "com.github.pcj:google-options:jar:1.0.0",
        sha1 = "85d54fe6771e5ff0d54827b0a3315c3e12fdd0c7",
    )
    

    Gradle

    dependencies {
      compile 'com.github.pcj:google-options:1.0.0'
    }
    

    Maven

    <dependency>
      <groupId>com.github.pcj</groupId>
      <artifactId>google-options</artifactId>
      <version>1.0.0</version>
    </dependency>
    

    Usage

    Create a class that extends OptionsBase and defines your @Option(s).

    package example;
    
    import com.google.devtools.common.options.Option;
    import com.google.devtools.common.options.OptionsBase;
    
    import java.util.List;
    
    /**
     * Command-line options definition for example server.
     */
    public class ServerOptions extends OptionsBase {
    
      @Option(
          name = "help",
          abbrev = 'h',
          help = "Prints usage info.",
          defaultValue = "true"
        )
      public boolean help;
    
      @Option(
          name = "host",
          abbrev = 'o',
          help = "The server host.",
          category = "startup",
          defaultValue = ""
      )
      public String host;
    
      @Option(
        name = "port",
        abbrev = 'p',
        help = "The server port.",
        category = "startup",
        defaultValue = "8080"
        )
        public int port;
    
      @Option(
        name = "dir",
        abbrev = 'd',
        help = "Name of directory to serve static files.",
        category = "startup",
        allowMultiple = true,
        defaultValue = ""
        )
        public List<String> dirs;
    
    }
    

    Parse the arguments and use them.

    package example;
    
    import com.google.devtools.common.options.OptionsParser;
    import java.util.Collections;
    
    public class Server {
    
      public static void main(String[] args) {
        OptionsParser parser = OptionsParser.newOptionsParser(ServerOptions.class);
        parser.parseAndExitUponError(args);
        ServerOptions options = parser.getOptions(ServerOptions.class);
        if (options.host.isEmpty() || options.port < 0 || options.dirs.isEmpty()) {
          printUsage(parser);
          return;
        }
    
        System.out.format("Starting server at %s:%d...\n", options.host, options.port);
        for (String dirname : options.dirs) {
          System.out.format("\\--> Serving static files at <%s>\n", dirname);
        }
      }
    
      private static void printUsage(OptionsParser parser) {
        System.out.println("Usage: java -jar server.jar OPTIONS");
        System.out.println(parser.describeOptions(Collections.<String, String>emptyMap(),
                                                  OptionsParser.HelpVerbosity.LONG));
      }
    
    }
    

    https://github.com/pcj/google-options

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

    Yeap.

    I think you're looking for something like this: http://commons.apache.org/cli

    The Apache Commons CLI library provides an API for processing command line interfaces.

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

    For Spring users, we should mention also https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/SimpleCommandLinePropertySource.html and his twin brother https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/JOptCommandLinePropertySource.html (JOpt implementation of the same functionality). The advantage in Spring is that you can directly bind the command line arguments to attributes, there is an example here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/CommandLinePropertySource.html

    0 讨论(0)
  • 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

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