package cz.crcs.ectester.common.cli;

import org.apache.commons.cli.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class TreeParser implements CommandLineParser {
    private Map<String, ParserOptions> parsers;
    private boolean required;

    public TreeParser(Map<String, ParserOptions> parsers, boolean required) {
        this.parsers = parsers;
        this.required = required;
    }

    public Map<String, ParserOptions> getParsers() {
        return Collections.unmodifiableMap(parsers);
    }

    public boolean isRequired() {
        return required;
    }

    @Override
    public CommandLine parse(Options options, String[] arguments) throws ParseException {
        return this.parse(options, arguments, null);
    }

    public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException {
        return this.parse(options, arguments, properties, false);
    }

    @Override
    public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException {
        return this.parse(options, arguments, null, stopAtNonOption);
    }

    public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException {
        DefaultParser thisParser = new DefaultParser();
        CommandLine cli = thisParser.parse(options, arguments, properties, true);

        CommandLine subCli = null;
        String[] args = cli.getArgs();
        String sub = null;
        if (args.length != 0) {
            sub = args[0];
            ParserOptions subparser = parsers.get(sub);
            if (subparser != null) {
                String[] remainingArgs = new String[args.length - 1];
                System.arraycopy(args, 1, remainingArgs, 0, args.length - 1);
                subCli = subparser.getParser().parse(subparser.getOptions(), remainingArgs, true);
            }
        } else {
            if (required) {
                throw new MissingOptionException(new ArrayList(parsers.keySet()));
            }
        }
        if (subCli instanceof TreeCommandLine) {
            TreeCommandLine subTreeCli = (TreeCommandLine) subCli;
            subTreeCli.setName(sub);
            return new TreeCommandLine(cli, subTreeCli);
        } else if (subCli != null) {
            TreeCommandLine subTreeCli = new TreeCommandLine(sub, subCli, null);
            return new TreeCommandLine(cli, subTreeCli);
        } else {
            return new TreeCommandLine(cli, null);
        }
    }
}

@@ -86,98 +86,9 @@
 til.
-ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Map;%0Aimport java.util.Properties
+*
 ;%0A%0A/
@@ -1594,16 +1594,17 @@
 rgs%5B0%5D;%0A
+%0A
         
@@ -1611,68 +1611,593 @@
     
-ParserOptions subparser = parsers.get(sub);%0A            if (
+List%3CString%3E matches = new LinkedList%3C%3E();%0A            String finalSub = sub;%0A            for (Map.Entry%3CString, ParserOptions%3E entry : parsers.entrySet()) %7B%0A                if (entry.getKey().equalsIgnoreCase(finalSub)) %7B%0A                    matches.clear();%0A                    matches.add(finalSub);%0A                    break;%0A                %7D else if (entry.getKey().startsWith(finalSub)) %7B%0A                    matches.add(entry.getKey());%0A                %7D%0A            %7D%0A%0A            if (matches.size() == 1) %7B%0A                sub = matches.get(0);%0A                ParserOptions 
 subp
@@ -2202,26 +2202,35 @@
 bparser 
-!
 = 
-null) %7B
+parsers.get(sub);
 %0A       
@@ -2465,24 +2465,256 @@
 rgs, true);%0A
+            %7D else if (matches.size() %3E 1) %7B%0A                throw new UnrecognizedOptionException(%22Ambiguous option: %22 + sub + %22, couldn't match. Partially matches: %22 + String.join(%22,%22, matches.toArray(new String%5B0%5D)) + %22.%22, sub);%0A
             

Add prefix handling of CLI actions.