add OptionCompleteHandler, better complete option

This commit is contained in:
hengyunabc 2021-04-29 17:41:47 +08:00
parent b704e7cabd
commit 6a102d61b0
2 changed files with 55 additions and 0 deletions

View File

@ -355,4 +355,47 @@ public class CompletionUtils {
}
return false;
}
public static boolean completeOptions(Completion completion, List<OptionCompleteHandler> handlers) {
List<CliToken> tokens = completion.lineTokens();
/**
* <pre>
* 比如 ` --name a`这样子的tokens
* </pre>
*/
if (tokens.size() >= 3) {
CliToken cliToken_2 = tokens.get(tokens.size() - 2);
CliToken cliToken_3 = tokens.get(tokens.size() - 3);
if (cliToken_2.isBlank()) {
String token_3 = cliToken_3.value();
for (OptionCompleteHandler handler : handlers) {
if (handler.matchName(token_3)) {
return handler.complete(completion);
}
}
}
}
/**
* <pre>
* 比如 ` --name `这样子的tokens
* </pre>
*/
if (tokens.size() >= 2) {
CliToken cliToken_1 = tokens.get(tokens.size() - 1);
CliToken cliToken_2 = tokens.get(tokens.size() - 2);
if (cliToken_1.isBlank()) {
String token_2 = cliToken_2.value();
for (OptionCompleteHandler handler : handlers) {
if (handler.matchName(token_2)) {
return handler.complete(completion);
}
}
}
}
return false;
}
}

View File

@ -0,0 +1,12 @@
package com.taobao.arthas.core.shell.cli;
/**
*
* @author hengyunabc 2021-04-29
*
*/
public interface OptionCompleteHandler {
boolean matchName(String token);
boolean complete(Completion completion);
}