Exemple #1
0
  private void validate(ParseState state) {
    CommandMetadata command = state.getCommand();
    if (command == null) {
      List<String> unparsedInput = state.getUnparsedInput();
      if (unparsedInput.isEmpty()) {
        throw new ParseCommandMissingException();
      } else {
        throw new ParseCommandUnrecognizedException(unparsedInput);
      }
    }

    ArgumentsMetadata arguments = command.getArguments();
    if (state.getParsedArguments().isEmpty() && arguments != null && arguments.isRequired()) {
      throw new ParseArgumentsMissingException(arguments.getTitle());
    }

    if (!state.getUnparsedInput().isEmpty()) {
      throw new ParseArgumentsUnexpectedException(state.getUnparsedInput());
    }

    if (state.getLocation() == Context.OPTION) {
      throw new ParseOptionMissingValueException(state.getCurrentOption().getTitle());
    }

    for (OptionMetadata option : command.getAllOptions()) {
      if (option.isRequired() && !state.getParsedOptions().containsKey(option)) {
        throw new ParseOptionMissingException(option.getOptions().iterator().next());
      }
    }
  }
Exemple #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;
  }
Exemple #3
0
  public C parse(C commandInstance, String... args) {
    Preconditions.checkNotNull(args, "args is null");

    Parser parser = new Parser();
    ParseState state = parser.parse(metadata, 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();

    return injectOptions(
        commandInstance,
        command.getAllOptions(),
        state.getParsedOptions(),
        command.getArguments(),
        state.getParsedArguments(),
        command.getMetadataInjections(),
        ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata));
  }
Exemple #4
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);
  }