01: /*
02: * Distributed as part of debuggen v.0.1.0
03: *
04: * Copyright (C) 2005 Machinery For Change, Inc.
05: *
06: * Author: Steve Waldman <swaldman@mchange.com>
07: *
08: * This library is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU Lesser General Public License version 2.1, as
10: * published by the Free Software Foundation.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public License
18: * along with this software; see the file LICENSE. If not, write to the
19: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: */
22:
23: package com.mchange.v2.cmdline;
24:
25: public final class CommandLineUtils {
26: /**
27: * "Parses" a command line by making use several conventions:
28: * <UL>
29: * <LI> Certain arguments are considered "switches", by virtue
30: * of being prefixed with some string, usually "-", "/", or "--"
31: * <LI> Switches may have arguments associated with them. This implementation
32: * permits only a single argument per switch
33: * <LI> Switch arguments are determined via two conventions:
34: * <OL>
35: * <LI> If a switch is of the form "--switch=value" (where "--" is
36: * set as the switch prefix), value is the switches argument.
37: * <LI> If a switch is not of this form (simply "--switch"), then the
38: * following item on the command line is considered the switch's
39: * argument if and only if
40: * <OL>
41: * <LI> the argSwitches array contains the switch, and
42: * <LI> the next item on the command line is not itself a switch
43: * </OL>
44: * </OL>
45: * </UL>
46: *
47: * @param argv the entire list of arguments, usually the argument to a main function
48: * @param switchPrefix the string which separates "switches" from regular command line args.
49: * Must be non-null
50: * @param validSwitches a list of all the switches permissible for this command line.
51: * If non-null, an UnexpectedSwitchException will be thrown if a switch not
52: * in this list is encountered. Use null to accept any switches.
53: * @param requiredSwitches a list of all the switches required by this command line.
54: * If non-null, an MissingSwitchException will be thrown if a switch
55: * in this list is not present. Use null if no switches should be considered required.
56: * @param argSwitches a list of switches that should have an argument associated with them
57: * If non-null, an MissingSwitchArgumentException will be thrown if a switch
58: * in this list has no argument is not present. Use null if no switches should
59: * be considered to require arguments. However, this parameter is required if
60: * distinct items on a command line should be considered arguments to preceding
61: * items. (For example, "f" must be an argSwitch for "-f myfile.txt" to be parsed
62: * as switch and argument, but argSwitches is not required to parse "--file=myfile.txt"
63: */
64: public static ParsedCommandLine parse(String[] argv,
65: String switchPrefix, String[] validSwitches,
66: String[] requiredSwitches, String[] argSwitches)
67: throws BadCommandLineException {
68: return new ParsedCommandLineImpl(argv, switchPrefix,
69: validSwitches, requiredSwitches, argSwitches);
70: }
71:
72: private CommandLineUtils() {
73: }
74: }
|