fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. static void mycmd(String[] args) {
  6. System.out.println("You ran: " + String.join(" ", args));
  7. }
  8.  
  9. static List<String> mycompletions(String[] words, int cword) {
  10. String[] options = {"start", "stop", "restart", "status", "file"};
  11. String cur = words[cword];
  12. List<String> matches = new ArrayList<>();
  13. for (String opt : options) {
  14. if (cur.isEmpty() || opt.startsWith(cur)) {
  15. matches.add(opt);
  16. }
  17. }
  18. return matches;
  19. }
  20.  
  21. public static void main(String[] args) {
  22.  
  23. // Simulate tab completion for "st"
  24. String[] words1 = {"mycmd", "st"};
  25. List<String> suggestions1 = mycompletions(words1, 1);
  26. System.out.println("Input: st → Suggestions: " + String.join(" ", suggestions1));
  27.  
  28. // Simulate tab completion for "re"
  29. String[] words2 = {"mycmd", "re"};
  30. List<String> suggestions2 = mycompletions(words2, 1);
  31. System.out.println("Input: re → Suggestions: " + String.join(" ", suggestions2));
  32.  
  33. // Simulate tab completion for "s"
  34. String[] words3 = {"mycmd", "s"};
  35. List<String> suggestions3 = mycompletions(words3, 1);
  36. System.out.println("Input: s → Suggestions: " + String.join(" ", suggestions3));
  37.  
  38. // Run the actual command
  39. mycmd(new String[]{"hello", "world"});
  40. }
  41. }
Success #stdin #stdout 0.12s 57592KB
stdin
Standard input is empty
stdout
Input: st → Suggestions: start stop status
Input: re → Suggestions: restart
Input: s → Suggestions: start stop status
You ran: hello world