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
The value types natively supported by commons-cli are:
422
+
<ul>
423
+
<li>Object.class - The string value must be the name of a class with a no argument constructor</li>
424
+
<li>Class.class - The string value must be the name of a class</li>
425
+
<li>Date.class - The string value must be a date parsable by <code>new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")</code></li>
426
+
<li>File.class - The string value is the name of the file.</li>
427
+
<li>Number.class - The string value is a number representation can can be converted into an Integer or a Double.</li>
428
+
<li>URL.class - The string value is the textual representation of a URL</li>
429
+
<li>FileInputStream.class - The string value is passed to <code>new FileInputStream(s)</code>.</li>
430
+
<li>Long.class - The string value is a valid argument to <code>Long.parseLong()</code>.</li>
431
+
<li>Integer.class - The string value is a valid argument to <code>Integer.parseInt()</code>.</li>
432
+
<li>Short.class - The string value is a valid argument to <code>Short.parseShort()</code>.</li>
433
+
<li>Byte.class - The string value is a valid argument to <code>Byte.parseByte()</code>.</li>
434
+
<li>Character.class - The string value is either a UTF-8 encoding for a character (e.g. "\\u0124") or the first character from the String."</li>
435
+
<li>Double.class - The string value is a valid argument to <code>Double.parseDouble()</code>.</li>
436
+
<li>Float.class - The string value is a valid argument to <code>Float.parseFloat()</code>.</li>
437
+
<li>BigInteger.class - The string value is a valid argument to <code>new BigInteger(s)</code>.</li>
438
+
<li>BigDecimal.class - The string value is a valid argument to <code>new BigDecimal(s)</code>.</li>
439
+
</ul>
440
+
Additional types may be added to the automatic parsing system by calling <code>TypeHandler.register(Class<T> clazz, Converter<T> converter)</code>.
441
+
The <code>Class<T></code> can be any defined class. The converter is a function that takes a <code>String</code> argument and returns an instance of
442
+
the class. Any expection thrown by the constructor will be caught and reported as a <code>ParseException</code>
443
+
</p>
444
+
<p>
445
+
Conversions can be specified without using the <code>TypeHandler</code> class by specifying the converter
446
+
directly during the option build. For example:
447
+
<source>
448
+
Option fooOpt = Option.builder("foo")
449
+
.hasArg()
450
+
.desc("the foo arg")
451
+
.converter((s) -> new Foo(s))
452
+
.build();
453
+
</source>
454
+
The above will create an option that passes the string value to the Foo constructor when <code>commandLine.getParsedOptionValue(fooOpt)</code> is called.
455
+
</p>
456
+
</section>
457
+
<sectionname="Restricting values">
458
+
<p>
459
+
Acceptable values for options may be specified by using <code>Option.Builder.verifier()</code> to specify a <code>java.util.function.Predicate</code>
460
+
to verify the input text. There are a few examples in the <code>Verifier</code> source file. Verifier also provides a method to construct a verifier
461
+
to test <code>enum</code> values. For example:
462
+
<source>
463
+
public enum MyEnum {ONE, TWO, THREE};
464
+
465
+
Option enumOpt = Option.builder("enumValue")
466
+
.hasArg()
467
+
.desc("the enum arg")
468
+
.varifier(Verifier.enumVerifier(MyEnum.ONE))
469
+
.build();
470
+
</source>
471
+
The above will create an option that verifies any argument passed to <code>enumOpt</code> matches a name from <code>MyEnum</code>.
472
+
The initial verification occures when the option string is set during the execution of <code>CommandLine line = parser.parse(options, args)</code>.
0 commit comments