How do I parse command line arguments in Java?

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

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

19条回答
  •  鱼传尺愫
    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

    
      com.github.pcj
      google-options
      1.0.0
    
    

    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 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.emptyMap(),
                                                  OptionsParser.HelpVerbosity.LONG));
      }
    
    }
    

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

提交回复
热议问题