| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CommandLineParser |
|
| 7.0;7 |
| 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; | |
| 25 | ||
| 26 | /** | |
| 27 | * Command line parser. | |
| 28 | * | |
| 29 | * @author Michael Heuer | |
| 30 | * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $ | |
| 31 | */ | |
| 32 | public final class CommandLineParser | |
| 33 | { | |
| 34 | ||
| 35 | /** | |
| 36 | * Private no-arg constructor. | |
| 37 | */ | |
| 38 | private CommandLineParser() | |
| 39 | 0 | { |
| 40 | // empty | |
| 41 | 0 | } |
| 42 | ||
| 43 | ||
| 44 | /** | |
| 45 | * Parse the specified command line according to the specified | |
| 46 | * list of arguments. | |
| 47 | * | |
| 48 | * @param commandLine command line, must not be null | |
| 49 | * @param arguments list of arguments, must not be null | |
| 50 | * @throws CommandLineParseException if an error occurs | |
| 51 | */ | |
| 52 | public static void parse(final CommandLine commandLine, final ArgumentList arguments) | |
| 53 | throws CommandLineParseException | |
| 54 | { | |
| 55 | 464 | if (commandLine == null) |
| 56 | { | |
| 57 | 1 | throw new IllegalArgumentException("commandLine must not be null"); |
| 58 | } | |
| 59 | 463 | if (arguments == null) |
| 60 | { | |
| 61 | 1 | throw new IllegalArgumentException("arguments must not be null"); |
| 62 | } | |
| 63 | ||
| 64 | try | |
| 65 | { | |
| 66 | 1297 | while (commandLine.hasNext()) |
| 67 | { | |
| 68 | 915 | String s = commandLine.next(); |
| 69 | ||
| 70 | 915 | for (Argument<?> a : arguments) |
| 71 | { | |
| 72 | 915 | a.visit(s, commandLine); |
| 73 | } | |
| 74 | 835 | } |
| 75 | } | |
| 76 | 80 | catch (Exception e) |
| 77 | { | |
| 78 | 80 | throw new CommandLineParseException(e); |
| 79 | 382 | } |
| 80 | ||
| 81 | 382 | for (Argument<?> a : arguments) |
| 82 | { | |
| 83 | 382 | if (a.isRequired() && !(a.wasFound())) |
| 84 | { | |
| 85 | 27 | StringBuffer sb = new StringBuffer(); |
| 86 | 27 | sb.append("required argument -"); |
| 87 | 27 | sb.append(a.getShortName()); |
| 88 | 27 | sb.append(", --"); |
| 89 | 27 | sb.append(a.getLongName()); |
| 90 | 27 | sb.append(" not found"); |
| 91 | ||
| 92 | 27 | throw new CommandLineParseException(sb.toString()); |
| 93 | } | |
| 94 | } | |
| 95 | 355 | } |
| 96 | } |