[DOC] Документация к Command/CommandHandler

This commit is contained in:
Gravit 2019-08-28 18:18:57 +07:00
parent 5f6da3a638
commit 31356af213
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 50 additions and 0 deletions

View file

@ -6,6 +6,9 @@
import pro.gravit.utils.helper.VerifyHelper; import pro.gravit.utils.helper.VerifyHelper;
public abstract class Command { public abstract class Command {
/**
* List of available subcommands
*/
public Map<String, Command> childCommands; public Map<String, Command> childCommands;
public Command() { public Command() {
@ -38,11 +41,24 @@ protected static UUID parseUUID(String s) throws CommandException {
public abstract String getUsageDescription(); public abstract String getUsageDescription();
/**
* Creates a JLine candidate that appears in the list of available options when you press TAB
* @param category this command category
* @param commandName this command name
* @return JLine Candidate
*/
public Candidate buildCandidate(CommandHandler.Category category, String commandName) public Candidate buildCandidate(CommandHandler.Category category, String commandName)
{ {
return new Candidate(commandName); return new Candidate(commandName);
} }
/**
* Returns a list of available options for the next word for the current command.
* @param words list all user words
* @param wordIndex current word index
* @param word current word
* @return list of available Candidate
*/
public List<Candidate> complete(List<String> words, int wordIndex, String word) public List<Candidate> complete(List<String> words, int wordIndex, String word)
{ {
if(wordIndex == 0) if(wordIndex == 0)
@ -64,6 +80,12 @@ public List<Candidate> complete(List<String> words, int wordIndex, String word)
} }
} }
/**
* Transfer control to subcommands
* @param args command arguments(includes subcommand name)
* @throws Exception
* Error executing command
*/
public void invokeSubcommands(String... args) throws Exception public void invokeSubcommands(String... args) throws Exception
{ {
verifyArgs(args, 1); verifyArgs(args, 1);
@ -73,6 +95,12 @@ public void invokeSubcommands(String... args) throws Exception
} }
/**
* Run current command
* @param args command arguments
* @throws Exception
* Error executing command
*/
public abstract void invoke(String... args) throws Exception; public abstract void invoke(String... args) throws Exception;

View file

@ -90,6 +90,12 @@ public Command findCommand(String name) {
} }
/**
* Reads a line from the console
* @return command line
* @throws IOException
* Internal Error
*/
public abstract String readLine() throws IOException; public abstract String readLine() throws IOException;
private void readLoop() throws IOException { private void readLoop() throws IOException {
@ -133,6 +139,12 @@ public interface CommandWalk {
void walk(Category category, String name, Command command); void walk(Category category, String name, Command command);
} }
/**
* Walk all categories
* Categories are sorted in the order they are added.
* The base category is walked last
* @param callback your callback
*/
public void walk(CommandWalk callback) { public void walk(CommandWalk callback) {
for (CommandHandler.Category category : getCategories()) { for (CommandHandler.Category category : getCategories()) {
for (Map.Entry<String, Command> entry : category.category.commandsMap().entrySet()) for (Map.Entry<String, Command> entry : category.category.commandsMap().entrySet())
@ -150,8 +162,18 @@ public List<Category> getCategories() {
return categories; return categories;
} }
/**
* If supported, sends a bell signal to the console
* @throws IOException
* Internal Error
*/
public abstract void bell() throws IOException; public abstract void bell() throws IOException;
/**
* Cleans the console
* @throws IOException
* Internal Error
*/
public abstract void clear() throws IOException; public abstract void clear() throws IOException;
} }