Search in sources :

Example 1 with OperationRegistry

use of org.locationtech.geowave.core.cli.spi.OperationRegistry in project geowave by locationtech.

the class GeoWaveMain method prepRegistry.

/**
 * This adds the help and explain commands to have all operations as children, so the user can do
 * 'help command' or 'explain command'
 *
 * @return
 */
private static OperationRegistry prepRegistry() {
    final OperationRegistry registry = OperationRegistry.getInstance();
    final OperationEntry explainCommand = registry.getOperation(ExplainCommand.class);
    final OperationEntry helpCommand = registry.getOperation(HelpCommand.class);
    final OperationEntry topLevel = registry.getOperation(GeoWaveTopLevelSection.class);
    // top level, and will have all the same children as top level.
    for (final OperationEntry entry : topLevel.getChildren()) {
        if ((entry != helpCommand) && (entry != explainCommand)) {
            helpCommand.addChild(entry);
            explainCommand.addChild(entry);
        }
    }
    return registry;
}
Also used : OperationEntry(org.locationtech.geowave.core.cli.spi.OperationEntry) OperationRegistry(org.locationtech.geowave.core.cli.spi.OperationRegistry)

Example 2 with OperationRegistry

use of org.locationtech.geowave.core.cli.spi.OperationRegistry in project geowave by locationtech.

the class HelpCommandTest method testPrepare.

@Test
public void testPrepare() {
    final String[] args = { "help" };
    final OperationRegistry registry = OperationRegistry.getInstance();
    final OperationParser parser = new OperationParser(registry);
    final CommandLineOperationParams params = parser.parse(GeoWaveTopLevelSection.class, args);
    final HelpCommand helpcommand = new HelpCommand();
    helpcommand.prepare(params);
    assertEquals(false, params.isValidate());
    assertEquals(true, params.isAllowUnknown());
}
Also used : CommandLineOperationParams(org.locationtech.geowave.core.cli.parser.CommandLineOperationParams) OperationRegistry(org.locationtech.geowave.core.cli.spi.OperationRegistry) OperationParser(org.locationtech.geowave.core.cli.parser.OperationParser) Test(org.junit.Test)

Example 3 with OperationRegistry

use of org.locationtech.geowave.core.cli.spi.OperationRegistry in project geowave by locationtech.

the class OperationParserTest method testParseOperation.

@Test
public void testParseOperation() {
    final OperationEntry op1Entry = new OperationEntry(Op1.class);
    final OperationEntry op2Entry = new OperationEntry(Op2.class);
    op1Entry.addChild(op2Entry);
    final List<OperationEntry> entries = new ArrayList<>();
    entries.add(op1Entry);
    entries.add(op2Entry);
    final OperationParser parser = new OperationParser(new OperationRegistry(entries));
    final Op2 op2 = new Op2();
    parser.parse(op2, new String[] { "--username", "user", "--password", "blah" });
    Assert.assertEquals("blah", op2.args.passWord);
    Assert.assertEquals("user", op2.args.userName);
}
Also used : OperationEntry(org.locationtech.geowave.core.cli.spi.OperationEntry) ArrayList(java.util.ArrayList) OperationRegistry(org.locationtech.geowave.core.cli.spi.OperationRegistry) Test(org.junit.Test)

Example 4 with OperationRegistry

use of org.locationtech.geowave.core.cli.spi.OperationRegistry in project geowave by locationtech.

the class OperationParserTest method testParseTopLevel.

@Test
public void testParseTopLevel() {
    final OperationEntry op1Entry = new OperationEntry(Op1.class);
    final OperationEntry op2Entry = new OperationEntry(Op2.class);
    op1Entry.addChild(op2Entry);
    final List<OperationEntry> entries = new ArrayList<>();
    entries.add(op1Entry);
    entries.add(op2Entry);
    final OperationParser parser = new OperationParser(new OperationRegistry(entries));
    final CommandLineOperationParams params = parser.parse(Op1.class, new String[] { "op", "--username", "user", "--password", "blah" });
    final Op2 op2 = (Op2) params.getOperationMap().get("op");
    Assert.assertEquals("blah", op2.args.passWord);
    Assert.assertEquals("user", op2.args.userName);
}
Also used : OperationEntry(org.locationtech.geowave.core.cli.spi.OperationEntry) ArrayList(java.util.ArrayList) OperationRegistry(org.locationtech.geowave.core.cli.spi.OperationRegistry) Test(org.junit.Test)

Example 5 with OperationRegistry

use of org.locationtech.geowave.core.cli.spi.OperationRegistry in project geowave by locationtech.

the class HelpCommand method execute.

@Override
public void execute(final OperationParams inputParams) {
    final CommandLineOperationParams params = (CommandLineOperationParams) inputParams;
    final List<String> nameArray = new ArrayList<>();
    final OperationRegistry registry = OperationRegistry.getInstance();
    StringBuilder builder = new StringBuilder();
    Operation lastOperation = null;
    for (final Map.Entry<String, Operation> entry : params.getOperationMap().entrySet()) {
        if (entry.getValue() == this) {
            continue;
        }
        nameArray.add(entry.getKey());
        lastOperation = entry.getValue();
    }
    if (lastOperation == null) {
        lastOperation = registry.getOperation(GeoWaveTopLevelSection.class).createInstance();
    }
    if (lastOperation != null) {
        final String usage = lastOperation.usage();
        if (usage != null) {
            System.out.println(usage);
        } else {
            // This is done because if we don't, then JCommander will
            // consider the given parameters as the Default parameters.
            // It's also done so that we can parse prefix annotations
            // and special delegate processing.
            final JCommanderPrefixTranslator translator = new JCommanderPrefixTranslator();
            translator.addObject(lastOperation);
            final JCommanderTranslationMap map = translator.translate();
            map.createFacadeObjects();
            // Copy default parameters over for help display.
            map.transformToFacade();
            // Execute a prepare
            // Add processed objects
            final JCommander jc = new JCommander();
            for (final Object obj : map.getObjects()) {
                jc.addObject(obj);
            }
            final String programName = StringUtils.join(nameArray, " ");
            jc.setProgramName(programName);
            jc.getUsageFormatter().usage(builder);
            // Trim excess newlines.
            final String operations = builder.toString().trim();
            builder = new StringBuilder();
            builder.append(operations);
            builder.append("\n\n");
            // Add sub-commands
            final OperationEntry lastEntry = registry.getOperation(lastOperation.getClass());
            // Cast to list so we can sort it based on operation name.
            final List<OperationEntry> children = new ArrayList<>(lastEntry.getChildren());
            Collections.sort(children, getOperationComparator());
            if (children.size() > 0) {
                builder.append("  Commands:\n");
                for (final OperationEntry childEntry : children) {
                    // Get description annotation
                    final Parameters p = childEntry.getOperationClass().getAnnotation(Parameters.class);
                    // If not hidden, then output it.
                    if ((p == null) || !p.hidden()) {
                        builder.append(String.format("    %s%n", StringUtils.join(childEntry.getOperationNames(), ", ")));
                        if (p != null) {
                            final String description = p.commandDescription();
                            builder.append(String.format("      %s%n", description));
                        } else {
                            builder.append("      <no description>\n");
                        }
                        builder.append("\n");
                    }
                }
            }
            // Trim excess newlines.
            final String output = builder.toString().trim();
            System.out.println(output);
        }
    }
}
Also used : Parameters(com.beust.jcommander.Parameters) JCommanderTranslationMap(org.locationtech.geowave.core.cli.prefix.JCommanderTranslationMap) OperationEntry(org.locationtech.geowave.core.cli.spi.OperationEntry) ArrayList(java.util.ArrayList) Operation(org.locationtech.geowave.core.cli.api.Operation) DefaultOperation(org.locationtech.geowave.core.cli.api.DefaultOperation) GeowaveOperation(org.locationtech.geowave.core.cli.annotations.GeowaveOperation) JCommanderPrefixTranslator(org.locationtech.geowave.core.cli.prefix.JCommanderPrefixTranslator) JCommander(com.beust.jcommander.JCommander) CommandLineOperationParams(org.locationtech.geowave.core.cli.parser.CommandLineOperationParams) OperationRegistry(org.locationtech.geowave.core.cli.spi.OperationRegistry) Map(java.util.Map) JCommanderTranslationMap(org.locationtech.geowave.core.cli.prefix.JCommanderTranslationMap)

Aggregations

OperationRegistry (org.locationtech.geowave.core.cli.spi.OperationRegistry)7 Test (org.junit.Test)5 CommandLineOperationParams (org.locationtech.geowave.core.cli.parser.CommandLineOperationParams)4 OperationEntry (org.locationtech.geowave.core.cli.spi.OperationEntry)4 ArrayList (java.util.ArrayList)3 OperationParser (org.locationtech.geowave.core.cli.parser.OperationParser)3 JCommander (com.beust.jcommander.JCommander)1 Parameters (com.beust.jcommander.Parameters)1 File (java.io.File)1 Map (java.util.Map)1 Properties (java.util.Properties)1 GeowaveOperation (org.locationtech.geowave.core.cli.annotations.GeowaveOperation)1 DefaultOperation (org.locationtech.geowave.core.cli.api.DefaultOperation)1 Operation (org.locationtech.geowave.core.cli.api.Operation)1 JCommanderPrefixTranslator (org.locationtech.geowave.core.cli.prefix.JCommanderPrefixTranslator)1 JCommanderTranslationMap (org.locationtech.geowave.core.cli.prefix.JCommanderTranslationMap)1