Class Args

java.lang.Object
com.sampullara.cli.Args

public class Args extends Object
  • Field Details

  • Constructor Details

    • Args

      public Args()
  • Method Details

    • parseOrExit

      public static List<String> parseOrExit(Object target, String[] args)
      A convenience method for parsing and automatically producing error messages.
      Parameters:
      target - Either an instance or a class
      args - The arguments you want to parse and populate
      Returns:
      The list of arguments that were not consumed
    • parse

      public static List<String> parse(Object target, String[] args)
    • parse

      public static List<String> parse(Object target, String[] args, boolean failOnExtraFlags)
      Parse a set of arguments and populate the target with the appropriate values.
      Parameters:
      target - Either an instance or a class
      args - The arguments you want to parse and populate
      failOnExtraFlags - Throw an IllegalArgumentException if extra flags are present
      Returns:
      The list of arguments that were not consumed
    • processField

      private static void processField(Object target, Field field, List<String> arguments)
    • addArgument

      private static void addArgument(Class type, Field field, Object target, Object value, String delimiter)
    • addPropertyArgument

      private static void addPropertyArgument(Class type, PropertyDescriptor property, Object target, Object value, String delimiter)
    • processProperty

      private static void processProperty(Object target, PropertyDescriptor property, List<String> arguments)
    • usage

      public static void usage(Object target)
      Generate usage information based on the target annotations.
      Parameters:
      target - An instance or class.
    • usage

      public static void usage(PrintStream errStream, Object target)
      Generate usage information based on the target annotations.
      Parameters:
      errStream - A PrintStream to print the usage information to.
      target - An instance or class.
    • usage

      public static void usage(PrintStream errStream, Object target, Class mainClass)
      Generate usage information based on the target annotations.
      Parameters:
      errStream - A PrintStream to print the usage information to.
      target - An instance or class.
      mainClass - The class containing the main method
    • fieldUsage

      private static void fieldUsage(PrintStream errStream, Object target, Field field)
    • propertyUsage

      private static void propertyUsage(PrintStream errStream, Object target, PropertyDescriptor field)
    • propertyUsage

      private static void propertyUsage(PrintStream errStream, String prefix, String name, String alias, Class<?> type, String delimiter, String description, Object defaultValue, List<String> possibleValues)
    • getTypeName

      private static String getTypeName(Class<?> type)
    • getName

      static String getName(Argument argument, PropertyDescriptor property)
    • consumeArgumentValue

      private static Object consumeArgumentValue(Class<?> type, Argument argument, Iterator<String> i)
    • setProperty

      static void setProperty(Class<?> type, PropertyDescriptor property, Object target, Object value, String delimiter)
    • getAlias

      static String getAlias(Argument argument)
    • getName

      static String getName(Argument argument, Field field)
    • setField

      static void setField(Class<?> type, Field field, Object target, Object value, String delimiter)
    • getValue

      private static Object getValue(Class<?> type, Object value, String delimiter) throws NoSuchMethodException
      Throws:
      NoSuchMethodException
    • createValue

      private static Object createValue(Class<?> type, String valueAsString) throws NoSuchMethodException
      Throws:
      NoSuchMethodException
    • makeAccessible

      private static void makeAccessible(AccessibleObject ao)
    • getPossibleValues

      private static List<String> getPossibleValues(Class<?> type, Class<? extends Callable<List<String>>> provider)
    • getPossibleValuesViaProvider

      private static List<String> getPossibleValuesViaProvider(Class<? extends Callable<List<String>>> provider)
    • getEnumPossibleValues

      private static List<String> getEnumPossibleValues(Class<?> clazz)
    • getEnumPossibleValuesImpl

      private static <T extends Enum<T>> List<String> getEnumPossibleValuesImpl(Class<T> clazz)
    • byStaticMethodInvocation

      public static Args.ValueCreator byStaticMethodInvocation(Class<?> compatibleType, String methodName)
      Creates a Args.ValueCreator object able to create object assignable from given type, using a static one arg method which name is the the given one taking a String object as parameter
      Parameters:
      compatibleType - the base assignable for which this object will try to invoke the given method
      methodName - the name of the one arg method taking a String as parameter that will be used to built a new value
      Returns:
      null if the object could not be created, the value otherwise
    • registerValueCreator

      public static void registerValueCreator(Args.ValueCreator vc)
      Allows external extension of the valiue creators.
      Parameters:
      vc - another value creator to take into account for trying to create values
    • resetValueCreators

      public static void resetValueCreators()
      Cleanup of registered ValueCreators (mainly for tests)