Search in sources :

Example 6 with CommandLineCompleter

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

the class DefaultOperationCandidatesProviderTestCase method testAttributeValueCompleter.

@Test
public void testAttributeValueCompleter() throws Exception {
    CommandContext ctx = CommandContextFactory.getInstance().newCommandContext();
    DefaultOperationRequestAddress address = new DefaultOperationRequestAddress();
    {
        Property prop = new Property("arg", ModelNode.fromString(list_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "", 0, candidates);
        assertEquals(Arrays.asList("["), candidates);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(map_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "", 0, candidates);
        assertEquals(Arrays.asList("{"), candidates);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(obj_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "{", 0, candidates);
        assertEquals(Arrays.asList("prop1", "prop2"), candidates);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(boolean_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "", 0, candidates);
        assertEquals(Arrays.asList("false", "true"), candidates);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(allowed_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "", 0, candidates);
        assertEquals(Arrays.asList("ALL", "FINER", "FINEST"), candidates);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(string_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        assertEquals(null, completer);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(bytes_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "", 0, candidates);
        assertEquals(Arrays.asList("bytes{"), candidates);
    }
    {
        Property prop = new Property("arg", ModelNode.fromString(bytes_content));
        CommandLineCompleter completer = DefaultOperationCandidatesProvider.getCompleter(prop, ctx, address);
        List<String> candidates = new ArrayList<>();
        completer.complete(ctx, "bytes", 0, candidates);
        assertEquals(Arrays.asList("bytes{"), candidates);
    }
}
Also used : MockCommandContext(org.jboss.as.cli.completion.mock.MockCommandContext) CommandContext(org.jboss.as.cli.CommandContext) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.jboss.dmr.Property) Test(org.junit.Test)

Example 7 with CommandLineCompleter

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

the class DefaultOperationCandidatesProvider method getCompleter.

private CommandLineCompleter getCompleter(final Map<String, CommandLineCompleterFactory> globalOpProps, final Property prop, CommandContext ctx, String operationName, OperationRequestAddress address) throws IllegalArgumentException {
    CommandLineCompleter propCompleter = null;
    final CommandLineCompleterFactory factory = globalOpProps == null ? null : globalOpProps.get(prop.getName());
    if (factory != null) {
        propCompleter = factory.createCompleter(ctx, address);
    }
    if (propCompleter == null) {
        propCompleter = getCompleter(prop, ctx, address);
    }
    return propCompleter;
}
Also used : CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter)

Example 8 with CommandLineCompleter

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

the class ResourceCompositeOperationHandler method getOperationArguments.

protected Map<String, ArgumentWithValue> getOperationArguments(CommandContext ctx, String opName) throws CommandLineException {
    Map<String, ArgumentWithValue> args = opArgs.get(opName);
    if (args != null) {
        return args;
    }
    final ModelNode descr = getOperationDescription(ctx, opName);
    if (descr.has(Util.REQUEST_PROPERTIES)) {
        args = new HashMap<String, ArgumentWithValue>();
        final List<Property> propList = descr.get(Util.REQUEST_PROPERTIES).asPropertyList();
        for (Property prop : propList) {
            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 = getType(prop.getValue());
                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(descr.get(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                            valueConverter = ArgumentValueConverter.PROPERTIES;
                        } else {
                            valueConverter = ArgumentValueConverter.LIST;
                        }
                    }
                }
            }
            final ArgumentWithValue arg = new ArgumentWithValue(ResourceCompositeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
            args.put(arg.getFullName(), arg);
        }
    } else {
        args = Collections.emptyMap();
    }
    opArgs.put(opName, args);
    return args;
}
Also used : ArgumentWithValue(org.jboss.as.cli.impl.ArgumentWithValue) ArgumentValueConverter(org.jboss.as.cli.ArgumentValueConverter) ModelType(org.jboss.dmr.ModelType) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 9 with CommandLineCompleter

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

the class OperationRequestCompleter method completeHeader.

private int completeHeader(Map<String, OperationRequestHeader> headers, CommandContext ctx, ParsedCommandLine parsedCmd, String buffer, int cursor, List<String> candidates) {
    final OperationRequestHeader header = headers.get(parsedCmd.getLastHeaderName());
    if (header == null) {
        return -1;
    }
    final CommandLineCompleter headerCompleter = header.getCompleter();
    if (headerCompleter == null) {
        return -1;
    }
    int valueResult = headerCompleter.complete(ctx, buffer.substring(parsedCmd.getLastChunkIndex()), cursor, candidates);
    if (valueResult < 0) {
        return -1;
    }
    return parsedCmd.getLastChunkIndex() + valueResult;
}
Also used : CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter)

Example 10 with CommandLineCompleter

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

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