package com.publicuhc.pluginframework.routing;

import com.publicuhc.pluginframework.routing.exception.OptionMissingException;
import org.bukkit.craftbukkit.libs.joptsimple.OptionParser;
import org.bukkit.craftbukkit.libs.joptsimple.OptionSet;

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

public class DefaultCommandOptionsParser implements CommandOptionsParser {

    private final OptionParser parser;
    private final String[] requiredOptions;

    public DefaultCommandOptionsParser(OptionParser parser, String[] required)
    {
        this.parser = parser;
        this.requiredOptions = required;
    }

    public String[] getRequiredOptions()
    {
        return requiredOptions;
    }

    public OptionParser getParser()
    {
        return parser;
    }

    /**
     * Proxy for {@link org.bukkit.craftbukkit.libs.joptsimple.OptionParser#parse(String...)} but also checks for all options set
     *
     * @param args the args to parse
     * @return the parsed optionset
     * @throws org.bukkit.craftbukkit.libs.joptsimple.OptionException when failing parsing the args or required args are not present
     * @throws com.publicuhc.pluginframework.routing.exception.OptionMissingException when a required option is missing
     */
    public OptionSet parse(String... args)
    {
        OptionSet set = parser.parse(args);

        List<String> missing = new ArrayList<String>();
        for (String req : requiredOptions) {
            if(!set.has(req)) {
                missing.add(req);
            }
        }

        if(!missing.isEmpty())
            throw new OptionMissingException(missing);

        return set;
    }
}

@@ -458,158 +458,641 @@
     
-public DefaultCommandOptionsParser(OptionParser parser, String%5B%5D required)%0A    %7B%0A        this.parser = parser;%0A        this.requiredOptions = required
+/**%0A     * Creates a new options parser that uses the given %7B@link org.bukkit.craftbukkit.libs.joptsimple.OptionParser%7D and%0A     * adds non-optional options%0A     *%0A     * @param parser the parser to use%0A     * @param required array of required option names%0A     */%0A    public DefaultCommandOptionsParser(OptionParser parser, String%5B%5D required)%0A    %7B%0A        this.parser = parser;%0A        this.requiredOptions = required;%0A    %7D%0A%0A    /**%0A     * Creates a new options parser that has no options%0A     */%0A    public DefaultCommandOptionsParser()%0A    %7B%0A        this.parser = new OptionParser();%0A        this.requiredOptions = new String%5B0%5D
 ;%0A  

add noarg constructor