package ee.shy.cli.command;

import ee.shy.cli.Command;
import ee.shy.cli.SuperCommand;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CompletionCommand implements Command {
    private final Command rootCommand;

    public CompletionCommand(Command rootCommand) {
        this.rootCommand = rootCommand;
    }

    @Override
    public void execute(String[] args) throws IOException {
        List<String> argsList = new ArrayList<>(Arrays.asList(args));
        argsList.add(null);

        Command command = rootCommand;
        for (String arg : argsList) {
            if (command instanceof SuperCommand) {
                Map<String, Command> subCommands = ((SuperCommand) command).getSubCommands();
                Command subCommand = subCommands.get(arg);
                if (subCommand != null) {
                    command = subCommand;
                }
                else {
                    System.out.println(String.join(" ", subCommands.keySet()));
                    break;
                }
            }
            else if (command instanceof HelpCommand) {
                command = ((HelpCommand) command).getRootCommand();
            }
        }
    }

    @Override
    public String getHelp() {
        return null;
    }
}

@@ -114,84 +114,8 @@
 on;%0A
-import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A
 impo
@@ -410,175 +410,150 @@
     
-List%3CString%3E argsList = new ArrayList%3C%3E(Arrays.asList(args));%0A        argsList.add(null);%0A%0A        Command command = rootCommand;%0A        for (String arg : argsList) %7B
+Command command = rootCommand;%0A        for (int i = 0; i %3C= args.length; i++) %7B // loop for one extra time for supercommands without arguments
 %0A   
@@ -732,33 +732,113 @@
 mand
- =
+;%0A                if ((i %3C args.length - 1) // complete
  sub
-C
+c
 ommand
-s.get(arg);%0A
+ even if fully typed (don't nest yet)%0A 
     
@@ -849,19 +849,27 @@
         
-if 
+       && (
 (subComm
@@ -871,16 +871,44 @@
 bCommand
+ = subCommands.get(args%5Bi%5D))
  != null
@@ -908,16 +908,17 @@
 != null)
+)
  %7B%0A     
@@ -1256,16 +1256,130 @@
 mmand();
+ // changed command point without parsing extra argument%0A                i--; // step back for that extra argument
 %0A       

Fix bash completion for HelpCommand and fully typed subcommands