You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.adoc
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@
13
13
:icons: font
14
14
:imagesdir: images
15
15
:sectanchors:
16
+
:linkattrs:
16
17
ifdef::env-github[]
17
18
:caution-caption: :fire:
18
19
:important-caption: :heavy_exclamation_mark:
@@ -814,15 +815,15 @@ If the user specifies invalid input, custom type converters should throw an exce
814
815
Any exception is fine, and will result in a message like the below being displayed to the user:
815
816
816
817
----
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')
818
819
----
819
820
820
821
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.
821
822
To achieve this, throw a `picocli.CommandLine.TypeConversionException` instead.
822
823
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:
823
824
824
825
----
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'
826
827
----
827
828
828
829
Below is an example custom converter that throws a `TypeConversionException`:
@@ -846,7 +847,7 @@ class InetSocketAddressConverter implements ITypeConverter<InetSocketAddress> {
846
847
}
847
848
----
848
849
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^].
850
851
851
852
=== Option-specific Type Converters
852
853
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.
0 commit comments