KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
27
28 class ParsedCommandLineImpl implements ParsedCommandLine
29 {
30     String JavaDoc[] argv;
31     String JavaDoc switchPrefix;
32
33     String JavaDoc[] unswitchedArgs;
34
35     //we are relying upon the fact that
36
//HashMaps are null-accepting collections
37
HashMap foundSwitches = new HashMap();
38     
39     ParsedCommandLineImpl(String JavaDoc[] argv,
40               String JavaDoc switchPrefix,
41               String JavaDoc[] validSwitches,
42               String JavaDoc[] requiredSwitches,
43               String JavaDoc[] argSwitches)
44     throws BadCommandLineException
45     {
46     this.argv = argv;
47     this.switchPrefix = switchPrefix;
48
49     List unswitchedArgsList = new LinkedList();
50     int sp_len = switchPrefix.length();
51
52     for (int i = 0; i < argv.length; ++i)
53         {
54         if (argv[i].startsWith(switchPrefix)) //okay, this is a switch
55
{
56             String JavaDoc sw = argv[i].substring( sp_len );
57             String JavaDoc arg = null;
58
59             int eq = sw.indexOf('=');
60             if ( eq >= 0 ) //equals convention
61
{
62                 arg = sw.substring( eq + 1 );
63                 sw = sw.substring( 0, eq );
64             }
65             else if ( contains( sw, argSwitches ) ) //we expect an argument, next arg convention
66
{
67                 if (i < argv.length - 1 && !argv[i + 1].startsWith( switchPrefix) )
68                 arg = argv[++i];
69             }
70
71             if (validSwitches != null && ! contains( sw, validSwitches ) )
72             throw new UnexpectedSwitchException("Unexpected Switch: " + sw, sw);
73             if (argSwitches != null && arg != null && ! contains( sw, argSwitches ))
74             throw new UnexpectedSwitchArgumentException("Switch \"" + sw +
75                                     "\" should not have an " +
76                                     "argument. Argument \"" +
77                                     arg + "\" found.", sw, arg);
78             foundSwitches.put( sw, arg );
79         }
80         else
81             unswitchedArgsList.add( argv[i] );
82         }
83
84     if (requiredSwitches != null)
85         {
86         for (int i = 0; i < requiredSwitches.length; ++i)
87             if (! foundSwitches.containsKey( requiredSwitches[i] ))
88             throw new MissingSwitchException("Required switch \"" + requiredSwitches[i] +
89                              "\" not found.", requiredSwitches[i]);
90         }
91
92     unswitchedArgs = new String JavaDoc[ unswitchedArgsList.size() ];
93     unswitchedArgsList.toArray( unswitchedArgs );
94     }
95
96     public String JavaDoc getSwitchPrefix()
97     { return switchPrefix; }
98
99     public String JavaDoc[] getRawArgs()
100     { return (String JavaDoc[]) argv.clone(); }
101     
102     public boolean includesSwitch(String JavaDoc sw)
103     { return foundSwitches.containsKey( sw ); }
104
105     public String JavaDoc getSwitchArg(String JavaDoc sw)
106     { return (String JavaDoc) foundSwitches.get(sw); }
107
108     public String JavaDoc[] getUnswitchedArgs()
109     { return (String JavaDoc[]) unswitchedArgs.clone(); }
110
111     private static boolean contains(String JavaDoc string, String JavaDoc[] list)
112     {
113     for (int i = list.length; --i >= 0;)
114         if (list[i].equals(string)) return true;
115     return false;
116     }
117     
118 }
119
120
121
122
123
124
125
Popular Tags