package com.ociweb.gl.api;

public class ArgumentParser implements ArgumentProvider {
    private final String[] args;

    public ArgumentParser(String[] args) {
        this.args = args;
    }

    @Override
    public String[] args() {
        return args;
    }

    @Override
    public String getArgumentValue(String longName, String shortName, String defaultValue) {
        return getOptArg(longName, shortName, defaultValue);
    }

    @Override
    public Character getArgumentValue(String longName, String shortName, Character defaultValue) {
        String value = getOptArg(longName, shortName, defaultValue!=null?defaultValue.toString():null);
        return value!=null?value.charAt(0):null;
    }

    @Override
    public Byte getArgumentValue(String longName, String shortName, Byte defaultValue) {
        String value = getOptArg(longName, shortName, defaultValue!=null?defaultValue.toString():null);
        return value!=null?Byte.parseByte(value):null;
    }

    @Override
    public Short getArgumentValue(String longName, String shortName, Short defaultValue) {
        String value = getOptArg(longName, shortName, defaultValue!=null?defaultValue.toString():null);
        return value!=null?Short.parseShort(value):null;
    }

    @Override
    public Long getArgumentValue(String longName, String shortName, Long defaultValue) {
        String value = getOptArg(longName, shortName, defaultValue!=null?defaultValue.toString():null);
        return value!=null?Long.parseLong(value):null;
    }

    @Override
    public Integer getArgumentValue(String longName, String shortName, Integer defaultValue) {
        String value = getOptArg(longName, shortName, defaultValue!=null?defaultValue.toString():null);
        return value!=null?Integer.parseInt(value):null;
    }

    @Override
    public Boolean getArgumentValue(String longName, String shortName, Boolean defaultValue) {
        String value = getOptArg(longName, shortName, defaultValue!=null?defaultValue.toString():null);
        return value!=null?Boolean.parseBoolean(value):null;
    }

    @Override
    public boolean hasArgument(String longName, String shortName) {
        return hasArg(longName, shortName);
    }

    private String getOptArg(String longName, String shortName, String defaultValue) {
        String prev = null;
        for (String token : args) {
            if (longName.equals(prev) || shortName.equals(prev)) {
                if (token == null || token.trim().length() == 0 || token.startsWith("-")) {
                    return defaultValue;
                }
                return reportChoice(longName, shortName, token.trim());
            }
            prev = token;
        }
        return reportChoice(longName, shortName, defaultValue);
    }


    private boolean hasArg(String longName, String shortName) {
        for(String token : args) {
            if(longName.equals(token) || shortName.equals(token)) {
                reportChoice(longName, shortName, "");
                return true;
            }
        }
        return false;
    }

    private String reportChoice(final String longName, final String shortName, final String value) {
        System.out.append(longName).append(" ").append(shortName).append(" = ").append(value).append("\n");
        return value;
    }
}

@@ -2546,16 +2546,50 @@
   return
+ reportChoice(longName, shortName,
  default
@@ -2589,24 +2589,25 @@
 defaultValue
+)
 ;%0A          
@@ -2683,16 +2683,30 @@
 n.trim()
+, defaultValue
 );%0A     
@@ -3005,16 +3005,23 @@
         
+return 
 reportCh
@@ -3050,10 +3050,12 @@
 me, 
-%22%22
+true
 );%0A 
@@ -3069,48 +3069,290 @@
     
-    return true
+%7D%0A        %7D%0A        return reportChoice(longName, shortName, false)
 ;%0A    
+%7D%0A%0A
     
-    %7D%0A        %7D
+private String reportChoice(final String longName, final String shortName, final String dflt) %7B%0A        System.out.append(longName).append(%22 %22).append(shortName).append(%22 = %22).append(dflt).append(%22%5Cn%22);
 %0A   
@@ -3363,21 +3363,20 @@
  return 
-false
+dflt
 ;%0A    %7D%0A
@@ -3477,140 +3477,556 @@
 alue
-) %7B%0A        System.out.append(longName).append(%22 %22).append(shortName).append(%22 = %22).append(value).append(%22%5Cn%22);%0A        return value
+, final String dflt) %7B%0A        if (value.equals(dflt)) %7B%0A            return reportChoice(longName, shortName, dflt);%0A        %7D%0A        System.out.append(longName).append(%22 %22).append(shortName).append(%22 = %22).append(value).append(%22 (default = %22).append(dflt).append(%22)%5Cn%22);%0A        return value;%0A    %7D%0A%0A    private boolean reportChoice(final String longName, final String shortName, boolean found) %7B%0A        System.out.append(longName).append(%22 %22).append(shortName).append(%22 found %22).append(Boolean.toString(found)).append(%22%5Cn%22);%0A        return found
 ;%0A  

add '=' to arg display