| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractArgument |
|
| 1.8571428571428572;1.857 |
| 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.argument; | |
| 25 | ||
| 26 | import org.dishevelled.commandline.Argument; | |
| 27 | import org.dishevelled.commandline.CommandLine; | |
| 28 | ||
| 29 | /** | |
| 30 | * Abstract implementation of Argument. | |
| 31 | * | |
| 32 | * @param <E> argument type | |
| 33 | * @author Michael Heuer | |
| 34 | */ | |
| 35 | public abstract class AbstractArgument<E> | |
| 36 | implements Argument<E> | |
| 37 | { | |
| 38 | /** Short name. */ | |
| 39 | private final String shortName; | |
| 40 | ||
| 41 | /** Long name. */ | |
| 42 | private final String longName; | |
| 43 | ||
| 44 | /** Description. */ | |
| 45 | private final String description; | |
| 46 | ||
| 47 | /** Is required. */ | |
| 48 | private final boolean required; | |
| 49 | ||
| 50 | /** Value. */ | |
| 51 | private E value; | |
| 52 | ||
| 53 | /** True if this argument was found. */ | |
| 54 | private boolean found; | |
| 55 | ||
| 56 | ||
| 57 | /** | |
| 58 | * Create a new abstract argument. | |
| 59 | * | |
| 60 | * @param shortName short argument name | |
| 61 | * @param longName long argument name | |
| 62 | * @param description argument description | |
| 63 | * @param required <code>true</code> if this argument is required | |
| 64 | */ | |
| 65 | protected AbstractArgument(final String shortName, | |
| 66 | final String longName, | |
| 67 | final String description, | |
| 68 | final boolean required) | |
| 69 | 233 | { |
| 70 | 233 | this.shortName = shortName; |
| 71 | 233 | this.longName = longName; |
| 72 | 233 | this.description = description; |
| 73 | 233 | this.required = required; |
| 74 | 233 | this.found = false; |
| 75 | 233 | } |
| 76 | ||
| 77 | ||
| 78 | /** {@inheritDoc} */ | |
| 79 | public final void visit(final String current, final CommandLine commandLine) | |
| 80 | throws Exception | |
| 81 | { | |
| 82 | 911 | if (isArgumentString(current)) |
| 83 | { | |
| 84 | 405 | found = true; |
| 85 | 405 | return; |
| 86 | } | |
| 87 | ||
| 88 | 506 | if (isValueString(current)) |
| 89 | { | |
| 90 | 500 | if (commandLine.hasPrevious()) |
| 91 | { | |
| 92 | 500 | commandLine.previous(); |
| 93 | 500 | if (commandLine.hasPrevious()) |
| 94 | { | |
| 95 | 446 | String previous = commandLine.previous(); |
| 96 | 446 | if (isArgumentString(previous)) |
| 97 | { | |
| 98 | 396 | value = convert(current); |
| 99 | } | |
| 100 | 366 | commandLine.next(); |
| 101 | } | |
| 102 | 420 | commandLine.next(); |
| 103 | } | |
| 104 | } | |
| 105 | 426 | } |
| 106 | ||
| 107 | /** | |
| 108 | * Return true if the specified string is an argument name. | |
| 109 | * | |
| 110 | * @param s string | |
| 111 | * @return true if the specified string is an argument name | |
| 112 | */ | |
| 113 | protected boolean isArgumentString(final String s) | |
| 114 | { | |
| 115 | 1357 | return ("-" + getShortName()).equals(s) |
| 116 | || ("--" + getLongName()).equals(s); | |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Return true if the specified string is a value. | |
| 121 | * | |
| 122 | * @param s string | |
| 123 | * @return true if the specified string is a value | |
| 124 | */ | |
| 125 | protected boolean isValueString(final String s) | |
| 126 | { | |
| 127 | 506 | if (s.startsWith("-")) |
| 128 | { | |
| 129 | 58 | char next = s.charAt(1); |
| 130 | 58 | if (Character.isDigit(next)) |
| 131 | { | |
| 132 | // allow negative numbers | |
| 133 | 52 | return true; |
| 134 | } | |
| 135 | 6 | return false; |
| 136 | } | |
| 137 | 448 | return true; |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Convert the specified string value into an instance | |
| 142 | * of class <code>E</code>. | |
| 143 | * | |
| 144 | * @param s string value | |
| 145 | * @return an instance of class <code>E</code> converted | |
| 146 | * from the specified string value | |
| 147 | * @throws Exception if an error occurs | |
| 148 | */ | |
| 149 | protected abstract E convert(final String s) | |
| 150 | throws Exception; | |
| 151 | ||
| 152 | ||
| 153 | /** {@inheritDoc} */ | |
| 154 | public final String getShortName() | |
| 155 | { | |
| 156 | 1805 | return shortName; |
| 157 | } | |
| 158 | ||
| 159 | /** {@inheritDoc} */ | |
| 160 | public final String getLongName() | |
| 161 | { | |
| 162 | 1400 | return longName; |
| 163 | } | |
| 164 | ||
| 165 | /** {@inheritDoc} */ | |
| 166 | public final String getDescription() | |
| 167 | { | |
| 168 | 31 | return description; |
| 169 | } | |
| 170 | ||
| 171 | /** {@inheritDoc} */ | |
| 172 | public final boolean isRequired() | |
| 173 | { | |
| 174 | 437 | return required; |
| 175 | } | |
| 176 | ||
| 177 | /** {@inheritDoc} */ | |
| 178 | public final boolean wasFound() | |
| 179 | { | |
| 180 | 412 | return found; |
| 181 | } | |
| 182 | ||
| 183 | /** {@inheritDoc} */ | |
| 184 | public final E getValue() | |
| 185 | { | |
| 186 | 382 | return value; |
| 187 | } | |
| 188 | ||
| 189 | /** {@inheritDoc} */ | |
| 190 | public final E getValue(final E defaultValue) | |
| 191 | { | |
| 192 | 3 | return found ? value : defaultValue; |
| 193 | } | |
| 194 | ||
| 195 | /** {@inheritDoc} */ | |
| 196 | public final boolean equals(final Object o) | |
| 197 | { | |
| 198 | 28 | return super.equals(o); |
| 199 | } | |
| 200 | ||
| 201 | /** {@inheritDoc} */ | |
| 202 | public final int hashCode() | |
| 203 | { | |
| 204 | 24 | return super.hashCode(); |
| 205 | } | |
| 206 | } |