Class CommandLine

java.lang.Object
aQute.lib.getopt.CommandLine

public class CommandLine extends Object
Helps parsing command lines. This class takes target object, a primary command, and a list of arguments. It will then find the command in the target object. The method of this command must start with a "_" and take an parameter of Options type. Usually this is an interface that extends Options. The methods on this interface are options or flags (when they return boolean).
  • Field Details

  • Constructor Details

    • CommandLine

      public CommandLine(Reporter reporter)
  • Method Details

    • execute

      public String execute(Object target, String cmd, List<String> input) throws Exception
      Execute a command in a target object with a set of options and arguments and returns help text if something fails. Errors are reported.
      Throws:
      Exception
    • generateDocumentation

      public void generateDocumentation(Object target, Appendable out)
    • help

      private String help(Object target, String cmd, Class<? extends Options> type) throws Exception
      Throws:
      Exception
    • getOptions

      public <T extends Options> T getOptions(Class<T> specification, List<String> arguments) throws Exception
      Parse the options in a command line and return an interface that provides the options from this command line. This will parse up to (and including) -- or an argument that does not start with -
      Throws:
      Exception
    • getOptions

      private Map<String,Method> getOptions(Class<? extends Options> interf)
      Answer a list of the options specified in an options interface
    • assignOptionValue

      public void assignOptionValue(Map<String,Object> options, Method m, List<String> args, boolean last)
      Assign an option, must handle flags, parameters, and parameters that can happen multiple times.
      Parameters:
      options - The command line map
      args - the args input
      m - the selected method for this option
      last - if this is the last in a multi single character option
    • help

      public void help(Formatter f, Object target, String cmd, Class<? extends Options> specification)
      Provide a help text.
    • help

      private void help(Formatter f, Class<? extends Options> specification, String title)
    • getOption

      private CommandLine.Option getOption(String optionName, Method m)
    • getSynopsis

      private String getSynopsis(String cmd, Map<String,Method> options, Arguments patterns)
    • lastPart

      private static String lastPart(String name)
    • help

      public void help(Formatter f, Object target) throws Exception
      Show all commands in a target
      Throws:
      Exception
    • help

      public void help(Formatter f, Object target, String cmd)
      Show the full help for a given command
    • getCommands

      public Map<String,Method> getCommands(Object target)
      Parse a class and return a list of command names
      Parameters:
      target -
      Returns:
      command names
    • isMandatory

      private boolean isMandatory(Method m)
      Answer if the method is marked mandatory
    • isOption

      private boolean isOption(Method m)
      Parameters:
      m -
    • getTypeDescriptor

      private String getTypeDescriptor(Type type)
      Show a type in a nice way
    • getResult

      public Object getResult()
    • subCmd

      public String subCmd(Options opts, Object target) throws Exception
      Throws:
      Exception