Skip to content

Commit b1d6b9c

Browse files
authored
Merge pull request #1190 from deining/converter-demo
[#1190] Improve InetSocketAddressConverter demo
2 parents 589a0b5 + c582419 commit b1d6b9c

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/index.adoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
:icons: font
1414
:imagesdir: images
1515
:sectanchors:
16+
:linkattrs:
1617
ifdef::env-github[]
1718
:caution-caption: :fire:
1819
:important-caption: :heavy_exclamation_mark:
@@ -814,15 +815,15 @@ If the user specifies invalid input, custom type converters should throw an exce
814815
Any exception is fine, and will result in a message like the below being displayed to the user:
815816

816817
----
817-
Invalid value for option '-a': cannot convert 'xxxinvalidinput' to InetSocketAddress (java.lang.IllegalArgumentException: Invalid format: must be 'host:port' but was 'xxxinvalidinput')
818+
Invalid value for option '--socket-address': cannot convert 'xxxinvalidinput' to InetSocketAddress (java.lang.IllegalArgumentException: Invalid format: must be 'host:port' but was 'xxxinvalidinput')
818819
----
819820

820821
The above error message is generic and is reasonable for many exceptions, but sometimes you want more control over the error message displayed to the user.
821822
To achieve this, throw a `picocli.CommandLine.TypeConversionException` instead.
822823
When a `TypeConversionException` is thrown, picocli will show an error message that indicates the problematic option, followed by the exception message text. The resulting output looks something like this:
823824

824825
----
825-
Invalid value for option '-a': Invalid format: must be 'host:port' but was 'xxxinvalidinput'
826+
Invalid value for option '--socket-address': Invalid format: must be 'host:port' but was 'xxxinvalidinput'
826827
----
827828

828829
Below is an example custom converter that throws a `TypeConversionException`:
@@ -846,7 +847,7 @@ class InetSocketAddressConverter implements ITypeConverter<InetSocketAddress> {
846847
}
847848
----
848849

849-
The `picocli-examples` module on GitHub has a runnable https://github.com/remkop/picocli/blob/master/picocli-examples/src/main/java/picocli/examples/typeconverter/InetSocketAddressConverterDemo.java[example].
850+
The `picocli-examples` module on GitHub has a minimal working https://github.com/remkop/picocli/blob/master/picocli-examples/src/main/java/picocli/examples/typeconverter/InetSocketAddressConverterDemo.java[example] which you can run in our https://www.jdoodle.com/embed/v0/2mxo?stdin=1&arg=1[online-editor^].
850851

851852
=== Option-specific Type Converters
852853
Picocli 2.2 added a `converter` attribute to the `@Option` and `@Parameter` annotations. This allows a specific option or positional parameter to use a different converter than would be used by default based on the type of the field.

docs/quick-guide.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
:source-highlighter: coderay
1111
:icons: font
1212
:imagesdir: images
13+
:linkattrs:
1314
:sectanchors:
1415
ifdef::env-github[]
1516
:caution-caption: :fire:

picocli-examples/src/main/java/picocli/examples/typeconverter/InetSocketAddressConverterDemo.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Picocli: Custom converter demo
3+
* Explanation: <a href="https://picocli.info/#_handling_invalid_input">Picocli user manual</a>
4+
* Discussion: <a href="https://github.com/remkop/picocli/issues/972">GitHub issue</a>
5+
* Origin of source code: <a href="https://github.com/remkop/picocli/tree/master/picocli-examples/src/main/java/picocli/examples/typeconverter">GitHub</a>
6+
* @author Remko Popma
7+
*/
18
package picocli.examples.typeconverter;
29

310
import picocli.CommandLine;
@@ -7,7 +14,7 @@
714

815
import java.net.InetSocketAddress;
916

10-
public class InetSocketAddressConverterDemo {
17+
public class InetSocketAddressConverterDemo implements Runnable {
1118

1219
static class InetSocketAddressConverter implements ITypeConverter<InetSocketAddress> {
1320
@Override
@@ -23,13 +30,18 @@ public InetSocketAddress convert(String value) {
2330
}
2431
}
2532

26-
@Option(names = "-a", converter = InetSocketAddressConverter.class)
33+
@Option(names = { "-s", "--socket-address" }, converter = InetSocketAddressConverter.class)
2734
InetSocketAddress address;
2835

2936
public static void main(String[] args) {
30-
new CommandLine(new InetSocketAddressConverterDemo()).execute("-a=xxxinvalidinput");
37+
new CommandLine(new InetSocketAddressConverterDemo()).execute("--socket-address=invalidInputWithoutColon");
3138

3239
// try this also:
33-
//new CommandLine(new InetSocketAddressConverterDemo()).execute("-a=xxxinvalidinput:xxx");
40+
//new CommandLine(new InetSocketAddressConverterDemo()).execute("--socket-address=invalidInputWithColon:xxx");
41+
}
42+
43+
@Override
44+
public void run() {
45+
System.out.println(String.format("Valid socket address given: host: %s, port: %s", address.getHostString(), address.getPort()));
3446
}
3547
}

0 commit comments

Comments
 (0)