Google Options (devtools)

Command line argument parsing library from the folks at Google.

License

License

GroupId

GroupId

com.github.pcj
ArtifactId

ArtifactId

google-options
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Google Options (devtools)
Command line argument parsing library from the folks at Google.
Project URL

Project URL

https://github.com/pcj/google-options
Source Code Management

Source Code Management

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

Download google-options

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.pcj/google-options/ -->
<dependency>
    <groupId>com.github.pcj</groupId>
    <artifactId>google-options</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.pcj/google-options/
implementation 'com.github.pcj:google-options:1.0.0'
// https://jarcasting.com/artifacts/com.github.pcj/google-options/
implementation ("com.github.pcj:google-options:1.0.0")
'com.github.pcj:google-options:jar:1.0.0'
<dependency org="com.github.pcj" name="google-options" rev="1.0.0">
  <artifact name="google-options" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.pcj', module='google-options', version='1.0.0')
)
libraryDependencies += "com.github.pcj" % "google-options" % "1.0.0"
[com.github.pcj/google-options "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 jar 3.0.1
com.google.guava : guava jar 19.0

test (4)

Group / Artifact Type Version
com.google.guava : guava-testlib jar 19.0
com.google.truth : truth jar 0.28
junit : junit jar 4.11
org.hamcrest : hamcrest-core jar 1.3

Project Modules

There are no modules declared in this project.

google-options Build Status Maven Central JavaDoc

Google Options

This is the command-line arguments parser from the Bazel Project. The com.google.devtools.common.options package has been split out into a separate jar for general utility.

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));
  }

}

Please consult the tests and source code for more detailed information.

JavaDoc API documentation is housed in the gh-pages branch.

Versions

Version
1.0.0