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;
}
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());
}
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);
}
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);
}
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);
}
}
}
Aggregations