package picocli.examples.typeconverter;

import picocli.CommandLine;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.Option;
import picocli.CommandLine.TypeConversionException;

import java.net.InetSocketAddress;

public class InetSocketAddressConverterDemo {

    static class InetSocketAddressConverter implements ITypeConverter<InetSocketAddress> {
        @Override
        public InetSocketAddress convert(String value) {
            int pos = value.lastIndexOf(':');
            if (pos < 0) {
                //throw new IllegalArgumentException("Invalid format: must be 'host:port' but was '" + value + "'");
                throw new TypeConversionException("Invalid format: must be 'host:port' but was '" + value + "'");
            }
            String adr = value.substring(0, pos);
            int port = Integer.parseInt(value.substring(pos + 1)); // invalid port shows the generic error message
            return new InetSocketAddress(adr, port);
        }
    }

    @Option(names = "-a", converter = InetSocketAddressConverter.class)
    InetSocketAddress address;

    public static void main(String[] args) {
        new CommandLine(new InetSocketAddressConverterDemo()).execute("-a=xxxinvalidinput");

        // try this also:
        //new CommandLine(new InetSocketAddressConverterDemo()).execute("-a=xxxinvalidinput:xxx");
    }
}

@@ -1,8 +1,411 @@
+/**%0A * Picocli: Custom converter demo%0A * Explanation: %3Ca href=%22https://picocli.info/#_handling_invalid_input%22%3EPicocli user manual%3C/a%3E%0A * Discussion: %3Ca href=%22https://github.com/remkop/picocli/issues/972%22%3EGitHub issue%3C/a%3E%0A * Origin of source code: %3Ca href=%22https://github.com/remkop/picocli/tree/master/picocli-examples/src/main/java/picocli/examples/typeconverter%22%3EGitHub%3C/a%3E%0A * @author Remko Popma%0A */%0A
 package 
@@ -676,16 +676,36 @@
 terDemo 
+implements Runnable 
 %7B%0A%0A    s
@@ -1442,12 +1442,36 @@
 s = 
-%22-a%22
+%7B %22-s%22, %22--socket-address%22 %7D
 , co
@@ -1663,33 +1663,56 @@
 ecute(%22-
-a=xxx
+-socket-address=
 invalid
-i
+I
 nput
+WithoutColon
 %22);%0A%0A   
@@ -1812,30 +1812,232 @@
 e(%22-
-a=xxx
+-socket-address=
 invalid
-i
+I
 nput
-:xxx%22
+WithColon:xxx%22);%0A    %7D%0A%0A    @Override%0A    public void run() %7B%0A        System.out.println(String.format(%22Valid socket address given: host: %25s, port: %25s%22, address.getHostString(), address.getPort())
 );%0A 

make demo runnable, add javadoc