Display supported commands when subcommand is not provided
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import com.google.inject.Provider;
|
||||
import org.apache.sshd.server.CommandFactory.Command;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -37,8 +38,13 @@ public class DispatchCommand extends BaseCommand {
|
||||
|
||||
@Override
|
||||
public void start() throws IOException {
|
||||
int sp = commandLine.indexOf(' ');
|
||||
if (commandLine.isEmpty()) {
|
||||
usage();
|
||||
return;
|
||||
}
|
||||
|
||||
final String name, args;
|
||||
int sp = commandLine.indexOf(' ');
|
||||
if (0 < sp) {
|
||||
name = commandLine.substring(0, sp);
|
||||
while (Character.isWhitespace(commandLine.charAt(sp))) {
|
||||
@@ -78,4 +84,20 @@ public class DispatchCommand extends BaseCommand {
|
||||
exit.onExit(127);
|
||||
}
|
||||
}
|
||||
|
||||
private void usage() throws IOException, UnsupportedEncodingException {
|
||||
final StringBuilder usage = new StringBuilder();
|
||||
usage.append("usage: " + prefix + " COMMAND [ARGS]\n");
|
||||
usage.append("\n");
|
||||
usage.append("Available commands of " + prefix + " are:\n");
|
||||
for (Map.Entry<String, Provider<Command>> e : commands.entrySet()) {
|
||||
usage.append(" ");
|
||||
usage.append(e.getKey());
|
||||
usage.append("\n");
|
||||
}
|
||||
usage.append("\n");
|
||||
err.write(usage.toString().getBytes("UTF-8"));
|
||||
err.flush();
|
||||
exit.onExit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,10 @@ import org.apache.sshd.server.CommandFactory.Command;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Creates DispatchCommand using commands registered by {@link CommandModule}.
|
||||
@@ -76,7 +77,7 @@ public class DispatchCommandProvider implements Provider<DispatchCommand> {
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Provider<Command>> createMap() {
|
||||
final Map<String, Provider<Command>> m =
|
||||
new HashMap<String, Provider<Command>>();
|
||||
new TreeMap<String, Provider<Command>>();
|
||||
|
||||
for (final Binding<?> b : allCommands()) {
|
||||
final Annotation annotation = b.getKey().getAnnotation();
|
||||
@@ -88,7 +89,7 @@ public class DispatchCommandProvider implements Provider<DispatchCommand> {
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.unmodifiableMap(m);
|
||||
return Collections.unmodifiableMap(new LinkedHashMap(m));
|
||||
}
|
||||
|
||||
private static final TypeLiteral<Command> type =
|
||||
|
||||
Reference in New Issue
Block a user