Add usage information to every command

This commit is contained in:
Johannes Frohnmeyer 2022-01-04 20:49:57 +01:00
parent 53ad3673d4
commit b3625d46ec
Signed by: Johannes
GPG Key ID: E76429612C2929F4
12 changed files with 55 additions and 43 deletions

View File

@ -4,15 +4,17 @@ import java.util.*;
public abstract class Command {
private final String help;
private final String usage;
private final List<String> aliases;
private final Collection<Command> subCommands;
public Command(String help, String... aliases) {
this(help, Arrays.asList(aliases), List.of());
public Command(String help, String usage, String... aliases) {
this(help, usage, Arrays.asList(aliases), List.of());
}
public Command(String help, List<String> aliases, List<Command> subCommands) {
public Command(String help, String usage, List<String> aliases, List<Command> subCommands) {
this.help = help;
this.usage = usage;
this.aliases = List.copyOf(aliases);
this.subCommands = List.copyOf(subCommands);
}
@ -36,8 +38,7 @@ public abstract class Command {
for (Command command : subCommands) {
if (command.isAlias(args.get(0))) {
CommandResolution resolution = command.resolve(args.subArgs());
if (!aliases.isEmpty())
resolution.resolvePath().add(0, aliases.get(0));
if (!aliases.isEmpty()) resolution.resolvePath().add(0, aliases.get(0));
return resolution;
}
}
@ -48,9 +49,9 @@ public abstract class Command {
}
public void buildHelp(HelpBuilder builder) {
builder.writeCommand(aliases, help);
builder.writeCommand(aliases, help, usage);
for (Command command : subCommands) {
command.buildHelp(builder.beginSubcommand());
command.buildHelp(aliases.isEmpty() ? builder : builder.beginSubcommand(aliases.get(0)));
}
}
}

View File

@ -8,7 +8,7 @@ import java.util.List;
public class Commands {
private static final GuiCommand GUI_COMMAND = new GuiCommand();
public static final Command COMMANDS_ROOT = new Command("", List.of(), List.of(
public static final Command COMMANDS_ROOT = new Command("Root command", "<command>", List.of(), List.of(
new HelpCommand(),
GUI_COMMAND,
new LaunchCommand(),

View File

@ -1,20 +1,21 @@
package io.gitlab.jfronny.inceptum.cli;
import java.util.ArrayList;
import java.util.List;
public class HelpBuilder {
private final boolean skip;
private final StringBuilder builder;
private final int level;
private final List<String> upper;
public HelpBuilder(boolean skipFirst) {
this.skip = skipFirst;
public HelpBuilder() {
this.builder = new StringBuilder();
this.level = 0;
this.upper = List.of();
}
private HelpBuilder(int level, StringBuilder builder) {
this.skip = false;
private HelpBuilder(int level, StringBuilder builder, List<String> upper) {
this.upper = upper;
this.builder = builder;
this.level = level;
}
@ -23,23 +24,34 @@ public class HelpBuilder {
return builder.length() > 0 ? builder.substring(1) : "";
}
public HelpBuilder beginSubcommand() {
return new HelpBuilder(skip ? level : level + 1, builder);
public HelpBuilder beginSubcommand(String of) {
List<String> newUpper = new ArrayList<>(upper);
newUpper.add(of);
return new HelpBuilder(level + 1, builder, List.copyOf(newUpper));
}
public void writeCommand(List<String> aliases, String help) {
if (skip) return;
builder.append('\n');
String indent = " ".repeat(level * 4);
builder.append(indent);
builder.append("- ");
public void writeCommand(List<String> aliases, String help, String usage) {
if (aliases.isEmpty()) return;
String indent = " ".repeat(level * 2);
builder.append('\n').append(indent).append("- ");
for (int i = 0, aliasesSize = aliases.size(); i < aliasesSize; i++) {
String alias = aliases.get(i);
builder.append(alias);
if (i < aliasesSize - 1)
builder.append(", ");
}
builder.append(": ");
builder.append(help.replace("\n", "\n " + indent));
builder.append(": ").append(help.replace("\n", "\n " + indent));
if (level == 0 && !usage.isBlank() && !aliases.isEmpty()) {
StringBuilder usagePrefix = new StringBuilder("inceptum");
for (String s : upper) {
usagePrefix.append(" ").append(s);
}
usagePrefix.append(" ").append(aliases.get(0)).append(" ");
builder.append("\n ")
.append(indent)
.append("Usage: ")
.append(usagePrefix)
.append(usage.replace("\n", "\n " + indent + usagePrefix));
}
}
}

View File

@ -14,7 +14,7 @@ import java.util.Set;
public class BatchCommand extends Command {
public BatchCommand() {
super("Executes several commands specified in a text file in one setting", "batch");
super("Executes several commands specified in a text file in one setting", "<source file>", "batch");
}
@Override

View File

@ -20,7 +20,7 @@ import java.util.List;
public class GitCommand extends Command {
public GitCommand() {
super("Aliases to some git commands. Intended to be used in batch commands", List.of("git", "sync"), List.of(
super("Aliases to some git commands. Intended to be used in batch commands", "", List.of("git", "sync"), List.of(
new CommitCommand(),
new PushCommand(),
new PullCommand(),
@ -35,8 +35,8 @@ public class GitCommand extends Command {
}
private static abstract class BaseGitCommand extends Command {
public BaseGitCommand(String help, String... aliases) {
super(help, aliases);
public BaseGitCommand(String help, String usage, String... aliases) {
super(help, usage, aliases);
}
@Override
@ -72,7 +72,7 @@ public class GitCommand extends Command {
private static class CommitCommand extends BaseGitCommand {
public CommitCommand() {
super("Adds and commits all changes in the instance.\nUsage: inceptum git commit <instance> [message...]", "commit");
super("Adds and commits all changes in the instance.", "<instance> [message...]", "commit");
}
@Override
@ -95,7 +95,7 @@ public class GitCommand extends Command {
private static class PushCommand extends BaseGitCommand {
public PushCommand() {
super("Pushes the current branch to the configured remote\nUsage: inceptum git push <instance>", "push");
super("Pushes the current branch to the configured remote", "<instance>", "push");
}
@Override
@ -112,7 +112,7 @@ public class GitCommand extends Command {
private static class PullCommand extends BaseGitCommand {
public PullCommand() {
super("Pulls remote changes into the current branch and working tree\nUsage: inceptum git pull <instance>", "pull");
super("Pulls remote changes into the current branch and working tree", "<instance>", "pull");
}
@Override
@ -130,7 +130,7 @@ public class GitCommand extends Command {
private static class ResetCommand extends BaseGitCommand {
public ResetCommand() {
super("Resets the current instance state to the last commit\nUsage: inceptum git reset <instance>", "reset");
super("Resets the current instance state to the last commit", "<instance>", "reset");
}
@Override
@ -151,7 +151,7 @@ public class GitCommand extends Command {
private static class CloneCommand extends Command {
public CloneCommand() {
super("Clones an instance from a URL\nUsage: inceptum git clone <remote> [name]", "clone");
super("Clones an instance from a URL", "<remote> [name]", "clone");
}
@Override

View File

@ -14,7 +14,7 @@ import java.net.URISyntaxException;
public class GuiCommand extends Command {
public GuiCommand() {
super("Displays the Inceptum UI", "gui", "show");
super("Displays the Inceptum UI", "", "gui", "show");
}
@Override

View File

@ -7,15 +7,14 @@ import java.util.List;
public class HelpCommand extends Command {
public HelpCommand() {
super("Displays this screen", "help");
super("Displays this screen", "[command]", "help");
}
@Override
protected void invoke(CommandArgs args) {
HelpBuilder help;
HelpBuilder help = new HelpBuilder();
if (args.length == 0) {
printHeader();
System.out.println("\nCommands:");
help = new HelpBuilder(true);
Commands.COMMANDS_ROOT.buildHelp(help);
}
else {
@ -27,7 +26,6 @@ public class HelpCommand extends Command {
}
printHeader();
System.out.println("\nFound matching: \"" + String.join(" ", resolution.resolvePath()) + "\"");
help = new HelpBuilder(false);
resolution.command().buildHelp(help);
}
System.out.println(help.getResult());

View File

@ -8,7 +8,7 @@ import java.util.Arrays;
public class JvmStateCommand extends Command {
public JvmStateCommand() {
super("Displays information about the JVM state. For debugging", "jvmstate");
super("Displays information about the JVM state. For debugging", "", "jvmstate");
}
@Override

View File

@ -24,6 +24,7 @@ public class LaunchCommand extends Command {
public LaunchCommand() {
super("Launches an instance of the game (client by default). Non-blocking (batch commands will continue if this is ran)",
"<instance> [game arguments...]",
List.of("run", "launch", "start"),
List.of(
new LaunchCommand("Explicitly launch a client", "client", false, false),
@ -36,7 +37,7 @@ public class LaunchCommand extends Command {
}
private LaunchCommand(String help, String name, boolean server, boolean restart, Command... subCommands) {
super(help, List.of(name), Arrays.asList(subCommands));
super(help, "<instance> [game arguments...]", List.of(name), Arrays.asList(subCommands));
this.server = server;
this.restart = restart;
}

View File

@ -14,7 +14,7 @@ import java.util.List;
public class ListCommand extends Command {
public ListCommand() {
super("Lists all available instances", "list", "ls");
super("Lists all available instances", "", "list", "ls");
}
@Override

View File

@ -15,14 +15,14 @@ import java.util.List;
public class UpdateCheckCommand extends Command {
private final boolean install;
public UpdateCheckCommand() {
super("Checks for inceptum updates", List.of("update"), List.of(
super("Checks for inceptum updates", "", List.of("update"), List.of(
new UpdateCheckCommand("Automatically install updates", "install", true)
));
install = false;
}
private UpdateCheckCommand(String help, String name, boolean install) {
super(help, name);
super(help, "", name);
this.install = install;
}

View File

@ -4,7 +4,7 @@ import io.gitlab.jfronny.inceptum.cli.*;
public class WrapperCommand extends Command {
public WrapperCommand() {
super("Invokes a command in a wrapper environment", "wrapper");
super("Invokes a command in a wrapper environment", "<command>", "wrapper");
}
@Override