Skip to content

Commit

Permalink
DOC: fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Aug 9, 2018
1 parent 73098fc commit 2230750
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@ assert counter.count == 3; // method was invoked with command line value

Methods annotated with `@Option` and `@Parameters` can do simple input validation by throwing a `ParameterException` when invalid values are specified on the command line.

[source,java
[source,java]
----
class ValidationExample {
private Map<String, String> properties = new LinkedHashMap<>();
Expand Down
31 changes: 14 additions & 17 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3900,35 +3900,32 @@ <h4 id="_annotating_methods_of_a_concrete_class">15.1.2. Annotating Methods of a
<div class="paragraph">
<p>Methods annotated with <code>@Option</code> and <code>@Parameters</code> can do simple input validation by throwing a <code>ParameterException</code> when invalid values are specified on the command line.</p>
</div>
<div class="paragraph">
<p>[source,java</p>
</div>
<div class="listingblock">
<div class="content">
<pre>class ValidationExample {
private Map&lt;String, String&gt; properties = new LinkedHashMap&lt;&gt;();
<pre class="CodeRay highlight"><code data-lang="java"><span class="type">class</span> <span class="class">ValidationExample</span> {
<span class="directive">private</span> <span class="predefined-type">Map</span>&lt;<span class="predefined-type">String</span>, <span class="predefined-type">String</span>&gt; properties = <span class="keyword">new</span> <span class="predefined-type">LinkedHashMap</span>&lt;&gt;();

@Spec private CommandSpec spec; // injected by picocli
<span class="annotation">@Spec</span> <span class="directive">private</span> CommandSpec spec; <span class="comment">// injected by picocli</span>

@Option(names = {"-D", "--property"}, paramLabel = "KEY=VALUE")
public void setProperty(Map&lt;String, String&gt; map) {
for (String key : map.keySet()) {
String newValue = map.get(key);
<span class="annotation">@Option</span>(names = {<span class="string"><span class="delimiter">&quot;</span><span class="content">-D</span><span class="delimiter">&quot;</span></span>, <span class="string"><span class="delimiter">&quot;</span><span class="content">--property</span><span class="delimiter">&quot;</span></span>}, paramLabel = <span class="string"><span class="delimiter">&quot;</span><span class="content">KEY=VALUE</span><span class="delimiter">&quot;</span></span>)
<span class="directive">public</span> <span class="type">void</span> setProperty(<span class="predefined-type">Map</span>&lt;<span class="predefined-type">String</span>, <span class="predefined-type">String</span>&gt; map) {
<span class="keyword">for</span> (<span class="predefined-type">String</span> key : map.keySet()) {
<span class="predefined-type">String</span> newValue = map.get(key);
validateUnique(key, newValue);
properties.put(key, newValue);
}
}

private void validateUnique(String key, String newValue) {
String existing = properties.get(key);
if (existing != null &amp;&amp; !existing.equals(newValue)) {
throw new ParameterException(spec.commandLine(),
String.format("Duplicate key '%s' for values '%s' and '%s'.",
<span class="directive">private</span> <span class="type">void</span> validateUnique(<span class="predefined-type">String</span> key, <span class="predefined-type">String</span> newValue) {
<span class="predefined-type">String</span> existing = properties.get(key);
<span class="keyword">if</span> (existing != <span class="predefined-constant">null</span> &amp;&amp; !existing.equals(newValue)) {
<span class="keyword">throw</span> <span class="keyword">new</span> ParameterException(spec.commandLine(),
<span class="predefined-type">String</span>.format(<span class="string"><span class="delimiter">&quot;</span><span class="content">Duplicate key '%s' for values '%s' and '%s'.</span><span class="delimiter">&quot;</span></span>,
key, existing, newValue));
}
}
// ...
}</pre>
<span class="comment">// ...</span>
}</code></pre>
</div>
</div>
</div>
Expand Down

0 comments on commit 2230750

Please sign in to comment.