Search in sources :

Example 11 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class ArgumentWithoutValue method canAppearNext.

@Override
public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
    if (!access.isSatisfied(ctx)) {
        return false;
    }
    ParsedCommandLine args = ctx.getParsedCommandLine();
    if (exclusive) {
        final Set<String> propertyNames = args.getPropertyNames();
        if (propertyNames.isEmpty()) {
            final List<String> values = args.getOtherProperties();
            if (values.isEmpty()) {
                return true;
            }
            if (index == -1) {
                return false;
            }
            return !(index == 0 && values.size() == 1);
        }
        if (propertyNames.size() != 1) {
            return false;
        }
        if (args.getLastParsedPropertyName() == null) {
            return false;
        }
        final List<String> values = args.getOtherProperties();
        if (!values.isEmpty()) {
            return false;
        }
        // The argument is already there, don't add it.
        if (fullName.equals(args.getLastParsedPropertyName())) {
            return false;
        }
        return fullName.startsWith(args.getLastParsedPropertyName()) || (shortName != null && shortName.startsWith(args.getLastParsedPropertyName()));
    }
    if (isPresent(args)) {
        // An argument without value has no value
        return false;
    }
    for (CommandArgument arg : cantAppearAfter) {
        if (arg.isPresent(args)) {
            return false;
        }
    }
    if (requiredPreceding != null) {
        for (CommandArgument arg : requiredPreceding) {
            if (arg.isPresent(args)) {
                return true;
            }
        }
        return false;
    }
    return true;
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine)

Example 12 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class OperationRequestCompleter method completeProperties.

private int completeProperties(CommandContext ctx, ParsedCommandLine parsedCmd, OperationCandidatesProvider candidatesProvider, String buffer, List<String> candidates) {
    // Invalid case of no operation name provided.
    if (!parsedCmd.hasOperationName()) {
        return -1;
    }
    // Retrieve all operation arguments from remote server.
    final Collection<CommandArgument> allArgs = candidatesProvider.getProperties(ctx, parsedCmd.getOperationName(), parsedCmd.getAddress());
    // No argument/option for this operation/command.
    if (allArgs.isEmpty()) {
        return completeNoProperties(parsedCmd, buffer, candidates);
    }
    // The lastcharacter typed is '!', we need a property name.
    if (parsedCmd.endsOnNotOperator()) {
        return completeImplicitValueProperties(ctx, buffer, allArgs, candidates);
    }
    // No properties have been already set.
    if (!parsedCmd.hasProperties()) {
        return completeNoPropertiesProvided(ctx, buffer, allArgs, candidates);
    }
    // We should complete at the end of the input.
    int result = buffer.length();
    // chunk is the last piece of text the user typed.
    String chunk = null;
    // a property/option value or an argument value.
    if (!parsedCmd.endsOnPropertySeparator()) {
        final String argName = parsedCmd.getLastParsedPropertyName();
        final String argValue = parsedCmd.getLastParsedPropertyValue();
        // Complete a value.
        if (argValue != null || parsedCmd.endsOnPropertyValueSeparator()) {
            result = parsedCmd.getLastChunkIndex();
            if (parsedCmd.endsOnPropertyValueSeparator()) {
                // it enters on '='
                ++result;
            }
            chunk = argValue;
            CommandLineCompleter valueCompleter = null;
            if (argName != null) {
                // Retrieve the completer based on name
                valueCompleter = getValueCompleter(ctx, allArgs, argName);
            } else {
                // retrieve the completer based on argument index.
                valueCompleter = getValueCompleter(ctx, allArgs, parsedCmd.getOtherProperties().size() - 1);
            }
            // No value completer for this property.
            if (valueCompleter == null) {
                return completeNoValueCompleter(ctx, parsedCmd, allArgs, argName, buffer, candidates);
            } else {
                // Complete with a value Completer.
                return completeWithValueCompleter(ctx, parsedCmd, allArgs, argName, buffer, candidates, chunk, result, valueCompleter);
            }
        } else {
            // Will complete possibly a name.
            chunk = argName;
            // Name completion is inlined at the begining of the name, not at
            // the end of the user input.
            result = parsedCmd.getLastChunkIndex();
        }
    }
    // unammed argument value (apply to commands only).
    return completeArgumentValueAndPropertyNames(ctx, parsedCmd, allArgs, candidates, chunk, result);
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter)

Example 13 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class OperationRequestCompleter method completeNoPropertiesProvided.

private int completeNoPropertiesProvided(CommandContext ctx, String buffer, Collection<CommandArgument> allArgs, List<String> candidates) {
    try {
        boolean needNeg = false;
        for (CommandArgument arg : allArgs) {
            if (arg.canAppearNext(ctx)) {
                // In this case call the value completer.
                if (arg.getIndex() >= 0) {
                    final CommandLineCompleter valCompl = arg.getValueCompleter();
                    if (valCompl != null) {
                        valCompl.complete(ctx, "", 0, candidates);
                    // Values have been added as candidate.
                    // If there are some options to propose, they will be mixed
                    // with the values. That only applies to commands.
                    }
                } else {
                    candidates.add(arg.getDecoratedName());
                    // doesn't need a value (is a boolean).
                    if (!arg.isValueRequired()) {
                        needNeg = true;
                    }
                }
            }
        }
        if (needNeg) {
            candidates.add(Util.NOT_OPERATOR);
        }
        Collections.sort(candidates);
        return buffer.length();
    } catch (CommandFormatException e) {
        return -1;
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter)

Aggregations

CommandArgument (org.jboss.as.cli.CommandArgument)13 CommandFormatException (org.jboss.as.cli.CommandFormatException)8 CommandLineCompleter (org.jboss.as.cli.CommandLineCompleter)6 ArrayList (java.util.ArrayList)4 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)4 ModelNode (org.jboss.dmr.ModelNode)4 Property (org.jboss.dmr.Property)4 CommandContext (org.jboss.as.cli.CommandContext)3 CommandLineException (org.jboss.as.cli.CommandLineException)2 CommandLineFormat (org.jboss.as.cli.CommandLineFormat)2 OperationCommand (org.jboss.as.cli.OperationCommand)2 ArgumentWithoutValue (org.jboss.as.cli.impl.ArgumentWithoutValue)2 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)2 IOException (java.io.IOException)1 ArgumentValueConverter (org.jboss.as.cli.ArgumentValueConverter)1 MockCommandContext (org.jboss.as.cli.completion.mock.MockCommandContext)1 SimpleTabCompleter (org.jboss.as.cli.handlers.SimpleTabCompleter)1 ArgumentWithValue (org.jboss.as.cli.impl.ArgumentWithValue)1 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)1 Node (org.jboss.as.cli.operation.OperationRequestAddress.Node)1