Example #1
0
  private Cli(
      String name,
      String description,
      TypeConverter typeConverter,
      Class<? extends C> defaultCommand,
      Iterable<Class<? extends C>> defaultGroupCommands,
      Iterable<GroupBuilder<C>> groups) {
    Preconditions.checkNotNull(name, "name is null");
    Preconditions.checkNotNull(typeConverter, "typeConverter is null");

    CommandMetadata defaultCommandMetadata = null;
    if (defaultCommand != null) {
      defaultCommandMetadata = MetadataLoader.loadCommand(defaultCommand);
    }

    final List<CommandMetadata> allCommands = new ArrayList<CommandMetadata>();

    List<CommandMetadata> defaultCommandGroup =
        Lists.newArrayList(MetadataLoader.loadCommands(defaultGroupCommands));

    // currentlly the default command is required to be in the commands list. If that changes, we'll
    // need to add it here and add checks for existence
    allCommands.addAll(defaultCommandGroup);

    List<CommandGroupMetadata> commandGroups =
        Lists.newArrayList(
            Iterables.transform(
                groups,
                new Function<GroupBuilder<C>, CommandGroupMetadata>() {
                  public CommandGroupMetadata apply(GroupBuilder<C> group) {
                    CommandMetadata groupDefault = MetadataLoader.loadCommand(group.defaultCommand);
                    List<CommandMetadata> groupCommands =
                        MetadataLoader.loadCommands(group.commands);

                    // currentlly the default command is required to be in the commands list. If
                    // that changes, we'll need to add it here and add checks for existence
                    allCommands.addAll(groupCommands);

                    return MetadataLoader.loadCommandGroup(
                        group.name, group.description, groupDefault, groupCommands);
                  }
                }));

    // add commands to groups based on the value of groups in the @Command annotations
    // rather than change the entire way metadata is loaded, I figured just post-processing was an
    // easier, yet uglier, way to go
    MetadataLoader.loadCommandsIntoGroupsByAnnotation(
        allCommands, commandGroups, defaultCommandGroup);

    this.metadata =
        MetadataLoader.loadGlobal(
            name,
            description,
            defaultCommandMetadata,
            ImmutableList.copyOf(defaultCommandGroup),
            ImmutableList.copyOf(commandGroups));
  }
Example #2
0
  public C parse(C commandInstance, String... args) {
    Preconditions.checkNotNull(args, "args is null");

    Parser parser = new Parser(metadata);
    ParseState state = parser.parse(args);

    CommandMetadata command = MetadataLoader.loadCommand(commandInstance.getClass());

    state = state.withCommand(command);

    validate(state);

    ImmutableMap.Builder<Class<?>, Object> bindings =
        ImmutableMap.<Class<?>, Object>builder().put(GlobalMetadata.class, metadata);

    if (state.getGroup() != null) {
      bindings.put(CommandGroupMetadata.class, state.getGroup());
    }

    bindings.put(CommandMetadata.class, command);

    C c =
        (C)
            ParserUtil.injectOptions(
                commandInstance,
                command.getAllOptions(),
                state.getParsedOptions(),
                command.getArguments(),
                state.getParsedArguments(),
                command.getMetadataInjections(),
                bindings.build());

    return c;
  }