diff options
author | Cedric Beust <cedric@beust.com> | 2011-06-29 16:31:43 -0700 |
---|---|---|
committer | Cedric Beust <cedric@beust.com> | 2011-06-29 16:31:43 -0700 |
commit | c87c81877428e84104613d50aa47222643422e17 (patch) | |
tree | ab2093cda2a01a8a639c5c535e47daedacc9d1dd /doc | |
parent | 1af343c624b09d38ef4cb79805a09dffd75edee2 (diff) | |
parent | fe8a696f6c65438505eb26d8b734d34910932ef7 (diff) | |
download | jcommander-c87c81877428e84104613d50aa47222643422e17.tar.gz |
Merge branch 'master' of github.com:cbeust/jcommander
Diffstat (limited to 'doc')
-rw-r--r-- | doc/index.html | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/doc/index.html b/doc/index.html index 21ffb09..7be5d96 100644 --- a/doc/index.html +++ b/doc/index.html @@ -258,6 +258,48 @@ All you need to do is add the factory to your JCommander object: Another advantage of using string converter factories is that your factories can come from a dependency injection framework. +<h2><a class="section" name="Parameter_validation">Parameter validation</a></h2> + +You can ask JCommander to perform early validation on your parameters by providing a class that implements the following interface: + +<pre class="brush:java"> +public interface IParameterValidator { + /** + * Validate the parameter. + * + * @param name The name of the parameter (e.g. "-host"). + * @param value The value of the parameter that we need to validate + * + * @throws ParameterException Thrown if the value of the parameter is invalid. + */ + void validate(String name, String value) throws ParameterException; +} + +</pre> + +Here is an example implementation that will make sure that the parameter is a positive integer: + +<pre class="brush:java"> +public class PositiveInteger implements IParameterValidator { + public void validate(String name, String value) + throws ParameterException { + int n = Integer.parseInt(value); + if (n < 0) { + throw new ParameterException("Parameter " + name + " should be positive (found " + value +")"); + } + } +} +</pre> + +Specify the name of a class implementing this interface in the <tt>validateWith</tt> attribute of your <tt>@Parameter</tt> annotations: + +<pre class="brush:java"> +@Parameter(names = "-age", validateWith = PositiveInteger.class) +public Integer age; +</pre> + +Attempting to pass a negative integer to this option will cause a <tt>ParameterException</tt> to be thrown. + <h2><a class="section" name="Main_parameter">Main parameter</a></h2> So far, all the <tt>@Parameter</tt> annotations we have seen had defined an attribute called <tt>names</tt>. You can define one (and at most one) parameter without any such attribute. This parameter needs to be a <tt>List<String></tt> and it will contain all the parameters that are not options: @@ -638,7 +680,7 @@ TestNG uses JCommander to parse its command line, here is <a href="http://github <h2><a class="section" name="Javadocs">Javadocs</a></h2> -The Javadocs for JCommander can be found <a href="http://beust.com/jcommander/apidocs/">here</a>. +The Javadocs for JCommander can be found <a href="apidocs/">here</a>. <h2><a class="section" name="Download">Download</a></h2> @@ -646,7 +688,7 @@ You can download JCommander from the following locations: <ul> <li><a href="http://github.com/cbeust/jcommander">Source on github</a></li> - <li><a href="http://beust.com/jcommander/jcommander-1.7.jar">Jar file</a></li> + <li><a href="jcommander-1.13.jar">Jar file</a></li> <li>Or if you are using Maven, add the following dependency to your <tt>pom.xml</tt>: <pre class="brush: xml"> @@ -654,7 +696,7 @@ You can download JCommander from the following locations: <dependency> <groupId>com.beust</groupId> <artifactId>jcommander</artifactId> - <version>1.7</version> + <version>1.17</version> </dependency> </pre> |