mirror of
https://gitee.com/arthas/arthas.git
synced 2024-11-30 03:07:37 +08:00
add OptionCompleteHandler, better complete option
This commit is contained in:
parent
b704e7cabd
commit
6a102d61b0
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user