-
Notifications
You must be signed in to change notification settings - Fork 414
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
Comments
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 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");
}
} |
As you said, 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 🙏 |
* Add section "Handling Invalid Input" for custom type converters to user manual * Demonstrate `TypeConversionException` * Add example `InetSocketAddressConverter` to `picocli-examples`
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 I added a runnable example This is not the same as getting a built-in |
It could be nice to add a
ITypeConverter
forInetSocketAddress
.The idea is to be able to support value like :
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 :
(But my guess is
ITypeConverter
API is not really adapted for that as it should add the default value asconvert
parameter)The text was updated successfully, but these errors were encountered: