package sample;

import static util.Utils.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import sample.model.Model;

public class SubcmdMain {

    public static void main(String[] rawArgs) {
        try {
            _main(rawArgs);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    static void _main(String[] rawArgs) throws Exception {
        String allArgs = rawArgs[0];
        List<String> tempArgs = Arrays.asList(allArgs.split("\u001f"));

        for (int i = 0; i < tempArgs.size(); i++) {
            debug(fmt("temp arg %s (%s)", i, tempArgs.get(i)));
        }

        Config.setCurrentDir(System.getenv("CURRENT_DIR"));
        Config.setProjectDir(System.getenv("PROJECT_DIR"));
        String subcmd = tempArgs.get(0);
        debug(fmt("subcmd (%s)", subcmd));

        List<String> mainArgs = new ArrayList<>();
        for (int i = 1; i < tempArgs.size(); i++) {
            mainArgs.add(tempArgs.get(i));
        }

        Options opts = new Options();
        opts.addOption("h", "help", false, "Print help");

        CommandLineParser parser = new DefaultParser();
        CommandLine cl;

        Model model = createModel();

        switch (subcmd) {
        case "cmd_a":
            opts.addOption("f", "foo", false, "Option foo");
            opts.addOption(null, "profile", true, "Profile");
            cl = parser.parse(opts, toArray(mainArgs));

            checkHelp(cl, opts);
            setProfile(cl);
            Config.load();

            model.cmdA(cl.hasOption("f"), cl.getArgList());
            break;

        case "cmd_b":
            opts.addOption("b", "bar", true, "Option bar");
            opts.addOption(null, "profile", true, "Profile");
            cl = parser.parse(opts, toArray(mainArgs));

            checkHelp(cl, opts);
            setProfile(cl);
            Config.load();

            model.cmdB(cl.getOptionValue("b"), cl.getArgList());
            break;

        default:
            throw new IllegalArgumentException("cmd (" + subcmd + ")");
        }
    }

    private static void checkHelp(CommandLine cl, Options opts) {
        if (cl.hasOption("help")) {
            new HelpFormatter().printHelp("run_subcmd_(mvn|gradle).sh", opts);
            System.exit(0);
        }
    }

    private static void setProfile(CommandLine cl) {
        if (cl.hasOption("profile")) {
            Config.setProfile(cl.getOptionValue("profile"));
        } else {
            Config.setProfile("devel");
        }

    }

    private static Model createModel() {
        return new Model();
    }

}

@@ -1130,131 +1130,44 @@
 s = 
-new ArrayList%3C%3E();%0A        for (int i = 1; i %3C tempArgs.size(); i++) %7B%0A            mainArgs.add(tempArgs.get(i));%0A        %7D
+slice(tempArgs, 1, tempArgs.size());
 %0A%0A  
@@ -2788,10 +2788,307 @@
 %0A    %7D%0A%0A
+    private static List%3CString%3E slice(List%3CString%3E list, int from, int until) %7B%0A        List%3CString%3E newList = new ArrayList%3C%3E();%0A        int newSize = until - from;%0A%0A        for (int i = 0; i %3C newSize; i++) %7B%0A            newList.add(list.get(from + i));%0A        %7D%0A%0A        return newList;%0A    %7D%0A
 %7D%0A

Use Utils.slice