Search in sources :

Example 1 with CommandLineCompleter

use of org.jboss.as.cli.CommandLineCompleter 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 2 with CommandLineCompleter

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

the class OperationRequestCompleter method completeHeaders.

private int completeHeaders(CommandContext ctx, ParsedCommandLine parsedCmd, OperationCandidatesProvider candidatesProvider, String buffer, int cursor, List<String> candidates) {
    final Map<String, OperationRequestHeader> headers = candidatesProvider.getHeaders(ctx);
    if (headers.isEmpty()) {
        return -1;
    }
    int result = buffer.length();
    if (parsedCmd.getLastHeaderName() != null) {
        if (buffer.endsWith(parsedCmd.getLastHeaderName())) {
            result = parsedCmd.getLastChunkIndex();
            for (String name : headers.keySet()) {
                if (name.equals(parsedCmd.getLastHeaderName())) {
                    result = completeHeader(headers, ctx, parsedCmd, buffer, cursor, candidates);
                    break;
                }
                if (!parsedCmd.hasHeader(name) && name.startsWith(parsedCmd.getLastHeaderName())) {
                    candidates.add(name);
                }
            }
        } else {
            result = completeHeader(headers, ctx, parsedCmd, buffer, cursor, candidates);
        }
    } else if (!parsedCmd.hasHeaders()) {
        candidates.addAll(headers.keySet());
    } else if (parsedCmd.endsOnHeaderSeparator()) {
        candidates.addAll(headers.keySet());
        for (ParsedOperationRequestHeader parsed : parsedCmd.getHeaders()) {
            candidates.remove(parsed.getName());
        }
    } else {
        final ParsedOperationRequestHeader lastParsedHeader = parsedCmd.getLastHeader();
        final OperationRequestHeader lastHeader = headers.get(lastParsedHeader.getName());
        if (lastHeader == null) {
            return -1;
        }
        final CommandLineCompleter headerCompleter = lastHeader.getCompleter();
        if (headerCompleter == null) {
            return -1;
        }
        result = headerCompleter.complete(ctx, buffer, cursor, candidates);
    }
    Collections.sort(candidates);
    return result;
}
Also used : CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter)

Example 3 with CommandLineCompleter

use of org.jboss.as.cli.CommandLineCompleter 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 4 with CommandLineCompleter

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

the class GenericTypeOperationHandler method getHandler.

private OperationCommand getHandler(CommandContext ctx, String op) throws CommandLineException {
    if (op == null) {
        if (writePropHandler == null) {
            writePropHandler = new WritePropertyHandler();
            Iterator<AttributeDescription> props = getNodeProperties(ctx);
            while (props.hasNext()) {
                final AttributeDescription prop = props.next();
                if (prop.isWriteAllowed()) {
                    CommandLineCompleter valueCompleter = null;
                    ArgumentValueConverter valueConverter = null;
                    if (propConverters != null) {
                        valueConverter = propConverters.get(prop.getName());
                    }
                    if (valueCompleters != null) {
                        valueCompleter = valueCompleters.get(prop.getName());
                    }
                    if (valueConverter == null) {
                        valueConverter = ArgumentValueConverter.DEFAULT;
                        final ModelType propType = prop.getType();
                        if (propType != null) {
                            if (ModelType.BOOLEAN == propType) {
                                if (valueCompleter == null) {
                                    valueCompleter = SimpleTabCompleter.BOOLEAN;
                                }
                            } else if (ModelType.STRING == propType) {
                                valueConverter = ArgumentValueConverter.NON_OBJECT;
                            } else if (prop.getName().endsWith("properties")) {
                                // TODO this is bad but can't rely on proper descriptions
                                valueConverter = ArgumentValueConverter.PROPERTIES;
                            } else if (ModelType.LIST == propType) {
                                if (asType(prop.getProperty(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                                    valueConverter = ArgumentValueConverter.PROPERTIES;
                                } else {
                                    valueConverter = ArgumentValueConverter.LIST;
                                }
                            }
                        }
                    }
                    final CommandArgument arg = new ArgumentWithValue(GenericTypeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
                    writePropHandler.addArgument(arg);
                }
            }
        }
        return writePropHandler;
    } else {
        if (customHandlers != null && customHandlers.containsKey(op)) {
            final OperationCommand opHandler = customHandlers.get(op);
            if (opHandler != null) {
                return opHandler;
            }
        }
        if (opHandlers != null) {
            OperationCommand opHandler = opHandlers.get(op);
            if (opHandler != null) {
                return opHandler;
            }
        }
        final ModelNode descr = getOperationDescription(ctx, op);
        if (opHandlers == null) {
            opHandlers = new HashMap<String, OperationCommand>();
        }
        final OpHandler opHandler = new OpHandler(op);
        opHandlers.put(op, opHandler);
        opHandler.addArgument(this.headers);
        if (descr != null && descr.has(Util.REQUEST_PROPERTIES)) {
            final List<Property> propList = descr.get(Util.REQUEST_PROPERTIES).asPropertyList();
            for (Property prop : propList) {
                final ModelNode propDescr = prop.getValue();
                CommandLineCompleter valueCompleter = null;
                ArgumentValueConverter valueConverter = null;
                if (propConverters != null) {
                    valueConverter = propConverters.get(prop.getName());
                }
                if (valueCompleters != null) {
                    valueCompleter = valueCompleters.get(prop.getName());
                }
                if (valueConverter == null) {
                    valueConverter = ArgumentValueConverter.DEFAULT;
                    if (propDescr.has(Util.TYPE)) {
                        final ModelType type = propDescr.get(Util.TYPE).asType();
                        if (ModelType.BOOLEAN == type) {
                            if (valueCompleter == null) {
                                valueCompleter = SimpleTabCompleter.BOOLEAN;
                            }
                        } else if (ModelType.STRING == type) {
                            valueConverter = ArgumentValueConverter.NON_OBJECT;
                        } else if (prop.getName().endsWith("properties")) {
                            // TODO this is bad but can't rely on proper descriptions
                            valueConverter = ArgumentValueConverter.PROPERTIES;
                        } else if (ModelType.LIST == type) {
                            if (propDescr.hasDefined(Util.VALUE_TYPE) && asType(propDescr.get(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                                valueConverter = ArgumentValueConverter.PROPERTIES;
                            } else {
                                valueConverter = ArgumentValueConverter.LIST;
                            }
                        }
                    }
                }
                final CommandArgument arg = new ArgumentWithValue(GenericTypeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
                opHandler.addArgument(arg);
            }
        }
        return opHandler;
    }
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) OperationCommand(org.jboss.as.cli.OperationCommand) ArgumentValueConverter(org.jboss.as.cli.ArgumentValueConverter) ArgumentWithValue(org.jboss.as.cli.impl.ArgumentWithValue) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 5 with CommandLineCompleter

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

the class MockOperationCandidatesProvider method getProperties.

@Override
public List<CommandArgument> getProperties(CommandContext ctx, String operationName, OperationRequestAddress address) {
    MockOperation operation = root.getOperation(operationName);
    if (operation == null) {
        return Collections.emptyList();
    }
    final List<MockOperationProperty> properties = operation.getProperties();
    final List<CommandArgument> result = new ArrayList<CommandArgument>(properties.size());
    for (final MockOperationProperty property : properties) {
        String name = property.getName();
        result.add(new CommandArgument() {

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

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

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

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

            @Override
            public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
                ParsedCommandLine args = ctx.getParsedCommandLine();
                if (isPresent(args)) {
                    return !isValueComplete(args);
                }
                return true;
            }

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

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

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

            @Override
            public boolean isValueRequired() {
                return property.isValueRequired();
            }

            @Override
            public CommandLineCompleter getValueCompleter() {
                return property.getPossibleValues() != null ? new SimpleTabCompleter(property.getPossibleValues()) : null;
            }
        });
    }
    return result;
}
Also used : SimpleTabCompleter(org.jboss.as.cli.handlers.SimpleTabCompleter) 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)

Aggregations

CommandLineCompleter (org.jboss.as.cli.CommandLineCompleter)11 CommandArgument (org.jboss.as.cli.CommandArgument)6 CommandFormatException (org.jboss.as.cli.CommandFormatException)4 Property (org.jboss.dmr.Property)4 ArrayList (java.util.ArrayList)3 CommandContext (org.jboss.as.cli.CommandContext)3 ModelNode (org.jboss.dmr.ModelNode)3 ArgumentValueConverter (org.jboss.as.cli.ArgumentValueConverter)2 ArgumentWithValue (org.jboss.as.cli.impl.ArgumentWithValue)2 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)2 ModelType (org.jboss.dmr.ModelType)2 List (java.util.List)1 CommandLineFormat (org.jboss.as.cli.CommandLineFormat)1 OperationCommand (org.jboss.as.cli.OperationCommand)1 MockCommandContext (org.jboss.as.cli.completion.mock.MockCommandContext)1 SimpleTabCompleter (org.jboss.as.cli.handlers.SimpleTabCompleter)1 ArgumentWithoutValue (org.jboss.as.cli.impl.ArgumentWithoutValue)1 Test (org.junit.Test)1