KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > cmdline > CommandLineUtils


1 /*
2  * Distributed as part of debuggen v.0.1.0
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * 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
24 package com.mchange.v2.cmdline;
25
26 public final class CommandLineUtils
27 {
28     /**
29      * "Parses" a command line by making use several conventions:
30      * <UL>
31      * <LI> Certain arguments are considered "switches", by virtue
32      * of being prefixed with some string, usually "-", "/", or "--"
33      * <LI> Switches may have arguments associated with them. This implementation
34      * permits only a single argument per switch
35      * <LI> Switch arguments are determined via two conventions:
36      * <OL>
37      * <LI> If a switch is of the form "--switch=value" (where "--" is
38      * set as the switch prefix), value is the switches argument.
39      * <LI> If a switch is not of this form (simply "--switch"), then the
40      * following item on the command line is considered the switch's
41      * argument if and only if
42      * <OL>
43      * <LI> the argSwitches array contains the switch, and
44      * <LI> the next item on the command line is not itself a switch
45      * </OL>
46      * </OL>
47      * </UL>
48      *
49      * @param argv the entire list of arguments, usually the argument to a main function
50      * @param switchPrefix the string which separates "switches" from regular command line args.
51      * Must be non-null
52      * @param validSwitches a list of all the switches permissible for this command line.
53      * If non-null, an UnexpectedSwitchException will be thrown if a switch not
54      * in this list is encountered. Use null to accept any switches.
55      * @param requiredSwitches a list of all the switches required by this command line.
56      * If non-null, an MissingSwitchException will be thrown if a switch
57      * in this list is not present. Use null if no switches should be considered required.
58      * @param argSwitches a list of switches that should have an argument associated with them
59      * If non-null, an MissingSwitchArgumentException will be thrown if a switch
60      * in this list has no argument is not present. Use null if no switches should
61      * be considered to require arguments. However, this parameter is required if
62      * distinct items on a command line should be considered arguments to preceding
63      * items. (For example, "f" must be an argSwitch for "-f myfile.txt" to be parsed
64      * as switch and argument, but argSwitches is not required to parse "--file=myfile.txt"
65      */

66     public static ParsedCommandLine parse(String JavaDoc[] argv,
67                    String JavaDoc switchPrefix,
68                    String JavaDoc[] validSwitches,
69                    String JavaDoc[] requiredSwitches,
70                    String JavaDoc[] argSwitches)
71     throws BadCommandLineException
72     {
73     return new ParsedCommandLineImpl( argv,
74                       switchPrefix,
75                       validSwitches,
76                       requiredSwitches,
77                       argSwitches );
78     }
79
80     private CommandLineUtils()
81     {}
82 }
83
84
85
86
87
Popular Tags