Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.cli;
  
  
  
  import static com.google.common.base.Preconditions.checkNotNull;
 import static java.lang.String.format;

A basic CLI command.
 
 public abstract class Command {
     private final String name;
     private final String description;

    
Create a new Command instance.

Parameters:
name the command name (must be unique for the service)
description the description of the command
 
     protected Command(String name,
                       String description) {
         this. = checkNotNull(name);
         this. = checkNotNull(description);
     }

    
Returns the command's name.

Returns:
the command's name
 
     public final String getName() {
         return ;
     }

    
Returns the command's description.

Returns:
the command's description
 
     public final String getDescription() {
         return ;
     }

    
Returns an empty org.apache.commons.cli.Options instance. Override this to allow your commands to parse command line arguments.

Returns:
an empty org.apache.commons.cli.Options instance
 
     public Options getOptions() {
         return new Options();
     }
 
     @SuppressWarnings("unchecked")
     final Options getOptionsWithHelp() {
         final Options options = new Options();
         final OptionGroup group = new OptionGroup();
         for (Option option : (Collection<Option>) getOptions().getOptions()) {
             group.addOption(option);
         }
         options.addOptionGroup(group);
         options.addOption("h""help"false"display usage information");
         return options;
     }

    
Executes when the user runs this specific command.

Parameters:
service the service to which the command belongs
params the command-line parameters of the invocation
Throws:
java.lang.Exception if something goes wrong
 
     protected abstract void run(AbstractService<?> service,
                                 CommandLine paramsthrows Exception;

    
Returns the usage syntax for the command.

Returns:
the usage syntax for the command
 
     protected String getSyntax() {
         return "[options]";
     }

    
Returns the usage string for the command, including the JAR file location, if possible.

Parameters:
klass the service class
Returns:
the usage string for the command
 
     protected String getUsage(Class<?> klass) {
         return format("%s %s %s"new JarLocation(klass), getName(), getSyntax());
     }

    
Execute the command.

Parameters:
service the service to which the command belongs
arguments the arguments passed to the command
Throws:
java.lang.Exception if something goes wrong
    public final void run(AbstractService<?> service,
                          String[] argumentsthrows Exception {
        final CommandLine cmdLine = new GnuParser().parse(getOptionsWithHelp(), checkNotNull(arguments));
        if (cmdLine.hasOption("help")) {
            printHelp(service.getClass());
        } else {
            run(checkNotNull(service), cmdLine);
        }
    }
    protected final void printHelp(Class<?> klass) {
        UsagePrinter.printCommandHelp(thisklass);
    }
    protected final void printHelp(String messageClass<?> klass) {
        UsagePrinter.printCommandHelp(thisklassmessage);
    }
New to GrepCode? Check out our FAQ X