Search in sources :

Example 1 with Configuration

use of com.twitter.common.args.apt.Configuration in project commons by twitter.

the class ArgScanner method parse.

/**
   * Applies the provided argument values to any {@literal @CmdLine} or {@literal @Positional}
   * {@code Arg} fields discovered on the classpath and accepted by the given {@code filter}.
   *
   * @param filter A predicate that selects or rejects scanned {@literal @CmdLine} fields for
   *    argument application.
   * @param args Argument values to map, parse, validate, and apply.
   * @return {@code true} if the given {@code args} were successfully applied to their corresponding
   *     {@link Arg} fields.
   * @throws ArgScanException if there was a problem loading {@literal @CmdLine} argument
   *    definitions
   * @throws IllegalArgumentException If the arguments provided are invalid based on the declared
   *    arguments found.
   */
public boolean parse(Predicate<Field> filter, Iterable<String> args) {
    Preconditions.checkNotNull(filter);
    ImmutableList<String> arguments = ImmutableList.copyOf(args);
    Configuration configuration = load();
    ArgsInfo argsInfo = Args.fromConfiguration(configuration, filter);
    return parse(argsInfo, arguments);
}
Also used : Configuration(com.twitter.common.args.apt.Configuration) ArgsInfo(com.twitter.common.args.Args.ArgsInfo)

Example 2 with Configuration

use of com.twitter.common.args.apt.Configuration in project commons by twitter.

the class Args method from.

/**
   * Loads arg info from the given sources in addition to the default compile-time configuration.
   *
   * @param filter A predicate to select fields with.
   * @param sources Classes or object instances to scan for {@link Arg} fields.
   * @return The args info describing all discovered {@link Arg args}.
   * @throws IOException If there was a problem loading the default Args configuration.
   */
public static ArgsInfo from(Predicate<Field> filter, Iterable<?> sources) throws IOException {
    Preconditions.checkNotNull(filter);
    Preconditions.checkNotNull(sources);
    Configuration configuration = Configuration.load();
    ArgsInfo staticInfo = Args.fromConfiguration(configuration, filter);
    final ImmutableSet.Builder<PositionalInfo<?>> positionalInfos = ImmutableSet.<PositionalInfo<?>>builder().addAll(staticInfo.getPositionalInfo().asSet());
    final ImmutableSet.Builder<OptionInfo<?>> optionInfos = ImmutableSet.<OptionInfo<?>>builder().addAll(staticInfo.getOptionInfos());
    for (Object source : sources) {
        Class<?> clazz = source instanceof Class ? (Class) source : source.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            if (filter.apply(field)) {
                boolean cmdLine = field.isAnnotationPresent(CmdLine.class);
                boolean positional = field.isAnnotationPresent(Positional.class);
                if (cmdLine && positional) {
                    throw new IllegalArgumentException("An Arg cannot be annotated with both @CmdLine and @Positional, found bad Arg " + "field: " + field);
                } else if (cmdLine) {
                    optionInfos.add(OptionInfo.createFromField(field, source));
                } else if (positional) {
                    positionalInfos.add(PositionalInfo.createFromField(field, source));
                }
            }
        }
    }
    @Nullable PositionalInfo<?> positionalInfo = Iterables.getOnlyElement(positionalInfos.build(), null);
    return new ArgsInfo(configuration, Optional.fromNullable(positionalInfo), optionInfos.build());
}
Also used : Configuration(com.twitter.common.args.apt.Configuration) Field(java.lang.reflect.Field) ImmutableSet(com.google.common.collect.ImmutableSet) Nullable(javax.annotation.Nullable)

Aggregations

Configuration (com.twitter.common.args.apt.Configuration)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArgsInfo (com.twitter.common.args.Args.ArgsInfo)1 Field (java.lang.reflect.Field)1 Nullable (javax.annotation.Nullable)1