| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Switch |
|
| 1.3333333333333333;1.333 |
| 1 | /* | |
| 2 | ||
| 3 | dsh-commandline Command line parser based on typed arguments. | |
| 4 | Copyright (c) 2004-2014 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; | |
| 25 | ||
| 26 | /** | |
| 27 | * Switch. | |
| 28 | * | |
| 29 | * @author Michael Heuer | |
| 30 | */ | |
| 31 | 0 | public final class Switch |
| 32 | implements Argument<Boolean> | |
| 33 | { | |
| 34 | /** Short name. */ | |
| 35 | private final String shortName; | |
| 36 | ||
| 37 | /** Long name. */ | |
| 38 | private final String longName; | |
| 39 | ||
| 40 | /** Description. */ | |
| 41 | private final String description; | |
| 42 | ||
| 43 | /** True if this switch was found. */ | |
| 44 | private boolean found; | |
| 45 | ||
| 46 | ||
| 47 | /** | |
| 48 | * Create a new switch. | |
| 49 | * | |
| 50 | * @param shortName short switch name | |
| 51 | * @param longName long switch name | |
| 52 | * @param description switch description | |
| 53 | */ | |
| 54 | public Switch(final String shortName, | |
| 55 | final String longName, | |
| 56 | final String description) | |
| 57 | 9 | { |
| 58 | 9 | this.shortName = shortName; |
| 59 | 9 | this.longName = longName; |
| 60 | 9 | this.description = description; |
| 61 | 9 | this.found = false; |
| 62 | 9 | } |
| 63 | ||
| 64 | ||
| 65 | /** {@inheritDoc} */ | |
| 66 | public void visit(final String current, final CommandLine commandLine) | |
| 67 | throws Exception | |
| 68 | { | |
| 69 | 4 | if (("-" + getShortName()).equals(current) || ("--" + getLongName()).equals(current)) |
| 70 | { | |
| 71 | 2 | found = true; |
| 72 | } | |
| 73 | 4 | } |
| 74 | ||
| 75 | /** {@inheritDoc} */ | |
| 76 | public String getShortName() | |
| 77 | { | |
| 78 | 11 | return shortName; |
| 79 | } | |
| 80 | ||
| 81 | /** {@inheritDoc} */ | |
| 82 | public String getLongName() | |
| 83 | { | |
| 84 | 10 | return longName; |
| 85 | } | |
| 86 | ||
| 87 | /** {@inheritDoc} */ | |
| 88 | public String getDescription() | |
| 89 | { | |
| 90 | 1 | return description; |
| 91 | } | |
| 92 | ||
| 93 | /** {@inheritDoc} */ | |
| 94 | public boolean isRequired() | |
| 95 | { | |
| 96 | 4 | return false; |
| 97 | } | |
| 98 | ||
| 99 | /** {@inheritDoc} */ | |
| 100 | public boolean wasFound() | |
| 101 | { | |
| 102 | 1 | return found; |
| 103 | } | |
| 104 | ||
| 105 | /** {@inheritDoc} */ | |
| 106 | public Boolean getValue() | |
| 107 | { | |
| 108 | 4 | return Boolean.valueOf(found); |
| 109 | } | |
| 110 | ||
| 111 | /** {@inheritDoc} */ | |
| 112 | public Boolean getValue(final Boolean defaultValue) | |
| 113 | { | |
| 114 | 4 | return found ? Boolean.TRUE : defaultValue; |
| 115 | } | |
| 116 | } |