Class CLIArgumentParser<T>

  • All Implemented Interfaces:
    CLIParser<T>

    public class CLIArgumentParser<T>
    extends java.lang.Object
    implements CLIParser<T>
    Utility class to handle typical command line argument parsing and log initialization. In addition to parsing the command line, this class can display help information. All command lines will support the "--help" argument, which will be handled automatically by CLIArgumentParser. To use this class, a command line application should define a public class with a public field for each command line option. The options should be annotated as either a CLIArgument or a CLIFlag. CLIArgument is used for options that expect some sort of argument, like a path or server hostname. CLIFlag is for boolean flags that have no argument. Both annotations require an argument description which will be used when displaying command line usage. They also allow a long option name (like "--server-address") and a short option name (like "-s"). If no long option is given, it will be automatically generated from the member variable name. If no short option is given, the option will have no short option. CLIArguments also require an argument name, and allow a default value. The argument name is displayed with usage information. In the usage information "--infile ", "filename" is the argument name. Default values are used for optional fields. If a field does not have a default value provided, then it is required on the command line. Otherwise, the default will be used. CLIFlags have no default value or argument name. They will be set to "true" if present on the command line; false otherwise. Below is a sample class that shows how to use CLIArgumentParser. It declares an inner Args class with one required argument "path" and three optional arguments.
    
      public class ExportTopology {
        public static class Args {
          @CLIArgument(description="Path to topology files", argumentName="products")
          public String path;
          @CLIArgument(description="Comma delimited list of fabrics to export", argumentName="products", defaultValue="all fabrics")
          public String fabrics;
          @CLIArgument(description="Comma delimited list of products to export", argumentName="products", defaultValue="all products")
          public String products;
          @CLIArgument(description="File to export to, in csv format", argumentName="filename", defaultValue="stdout")
          public String outfile;
        }
    
        public static void main(String[] args) {
          Args cliArgs = new CLIArgumentParser("ExportTopology", Args.class).startup(args);
          if (cliArgs == null) {
            // bad command line, exit.
            return;
          }
          // ...
        }
      }
     
    The usage information generated by this class is:
       usage: ExportTopology [--fabrics ] [--help] [--outfile ] --path  [--products ]
        --fabrics     Comma delimited list of fabrics to export(optional; default=all fabrics)
        --help                  Show usage information
        --outfile     File to export to, in csv format(optional; default=stdout)
        --path        Path to topology files (required)
        --products    Comma delimited list of products to export(optional; default=all products)
     
    • Constructor Summary

      Constructors 
      Constructor Description
      CLIArgumentParser​(java.lang.String applicationName, java.lang.Class<T> klass)
      Create a new parser for the given application and argument class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void logConfiguredValues​(T args)
      Log the configured values.
      T parse​(java.lang.String[] args)
      Parse the given command line and return the arguments, or null if the arguments are invalid.
      CLIArgumentParser<T> redirect​(java.io.PrintWriter out, java.io.PrintWriter err)  
      void showUsage​(boolean verbose)
      Display command line usage information.
      void showUsage​(java.lang.String error)
      Display an error message plus command line usage information.
      T startup​(java.lang.String[] args)
      Parse the given command line.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CLIArgumentParser

        public CLIArgumentParser​(java.lang.String applicationName,
                                 java.lang.Class<T> klass)
        Create a new parser for the given application and argument class. The argument class should have one or more public fields, annotated with the CLIArgument or CLIOption annotation.
        Parameters:
        applicationName - name of the application, used in usage messages
        klass - class used for arguments
    • Method Detail

      • startup

        public T startup​(java.lang.String[] args)
        Parse the given command line. If the command line is legal, the options will be printed to the log and returned. If the command line is invalid, it will display command line usage and exit.

        In addition, the startup method understands the "--help" argument. If "--help" is given, startup will display usage information and return null.

        Parameters:
        args - command line arguments
        Returns:
        parsed arguments; null if the command line was invalid
      • logConfiguredValues

        public void logConfiguredValues​(T args)
        Log the configured values. An application will typically not call this method, since this information is logged automatically by the startup() method.
        Specified by:
        logConfiguredValues in interface CLIParser<T>
        Parameters:
        args - arguments to log
        See Also:
        startup(String[])
      • parse

        public T parse​(java.lang.String[] args)
        Parse the given command line and return the arguments, or null if the arguments are invalid. An application will typically not call this method, but will instead use startup(), which parses the command line and logs the configured values.
        Specified by:
        parse in interface CLIParser<T>
        Parameters:
        args - arguments to parse
        Returns:
        parsed arguments; null if the command line is invalid
        See Also:
        startup(String[])
      • redirect

        public CLIArgumentParser<T> redirect​(@Nonnull
                                             java.io.PrintWriter out,
                                             @Nonnull
                                             java.io.PrintWriter err)
      • showUsage

        public void showUsage​(boolean verbose)
        Display command line usage information. This is invoked automatically by startup() if the --help argument is present.
        Specified by:
        showUsage in interface CLIParser<T>
      • showUsage

        public void showUsage​(java.lang.String error)
        Display an error message plus command line usage information. This is invoked automatically by startup() if the command line is invalid.
        Specified by:
        showUsage in interface CLIParser<T>