| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BooleanSetArgument |
|
| 5.0;5 |
| 1 | /* | |
| 2 | ||
| 3 | dsh-commandline Command line parser based on typed arguments. | |
| 4 | Copyright (c) 2004-2012 held jointly by the individual authors. | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or modify it | |
| 7 | under the terms of the GNU Lesser General Public License as published | |
| 8 | by the Free Software Foundation; either version 3 of the License, or (at | |
| 9 | your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or | |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
| 14 | License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public License | |
| 17 | along with this library; if not, write to the Free Software Foundation, | |
| 18 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
| 19 | ||
| 20 | > http://www.fsf.org/licensing/licenses/lgpl.html | |
| 21 | > http://www.opensource.org/licenses/lgpl-license.php | |
| 22 | ||
| 23 | */ | |
| 24 | package org.dishevelled.commandline.argument; | |
| 25 | ||
| 26 | import java.util.Set; | |
| 27 | import java.util.HashSet; | |
| 28 | import java.util.StringTokenizer; | |
| 29 | ||
| 30 | import org.apache.commons.lang.StringUtils; | |
| 31 | ||
| 32 | /** | |
| 33 | * A boolean set argument. | |
| 34 | * | |
| 35 | * @author Michael Heuer | |
| 36 | * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $ | |
| 37 | */ | |
| 38 | 64 | public final class BooleanSetArgument |
| 39 | extends AbstractArgument<Set<Boolean>> | |
| 40 | { | |
| 41 | ||
| 42 | /** | |
| 43 | * Create a new boolean set argument. | |
| 44 | * | |
| 45 | * @param shortName short argument name | |
| 46 | * @param longName long argument name | |
| 47 | * @param description argument description | |
| 48 | * @param required <code>true</code> if this argument is required | |
| 49 | */ | |
| 50 | public BooleanSetArgument(final String shortName, | |
| 51 | final String longName, | |
| 52 | final String description, | |
| 53 | final boolean required) | |
| 54 | { | |
| 55 | 7 | super(shortName, longName, description, required); |
| 56 | 7 | } |
| 57 | ||
| 58 | ||
| 59 | /** {@inheritDoc} */ | |
| 60 | protected Set<Boolean> convert(final String s) | |
| 61 | throws Exception | |
| 62 | { | |
| 63 | 64 | Set<Boolean> set = new HashSet<Boolean>(); |
| 64 | 64 | StringTokenizer st = new StringTokenizer(s, ","); |
| 65 | 180 | while (st.hasMoreTokens()) |
| 66 | { | |
| 67 | 124 | String token = StringUtils.stripToEmpty(st.nextToken()); |
| 68 | ||
| 69 | 124 | if ("true".equalsIgnoreCase(token)) |
| 70 | { | |
| 71 | 40 | set.add(Boolean.TRUE); |
| 72 | } | |
| 73 | 84 | else if ("false".equalsIgnoreCase(token)) |
| 74 | { | |
| 75 | 40 | set.add(Boolean.FALSE); |
| 76 | } | |
| 77 | 44 | else if ("t".equalsIgnoreCase(token)) |
| 78 | { | |
| 79 | 12 | set.add(Boolean.TRUE); |
| 80 | } | |
| 81 | 32 | else if ("f".equalsIgnoreCase(token)) |
| 82 | { | |
| 83 | 12 | set.add(Boolean.FALSE); |
| 84 | } | |
| 85 | 20 | else if ("1".equalsIgnoreCase(token)) |
| 86 | { | |
| 87 | 6 | set.add(Boolean.TRUE); |
| 88 | } | |
| 89 | 14 | else if ("0".equalsIgnoreCase(token)) |
| 90 | { | |
| 91 | 6 | set.add(Boolean.FALSE); |
| 92 | } | |
| 93 | else | |
| 94 | { | |
| 95 | 8 | throw new Exception("could not parse " + token + " into a Boolean"); |
| 96 | } | |
| 97 | 116 | } |
| 98 | 56 | return set; |
| 99 | } | |
| 100 | } |