Search in sources :

Example 1 with CommandArgument

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

the class CommandHandlerWithArguments method addArgument.

public void addArgument(CommandArgument arg) {
    checkNotNullParam("arg", arg);
    if (arg.getIndex() > -1) {
        maxArgumentIndex = arg.getIndex() > maxArgumentIndex ? arg.getIndex() : maxArgumentIndex;
    }
    checkNotNullParam("arg.getFullName()", arg.getFullName());
    switch(argList.size()) {
        case 0:
            argList = Collections.singletonList(arg);
            args = new HashMap<String, CommandArgument>();
            break;
        case 1:
            CommandArgument tmp = argList.get(0);
            argList = new ArrayList<CommandArgument>();
            argList.add(tmp);
        default:
            argList.add(arg);
    }
    args.put(arg.getFullName(), arg);
    if (arg.getShortName() != null) {
        args.put(arg.getShortName(), arg);
    }
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument)

Example 2 with CommandArgument

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

the class DefaultOperationCandidatesProvider method getProperties.

@Override
public List<CommandArgument> getProperties(CommandContext ctx, String operationName, OperationRequestAddress address) {
    final ModelControllerClient client = ctx.getModelControllerClient();
    if (client == null) {
        return Collections.emptyList();
    }
    /*        if(address.endsOnType()) {
            throw new IllegalArgumentException("The prefix isn't expected to end on a type.");
        }
*/
    final ModelNode request;
    final DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(address);
    try {
        builder.setOperationName(Util.READ_OPERATION_DESCRIPTION);
        builder.addProperty(Util.NAME, operationName);
        request = builder.buildRequest();
    } catch (OperationFormatException e1) {
        throw new IllegalStateException("Failed to build operation", e1);
    }
    List<CommandArgument> result;
    try {
        ModelNode outcome = client.execute(request);
        if (!Util.isSuccess(outcome)) {
            result = Collections.emptyList();
        } else {
            final ModelNode resultNode = outcome.get(Util.RESULT);
            if (!resultNode.isDefined()) {
                return Collections.emptyList();
            }
            final ModelNode reqProps = resultNode.get(Util.REQUEST_PROPERTIES);
            if (!reqProps.isDefined()) {
                return Collections.emptyList();
            }
            final List<Property> propList = reqProps.asPropertyList();
            result = getPropertiesFromPropList(propList, ctx, operationName, address);
        }
    } catch (Exception e) {
        result = Collections.emptyList();
    }
    return result;
}
Also used : ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) CommandFormatException(org.jboss.as.cli.CommandFormatException)

Example 3 with CommandArgument

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

the class DefaultOperationCandidatesProvider method getPropertiesFromPropList.

protected List<CommandArgument> getPropertiesFromPropList(List<Property> propList, CommandContext ctx, String operationName, OperationRequestAddress address) {
    final Map<String, CommandLineCompleterFactory> globalOpProps = globalOpPropCompleters.get(operationName);
    List<CommandArgument> result = new ArrayList<CommandArgument>(propList.size());
    String radical = null;
    if (ctx.getParsedCommandLine().getLastParsedPropertyValue() == null) {
        radical = ctx.getParsedCommandLine().getLastParsedPropertyName();
        // Check if the property is completely specified and is negated
        if (ctx.getParsedCommandLine().isLastPropertyNegated()) {
            for (Property prop : propList) {
                if (radical.equals(prop.getName())) {
                    radical = null;
                    break;
                }
            }
        }
    }
    final PropertyVisibility visibility = new PropertyVisibility(propList, ctx.getParsedCommandLine().getPropertyNames(), radical);
    for (final Property prop : propList) {
        final CommandLineCompleter completer = getCompleter(globalOpProps, prop, ctx, operationName, address);
        result.add(new CommandArgument() {

            final String argName = prop.getName();

            @Override
            public String getFullName() {
                return argName;
            }

            @Override
            public String getDecoratedName() {
                return visibility.getName(prop);
            }

            @Override
            public String getShortName() {
                return null;
            }

            @Override
            public int getIndex() {
                return -1;
            }

            @Override
            public boolean isPresent(ParsedCommandLine args) throws CommandFormatException {
                return args.hasProperty(argName);
            }

            @Override
            public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
                return visibility.canAppearNext(prop);
            }

            @Override
            public String getValue(ParsedCommandLine args) throws CommandFormatException {
                return args.getPropertyValue(argName);
            }

            @Override
            public String getValue(ParsedCommandLine args, boolean required) throws CommandFormatException {
                if (!isPresent(args)) {
                    throw new CommandFormatException("Property '" + argName + "' is missing required value.");
                }
                return args.getPropertyValue(argName);
            }

            @Override
            public boolean isValueComplete(ParsedCommandLine args) throws CommandFormatException {
                if (!isPresent(args)) {
                    return false;
                }
                if (argName.equals(args.getLastParsedPropertyName())) {
                    return false;
                }
                return true;
            }

            @Override
            public boolean isValueRequired() {
                boolean required = true;
                ModelNode mn = prop.getValue().get("type");
                if (mn != null) {
                    // No value required for boolean
                    required = mn.asType() != ModelType.BOOLEAN;
                }
                return required;
            }

            @Override
            public CommandLineCompleter getValueCompleter() {
                return completer;
            }
        });
    }
    return result;
}
Also used : CommandContext(org.jboss.as.cli.CommandContext) CommandArgument(org.jboss.as.cli.CommandArgument) ArrayList(java.util.ArrayList) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 4 with CommandArgument

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

the class OperationRequestCompleter method completeArgumentValueAndPropertyNames.

private int completeArgumentValueAndPropertyNames(CommandContext ctx, ParsedCommandLine parsedCmd, Collection<CommandArgument> allArgs, List<String> candidates, String chunk, int result) {
    // lastArg will be not null if its name is equals to the last typed one.
    CommandArgument lastArg = null;
    // All property present means proposing end of list instead of property
    // separator when the last property is a boolean one.
    boolean allPropertiesPresent = true;
    // e.g.: recursive and recursive-depth
    for (CommandArgument arg : allArgs) {
        try {
            if (arg.canAppearNext(ctx)) {
                allPropertiesPresent = false;
            } else if (arg.getFullName().equals(chunk)) {
                lastArg = arg;
            }
        } catch (CommandFormatException e) {
            return -1;
        }
    }
    boolean needNeg = false;
    // We must first check that we have no matches for option names
    // prior to call the argument (option with no value) value completers
    // Otherwise we can enter the case where value completer would complete
    // an option name. That is possible because value completer parsers are not strict.
    // for example: ls --hel<TAB> ==> --hel will be completed by the node-path completer
    // and this can give strange completion result.
    boolean optionMatch = false;
    for (CommandArgument arg : allArgs) {
        try {
            if (arg.canAppearNext(ctx)) {
                if (arg.getIndex() < 0) {
                    String argFullName = arg.getFullName();
                    if (chunk != null && argFullName.startsWith(chunk)) {
                        if (!parsedCmd.isLastPropertyNegated()) {
                            optionMatch = true;
                            break;
                        } else // We can only add candidates that are of type boolean
                        if (!arg.isValueRequired()) {
                            optionMatch = true;
                            break;
                        }
                    }
                }
            }
        } catch (CommandFormatException e) {
            return -1;
        }
    }
    for (CommandArgument arg : allArgs) {
        try {
            if (arg.canAppearNext(ctx)) {
                if (arg.getIndex() >= 0) {
                    if (optionMatch) {
                        continue;
                    }
                    CommandLineCompleter valCompl = arg.getValueCompleter();
                    if (valCompl != null) {
                        final String value = chunk == null ? "" : chunk;
                        valCompl.complete(ctx, value, value.length(), 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 {
                    String argFullName = arg.getFullName();
                    if (chunk == null || argFullName.startsWith(chunk)) {
                        /* The following complexity is due to cases like:
                                 recursive and recursive-depth. Both start with the same name
                                 but are of different types. Completion can't propose
                                 recursive-depth of !recursive has been typed.
                                 If the last property is not negated,
                                 we can add all properties with the same name.
                             */
                        if (!parsedCmd.isLastPropertyNegated()) {
                            candidates.add(arg.getDecoratedName());
                        } else // We can only add candidates that are of type boolean
                        {
                            if (!arg.isValueRequired()) {
                                candidates.add(arg.getDecoratedName());
                            }
                        }
                    }
                    // and this property is not already negated.
                    if (!arg.isValueRequired() && !parsedCmd.isLastPropertyNegated()) {
                        needNeg = true;
                    }
                }
            }
        } catch (CommandFormatException e) {
            return -1;
        }
    }
    // Propose not operator only after a property separator
    if (needNeg && parsedCmd.endsOnPropertySeparator()) {
        candidates.add(Util.NOT_OPERATOR);
    }
    if (lastArg != null) {
        if (lastArg.isValueRequired()) {
            candidates.add(lastArg.getFullName() + "=");
        } else if (lastArg instanceof ArgumentWithoutValue) {
            ArgumentWithoutValue argWithoutValue = (ArgumentWithoutValue) lastArg;
            // If the last argument is exclusive, no need to add any separator
            if (!argWithoutValue.isExclusive()) {
                // to make completion propose next argument
                if (!allPropertiesPresent) {
                    CommandLineFormat format = parsedCmd.getFormat();
                    if (format != null && format.getPropertySeparator() != null) {
                        candidates.add(lastArg.getFullName() + format.getPropertySeparator());
                    }
                }
            }
        } else {
            // We are completing implicit values for operation.
            CommandLineFormat format = parsedCmd.getFormat();
            // enough.
            if (!parsedCmd.isLastPropertyNegated()) {
                candidates.add("=" + Util.FALSE);
                result = recalculateResult(parsedCmd, candidates, chunk, result);
            }
            if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
                candidates.add(format.getPropertyListEnd());
                result = recalculateResult(parsedCmd, candidates, chunk, result);
                if (!allPropertiesPresent) {
                    candidates.add(format.getPropertySeparator());
                    result = recalculateResult(parsedCmd, candidates, chunk, result);
                }
            }
        }
    }
    if (candidates.isEmpty()) {
        if (chunk == null && !parsedCmd.endsOnSeparator()) {
            final CommandLineFormat format = parsedCmd.getFormat();
            if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
                candidates.add(format.getPropertyListEnd());
                result = recalculateResult(parsedCmd, candidates, chunk, result);
            }
        }
    } else {
        Collections.sort(candidates);
    }
    return result;
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) CommandLineFormat(org.jboss.as.cli.CommandLineFormat) ArgumentWithoutValue(org.jboss.as.cli.impl.ArgumentWithoutValue)

Example 5 with CommandArgument

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

the class OperationRequestCompleter method completeWithValueCompleter.

private int completeWithValueCompleter(CommandContext ctx, ParsedCommandLine parsedCmd, Collection<CommandArgument> allArgs, String argName, String buffer, List<String> candidates, String chunk, int result, CommandLineCompleter valueCompleter) {
    if (chunk == null) {
        // Complete with false if boolean
        for (CommandArgument arg : allArgs) {
            String argFullName = arg.getFullName();
            if (argFullName.equals(argName)) {
                if (!arg.isValueRequired()) {
                    candidates.add(Util.FALSE);
                    return buffer.length();
                }
            }
        }
    }
    // Call the value completer.
    // Completion is done with substituted line, so returned offset
    // is possibly wrong.
    // Some completers are reseting the CommandContext referenced parsed
    // command. We need to keep a local reference to substitution
    // to compute substitution index after completion occured.
    SubstitutedLine substitutions = parsedCmd.getSubstitutions();
    final String normalizedChunk = chunk == null ? "" : chunk;
    int valueResult = valueCompleter.complete(ctx, normalizedChunk, normalizedChunk.length(), candidates);
    // No proposition.
    if (valueResult < 0) {
        return valueResult;
    } else {
        // or the propertyListEnd if no more properties.
        if (suggestionEqualsUserEntry(candidates, chunk, valueResult)) {
            final CommandLineFormat format = parsedCmd.getFormat();
            if (format != null) {
                for (CommandArgument arg : allArgs) {
                    try {
                        if (arg.canAppearNext(ctx)) {
                            candidates.set(0, "" + format.getPropertySeparator());
                            return buffer.length();
                        }
                    } catch (CommandFormatException e) {
                        return -1;
                    }
                }
                // inline the end of properties.
                candidates.set(0, format.getPropertyListEnd());
                // at the end of the input.
                return buffer.length();
            }
        }
        // WFCORE-3190 ignore trailing spaces after the cursor position
        int trailOffset = substitutions.getSubstitued().substring(result).length() - normalizedChunk.length();
        return result + valueResult + trailOffset;
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) SubstitutedLine(org.jboss.as.cli.parsing.StateParser.SubstitutedLine) CommandLineFormat(org.jboss.as.cli.CommandLineFormat)

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