Package org.springframework.shell.core.annotation

Examples of org.springframework.shell.core.annotation.CliCommand


    // The reflection could certainly be optimised, but it's good enough for now (and cached reflection
    // is unlikely to be noticeable to a human being using the CLI)
    for (final CommandMarker command : commands) {
      for (final Method method : command.getClass().getMethods()) {
        CliCommand cmd = method.getAnnotation(CliCommand.class);
        if (cmd != null) {
          // We have a @CliCommand.
          if (checkAvailabilityIndicators) {
            // Decide if this @CliCommand is available at this moment
            Boolean available = null;
            for (String value : cmd.value()) {
              MethodTarget mt = getAvailabilityIndicator(value);
              if (mt != null) {
                Assert.isNull(available, "More than one availability indicator is defined for '"
                    + method.toGenericString() + "'");
                try {
                  available = (Boolean) mt.getMethod().invoke(mt.getTarget());
                  // We should "break" here, but we loop over all to ensure no conflicting
                  // availability indicators are defined
                }
                catch (Exception e) {
                  available = false;
                }
              }
            }
            // Skip this @CliCommand if it's not available
            if (available != null && !available) {
              continue;
            }
          }

          for (String value : cmd.value()) {
            String remainingBuffer = isMatch(buffer, value, strictMatching);
            if (remainingBuffer != null) {
              result.add(new MethodTarget(method, command, remainingBuffer, value));
            }
          }
View Full Code Here


      // There is a single target of this method, so provide completion services for it
      MethodTarget methodTarget = targets.iterator().next();

      // Identify the command we're working with
      CliCommand cmd = methodTarget.getMethod().getAnnotation(CliCommand.class);
      Assert.notNull(cmd, "CliCommand unavailable for '" + methodTarget.getMethod().toGenericString() + "'");

      // Make a reasonable attempt at parsing the remainingBuffer
      Tokenizer tokenizer = null;
      try {
        tokenizer = new Tokenizer(methodTarget.getRemainingBuffer(), true);
      }
      catch (TokenizingException e) {
        // Make sure we don't crash the main shell loop just
        // because the user specified some option twice
        return -1;
      }
      catch (IllegalArgumentException e) {
        // Make sure we don't crash the main shell loop just
        // because the user specified some option twice
        return -1;
      }
      Map<String, String> options = tokenizer.getTokens();

      // Lookup arguments for this target
      Annotation[][] parameterAnnotations = methodTarget.getMethod().getParameterAnnotations();

      // If there aren't any parameters for the method, at least ensure they have typed the command properly
      if (parameterAnnotations.length == 0) {
        for (String value : cmd.value()) {
          if (buffer.startsWith(value) || value.startsWith(buffer)) {
            results.add(new Completion(value)); // no space at the end, as there's no need to continue the
                              // command further
          }
        }
        candidates.addAll(results);
        return 0;
      }

      // If they haven't specified any parameters yet, at least verify the command name is fully completed
      if (options.isEmpty()) {
        for (String value : cmd.value()) {
          if (value.startsWith(buffer)) {
            // They are potentially trying to type this command
            // We only need provide completion, though, if they failed to specify it fully
            if (!buffer.startsWith(value)) {
              // They failed to specify the command fully
View Full Code Here

        // Argument conversion time
        Annotation[][] parameterAnnotations = methodTarget.getMethod().getParameterAnnotations();
        if (parameterAnnotations.length > 0) {
          // Offer specified help
          CliCommand cmd = methodTarget.getMethod().getAnnotation(CliCommand.class);
          Assert.notNull(cmd, "CliCommand not found");

          for (String value : cmd.value()) {
            sb.append("Keyword:                   ").append(value).append(OsUtils.LINE_SEPARATOR);
          }

          sb.append("Description:               ").append(cmd.help()).append(OsUtils.LINE_SEPARATOR);

          for (Annotation[] annotations : parameterAnnotations) {
            CliOption cliOption = null;
            for (Annotation a : annotations) {
              if (a instanceof CliOption) {
                cliOption = (CliOption) a;

                for (String key : cliOption.key()) {
                  if ("".equals(key)) {
                    key = "** default **";
                  }
                  sb.append(" Keyword:                  ").append(key).append(OsUtils.LINE_SEPARATOR);
                }

                sb.append("   Help:                   ").append(cliOption.help())
                    .append(OsUtils.LINE_SEPARATOR);
                sb.append("   Mandatory:              ").append(cliOption.mandatory())
                    .append(OsUtils.LINE_SEPARATOR);
                sb.append("   Default if specified:   '").append(cliOption.specifiedDefaultValue())
                    .append("'").append(OsUtils.LINE_SEPARATOR);
                sb.append("   Default if unspecified: '").append(cliOption.unspecifiedDefaultValue())
                    .append("'").append(OsUtils.LINE_SEPARATOR);
                sb.append(OsUtils.LINE_SEPARATOR);
              }

            }
            Assert.notNull(cliOption, "CliOption not found for parameter '" + Arrays.toString(annotations)
                + "'");
          }
        }
        // Only a single argument, so default to the normal help operation
      }

      SortedSet<String> result = new TreeSet<String>(COMPARATOR);
      for (MethodTarget mt : matchingTargets) {
        CliCommand cmd = mt.getMethod().getAnnotation(CliCommand.class);
        if (cmd != null) {
          for (String value : cmd.value()) {
            if ("".equals(cmd.help())) {
              result.add("* " + value);
            }
            else {
              result.add("* " + value + " - " + cmd.help());
            }
          }
        }
      }

View Full Code Here

    synchronized (mutex) {
      SortedSet<String> result = new TreeSet<String>(COMPARATOR);
      for (Object o : commands) {
        Method[] methods = o.getClass().getMethods();
        for (Method m : methods) {
          CliCommand cmd = m.getAnnotation(CliCommand.class);
          if (cmd != null) {
            result.addAll(Arrays.asList(cmd.value()));
          }
        }
      }
      return result;
    }
View Full Code Here

TOP

Related Classes of org.springframework.shell.core.annotation.CliCommand

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.