Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A type converter for InetSocketAddress #972

Closed
sbernard31 opened this issue Mar 5, 2020 · 3 comments
Closed

A type converter for InetSocketAddress #972

sbernard31 opened this issue Mar 5, 2020 · 3 comments
Milestone

Comments

@sbernard31
Copy link
Contributor

It could be nice to add a ITypeConverter for InetSocketAddress.

The idea is to be able to support value like :

10.0.0.1:8080
localhost:30
[2001:db8::85a3::ac1f:8001]:5555

Ideally it would be good to be able to have a way to define value for hostname or port or both meaning that you could write :

10.0.0.1
localhost
[2001:db8::85a3::ac1f:8001]
:8080

(But my guess is ITypeConverter API is not really adapted for that as it should add the default value as convert parameter)

@remkop
Copy link
Owner

remkop commented Mar 6, 2020

I think I briefly looked at this in the early days but decided not to create a built-in converter.

We would need to decide on a separator between host and port, and although I have seen ':' used a lot, there are also many tools that require the host and port to be specified separately (space-separated). There is no unambiguous standardized format, so I thought that instead of deciding on an arbitrary one, this converter was better done with a custom implementation.

Making a custom converter is fairly straightforward, something like this:

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) throws Exception {
            int pos = value.lastIndexOf(':');
            if (pos < 0) {
                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));
            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=invalid");
    }
}

@sbernard31
Copy link
Contributor Author

As you said, ':' is used a lot. So even if this not really a standard, IMHO, this make sense that "the more often used syntax" get a default implementation. Nothing prevent someone else to use a custom converter if he don't want to use the default/more popular way.

Anyway this was just an idea, feel free to close the issue if you think it's not a good one 😉

Thx again for this great piece of software 🙏

@remkop remkop added this to the 4.3 milestone Mar 7, 2020
remkop added a commit that referenced this issue Mar 7, 2020
* Add section "Handling Invalid Input" for custom type converters to user manual
* Demonstrate `TypeConversionException`
* Add example `InetSocketAddressConverter` to `picocli-examples`
@remkop
Copy link
Owner

remkop commented Mar 7, 2020

I added a new section Handling Invalid Input for custom type converters to the user manual. During our discussion I realized the manual did not mention the TypeConversionException, and I took this opportunity to correct that. This section now shows the code for a custom InetSocketAddressConverter, to demonstrate the use of that exception.

I added a runnable example InetSocketAddressConverterDemo to the picocli-examples module.

This is not the same as getting a built-in InetSocketAddressConverter, but I hope this is still valuable. Enjoy using picocli!

@remkop remkop closed this as completed Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants