예제 #1
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;
  }
예제 #2
0
  public C parse(CommandFactory<C> commandFactory, Iterable<String> args) {
    Preconditions.checkNotNull(args, "args is null");

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

    if (state.getCommand() == null) {
      if (state.getGroup() != null) {
        state = state.withCommand(state.getGroup().getDefaultCommand());
      } else {
        state = state.withCommand(metadata.getDefaultCommand());
      }
    }

    validate(state);

    CommandMetadata command = state.getCommand();

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

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

    if (state.getCommand() != null) {
      bindings.put(CommandMetadata.class, state.getCommand());
    }

    return createInstance(
        command.getType(),
        command.getAllOptions(),
        state.getParsedOptions(),
        command.getArguments(),
        state.getParsedArguments(),
        command.getMetadataInjections(),
        bindings.build(),
        commandFactory);
  }