CLI: Add a "help" command

This commit is contained in:
zzz
2022-12-21 07:36:03 -05:00
parent 4e174df746
commit 479ccd57df

View File

@@ -19,6 +19,7 @@ import net.i2p.CoreVersion;
public class CommandLine {
protected static final List<String> CLASSES = Arrays.asList(new String[] {
"help",
"freenet.support.CPUInformation.CPUID",
"net.i2p.CoreVersion",
"net.i2p.client.naming.LookupDest",
@@ -64,15 +65,33 @@ public class CommandLine {
/** will only return if command not found */
protected static void exec(String args[], List<String> classes) {
boolean help = false;
String cmd = args[0].toLowerCase(Locale.US);
if (cmd.equals("help")) {
if (args.length != 2)
return;
cmd = args[1].toLowerCase(Locale.US);
args[1] = "-?";
help = true;
}
for (String cls : classes) {
String ccmd = cls.substring(cls.lastIndexOf('.') + 1).toLowerCase(Locale.US);
if (cmd.equals(ccmd)) {
try {
Class<?> c = Class.forName(cls, true, ClassLoader.getSystemClassLoader());
if (help) {
// if it has a usage() method, call that instead
try {
Method usage = c.getDeclaredMethod("usage", (Class[]) null);
usage.setAccessible(true);
usage.invoke(null);
System.exit(0);
} catch (Exception e) {}
// else fall through to try main("-?")
}
Method main = c.getMethod("main", String[].class);
String[] cargs = new String[args.length - 1];
System.arraycopy(args, 1, cargs, 0, args.length - 1);
Class<?> c = Class.forName(cls, true, ClassLoader.getSystemClassLoader());
Method main = c.getMethod("main", String[].class);
main.invoke(null, (Object) cargs);
System.exit(0);
} catch (Exception e) {
@@ -100,6 +119,6 @@ public class CommandLine {
for (String cmd : cmds) {
System.err.println(" " + cmd);
}
System.err.println("Enter command for detailed help.");
System.err.println("Enter \"help command\" for detailed help.");
}
}