Exemplo n.º 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());
      }
    }
  }
Exemplo n.º 2
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));
  }