[FEATURE] Субкомманды и автодополнение для них

This commit is contained in:
Gravit 2019-07-06 18:42:29 +07:00
parent 752fb9e879
commit 5b7ae04fbd
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 40 additions and 16 deletions

View file

@ -1,14 +1,12 @@
package pro.gravit.utils.command;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import org.jline.reader.Candidate;
import pro.gravit.utils.helper.VerifyHelper;
public abstract class Command {
public Map<String, Command> childCommands = new HashMap<>();
protected static String parseUsername(String username) throws CommandException {
try {
@ -39,20 +37,31 @@ public Candidate buildCandidate(CommandHandler.Category category, String command
public List<Candidate> complete(List<String> words, int wordIndex, String word)
{
return new ArrayList<>();
if(wordIndex == 0)
{
List<Candidate> candidates = new ArrayList<>();
childCommands.forEach((k,v) -> {
if(k.startsWith(word))
{
candidates.add(new Candidate(k));
}
});
return candidates;
}
else
{
Command cmd = childCommands.get(words.get(0));
if(cmd == null) return new ArrayList<>();
return cmd.complete(words.subList(1, words.size()), wordIndex - 1, word);
}
}
public List<Candidate> subCommandComplete(String word, List<String> commands)
public void invokeSubcommands(String... args) throws Exception
{
List<Candidate> candidates = new ArrayList<>();
for(String s : commands)
{
if(word.startsWith(s))
{
candidates.add(new Candidate(s));
}
}
return candidates;
verifyArgs(args, 1);
Command command = childCommands.get(args[0]);
if(command == null) throw new CommandException(String.format("Unknown sub command: '%s'", args[0]));
command.invoke(Arrays.copyOfRange(args, 1, args.length));
}

View file

@ -1,6 +1,7 @@
package pro.gravit.utils.command;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.jline.reader.Candidate;
@ -47,7 +48,8 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
else
{
Command target = findCommand(line.words().get(0));
List<Candidate> candidates1 = target.complete(line.words(), line.wordIndex(), completeWord);
List<String> words = line.words();
List<Candidate> candidates1 = target.complete(words.subList(1, words.size()), line.wordIndex() - 1, completeWord);
candidates.addAll(candidates1);
}
}

View file

@ -0,0 +1,13 @@
package pro.gravit.utils.command;
public abstract class SubCommand extends Command {
@Override
public String getArgsDescription() {
return null;
}
@Override
public String getUsageDescription() {
return null;
}
}