com.sampullara.cli.Args类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(88)

本文整理了Java中com.sampullara.cli.Args类的一些代码示例,展示了Args类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Args类的具体详情如下:
包路径:com.sampullara.cli.Args
类名称:Args

Args介绍

暂无

代码示例

代码示例来源:origin: spullara/mustache.java

public static void main(String[] args) throws Exception {
 try {
  Args.parse(Handlebar.class, args);
 } catch (IllegalArgumentException e) {
  Args.usage(Handlebar.class);
  System.exit(1);

代码示例来源:origin: spullara/cli-parser

public static List<String> parse(Object target, String[] args) {
  return parse(target, args, true);
}

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

/**
 * Generate usage information based on the target annotations.
 *
 * @param target An instance or class.
 */
public static void usage(Object target) {
  usage(System.err, target);
}

代码示例来源:origin: com.google.code.cli-parser/cli

private static void fieldUsage(Object target, Field field) {
  Argument argument = field.getAnnotation(Argument.class);
  if (argument != null) {
    String name = getName(argument, field);
    String alias = getAlias(argument);
    String prefix = argument.prefix();
    String delimiter = argument.delimiter();
    String description = argument.description();
    makeAccessible(field);
    try {
      Object defaultValue = field.get(target);
      Class type = field.getType();
      propertyUsage(prefix, name, alias, type, delimiter, description, defaultValue);
    } catch (IllegalAccessException e) {
      throw new IllegalArgumentException("Could not use thie field " + field + " as an argument field", e);
    }
  }
}

代码示例来源:origin: spullara/cli-parser

if (arg.startsWith(prefix)) {
  Object value;
  String name = getName(argument, field);
  String alias = getAlias(argument);
  arg = arg.substring(prefix.length());
  Class<?> type = field.getType();
  if (arg.equals(name) || (alias != null && arg.equals(alias))) {
    i.remove();
    value = consumeArgumentValue(type, argument, i);
    if (!set) {
      setField(type, field, target, value, delimiter);
    } else {
      addArgument(type, field, target, value, delimiter);
String name = getName(argument, field);
throw new IllegalArgumentException("You must set argument " + name);

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

if (arg.startsWith(prefix)) {
  Object value;
  String name = getName(argument, property);
  String alias = getAlias(argument);
  arg = arg.substring(prefix.length());
  Class<?> type = property.getPropertyType();
  if (arg.equals(name) || (alias != null && arg.equals(alias))) {
    i.remove();
    value = consumeArgumentValue(type, argument, i);
    if (!set) {
      setProperty(type, property, target, value, delimiter);
    } else {
      addPropertyArgument(type, property, target, value, delimiter);
String name = getName(argument, property);
throw new IllegalArgumentException("You must set argument " + name);

代码示例来源:origin: spullara/cli-parser

private static void processField(Object target, Field field, Properties arguments) {
  Argument argument = field.getAnnotation(Argument.class);
  if (argument != null) {
    String name = Args.getName(argument, field);
    String alias = Args.getAlias(argument);
    Class type = field.getType();
    Object value = arguments.get(name);
    if (value == null && alias != null) {
      value = arguments.get(alias);
    }
    if (value != null) {
      if (type == Boolean.TYPE || type == Boolean.class) {
        value = true;
      }
      Args.setField(type, field, target, value, argument.delimiter());
    } else {
      if (argument.required()) {
        throw new IllegalArgumentException("You must set argument " + name);
      }
    }
  }
}

代码示例来源:origin: spullara/cli-parser

private static void processProperty(Object target, PropertyDescriptor property, Properties arguments) {
    Method writeMethod = property.getWriteMethod();
    if (writeMethod != null) {
      Argument argument = writeMethod.getAnnotation(Argument.class);
      if (argument != null) {
        String name = Args.getName(argument, property);
        String alias = Args.getAlias(argument);
        Object value = arguments.get(name);
        if (value == null && alias != null) {
          value = arguments.get(alias);
        }
        if (value != null) {
          Class type = property.getPropertyType();
          if (type == Boolean.TYPE || type == Boolean.class) {
            value = true;
          }
          Args.setProperty(type, property, target, value, argument.delimiter());
        } else {
          if (argument.required()) {
            throw new IllegalArgumentException("You must set argument " + name);
          }
        }
      }
    }
  }
}

代码示例来源:origin: com.google.code.cli-parser/cli

private static void propertyUsage(Object target, PropertyDescriptor field) {
  Method writeMethod = field.getWriteMethod();
  if (writeMethod != null) {
    Argument argument = writeMethod.getAnnotation(Argument.class);
    if (argument != null) {
      String name = getName(argument, field);
      String alias = getAlias(argument);
      String prefix = argument.prefix();
      String delimiter = argument.delimiter();
      String description = argument.description();
      try {
        Method readMethod = field.getReadMethod();
        Object defaultValue;
        if (readMethod == null) {
          defaultValue = null;
        } else {
          defaultValue = readMethod.invoke(target, (Object[]) null);
        }
        Class type = field.getPropertyType();
        propertyUsage(prefix, name, alias, type, delimiter, description, defaultValue);
      } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("Could not use thie field " + field + " as an argument field", e);
      } catch (InvocationTargetException e) {
        throw new IllegalArgumentException("Could not get default value for " + field, e);
      }
    }
  }
}

代码示例来源:origin: com.google.code.cli-parser/cli

/**
 * Generate usage information based on the target annotations.
 *
 * @param target An instance or class.
 */
public static void usage(Object target) {
  Class clazz;
  if (target instanceof Class) {
    clazz = (Class) target;
  } else {
    clazz = target.getClass();
  }
  System.err.println("Usage: " + clazz.getName());
  for (Field field : clazz.getDeclaredFields()) {
    fieldUsage(target, field);
  }
  try {
    BeanInfo info = Introspector.getBeanInfo(clazz);
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
      propertyUsage(target, pd);
    }
  } catch (IntrospectionException e) {
    // If its not a JavaBean we ignore it
  }
}

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

private static Object getValue(Class<?> type, Object value, String delimiter) throws NoSuchMethodException {
  if (type != String.class && type != Boolean.class && type != Boolean.TYPE) {
    String string = (String) value;
    if (type.isArray()) {
      String[] strings = string.split(delimiter);
      type = type.getComponentType();
      if (type == String.class) {
        value = strings;
      } else {
        Object[] array = (Object[]) Array.newInstance(type, strings.length);
        for (int i = 0; i < array.length; i++) {
          array[i] = createValue(type, strings[i]);
        }
        value = array;
      }
    } else {
      value = createValue(type, string);
    }
  }
  return value;
}

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

public static List<String> parse(Object target, String[] args) {
  return parse(target, args, true);
}

代码示例来源:origin: spullara/cli-parser

/**
 * Generate usage information based on the target annotations.
 *
 * @param target An instance or class.
 */
public static void usage(Object target) {
  usage(System.err, target);
}

代码示例来源:origin: spullara/cli-parser

private static void fieldUsage(PrintStream errStream, Object target, Field field) {
  Argument argument = field.getAnnotation(Argument.class);
  if (argument != null) {
    String name = getName(argument, field);
    String alias = getAlias(argument);
    String prefix = argument.prefix();
    String delimiter = argument.delimiter();
    String description = argument.description();
    Class<? extends Callable<List<String>>> valuesProvider = argument.valuesProvider();
    makeAccessible(field);
    try {
      Object defaultValue = field.get(target);
      Class<?> type = field.getType();
      propertyUsage(errStream, prefix, name, alias, type, delimiter, description, defaultValue, getPossibleValues(type, valuesProvider));
    } catch (IllegalAccessException e) {
      throw new IllegalArgumentException("Could not use this field " + field + " as an argument field", e);
    }
  }
}

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

if (arg.startsWith(prefix)) {
  Object value;
  String name = getName(argument, field);
  String alias = getAlias(argument);
  arg = arg.substring(prefix.length());
  Class<?> type = field.getType();
  if (arg.equals(name) || (alias != null && arg.equals(alias))) {
    i.remove();
    value = consumeArgumentValue(type, argument, i);
    if (!set) {
      setField(type, field, target, value, delimiter);
    } else {
      addArgument(type, field, target, value, delimiter);
String name = getName(argument, field);
throw new IllegalArgumentException("You must set argument " + name);

代码示例来源:origin: spullara/cli-parser

if (arg.startsWith(prefix)) {
  Object value;
  String name = getName(argument, property);
  String alias = getAlias(argument);
  arg = arg.substring(prefix.length());
  Class<?> type = property.getPropertyType();
  if (arg.equals(name) || (alias != null && arg.equals(alias))) {
    i.remove();
    value = consumeArgumentValue(type, argument, i);
    if (!set) {
      setProperty(type, property, target, value, delimiter);
    } else {
      addPropertyArgument(type, property, target, value, delimiter);
String name = getName(argument, property);
throw new IllegalArgumentException("You must set argument " + name);

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

private static void processField(Object target, Field field, Properties arguments) {
  Argument argument = field.getAnnotation(Argument.class);
  if (argument != null) {
    String name = Args.getName(argument, field);
    String alias = Args.getAlias(argument);
    Class type = field.getType();
    Object value = arguments.get(name);
    if (value == null && alias != null) {
      value = arguments.get(alias);
    }
    if (value != null) {
      if (type == Boolean.TYPE || type == Boolean.class) {
        value = true;
      }
      Args.setField(type, field, target, value, argument.delimiter());
    } else {
      if (argument.required()) {
        throw new IllegalArgumentException("You must set argument " + name);
      }
    }
  }
}

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

private static void processProperty(Object target, PropertyDescriptor property, Properties arguments) {
    Method writeMethod = property.getWriteMethod();
    if (writeMethod != null) {
      Argument argument = writeMethod.getAnnotation(Argument.class);
      if (argument != null) {
        String name = Args.getName(argument, property);
        String alias = Args.getAlias(argument);
        Object value = arguments.get(name);
        if (value == null && alias != null) {
          value = arguments.get(alias);
        }
        if (value != null) {
          Class type = property.getPropertyType();
          if (type == Boolean.TYPE || type == Boolean.class) {
            value = true;
          }
          Args.setProperty(type, property, target, value, argument.delimiter());
        } else {
          if (argument.required()) {
            throw new IllegalArgumentException("You must set argument " + name);
          }
        }
      }
    }
  }
}

代码示例来源:origin: com.github.spullara.cli-parser/cli-parser

for (Class<?> currentClazz = clazz; currentClazz != null; currentClazz = currentClazz.getSuperclass()) {
  for (Field field : currentClazz.getDeclaredFields()) {
    fieldUsage(errStream, target, field);
  BeanInfo info = Introspector.getBeanInfo(clazz);
  for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
    propertyUsage(errStream, target, pd);

代码示例来源:origin: com.google.code.cli-parser/cli

private static Object getValue(Class type, Object value, String delimiter) throws NoSuchMethodException {
  if (type != String.class && type != Boolean.class && type != Boolean.TYPE) {
    if (type.isArray()) {
      String string = (String) value;
      String[] strings = string.split(delimiter);
      type = type.getComponentType();
      if (type == String.class) {
        value = strings;
      } else {
        Object[] array = (Object[]) Array.newInstance(type, strings.length);
        for (int i = 0; i < array.length; i++) {
          array[i] = createValue(type, strings[i]);
        }
        value = array;
      }
    } else {
      value = createValue(type, value);
    }
  }
  return value;
}

相关文章