KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > CommandLine


1 /*
2  * Copyright (c) 2004, Brian Hawkins
3  * brianhks@activeclickweb.com
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21
22
23 package cpmake;
24
25 import java.util.*;
26
27 class CommandLine
28     {
29     HashMap m_optionMap;
30     String JavaDoc[] m_optionList;
31     TreeSet m_boolOptionSet;
32     TreeSet m_valueOptionSet;
33     TreeSet m_invalidOptionSet;
34     Vector m_nonOptions;
35     boolean m_commandLineProcessed;
36     
37     private void processCommandLine()
38         {
39         Character JavaDoc option;
40         if (m_commandLineProcessed)
41             return;
42         
43         for (int I = 0;I < m_optionList.length;I++)
44             {
45             if (m_optionList[I].charAt(0) == '-')
46                 {
47                 option = new Character JavaDoc(m_optionList[I].charAt(1));
48                 if (m_boolOptionSet.contains(option))
49                     m_optionMap.put(option, "");
50                 else if (m_valueOptionSet.contains(option))
51                     {
52                     m_optionMap.put(option, m_optionList[++I]);
53                     }
54                 else
55                     m_invalidOptionSet.add(option);
56                 }
57             else
58                 m_nonOptions.add(m_optionList[I]);
59             }
60         
61         m_commandLineProcessed = true;
62         }
63     
64     public CommandLine(String JavaDoc[] args)
65         {
66         m_optionList = args;
67         m_commandLineProcessed = false;
68         m_optionMap = new HashMap();
69         m_boolOptionSet = new TreeSet();
70         m_valueOptionSet = new TreeSet();
71         m_invalidOptionSet = new TreeSet();
72         m_nonOptions = new Vector();
73         }
74         
75     public void setBooleanOptions(String JavaDoc options)
76         {
77         char[] optArr = options.toCharArray();
78         
79         for(int I = 0;I < optArr.length;I++)
80             m_boolOptionSet.add(new Character JavaDoc(optArr[I]));
81         
82         m_commandLineProcessed = false;
83         }
84         
85     public void setValueOptions(String JavaDoc options)
86         {
87         char[] optArr = options.toCharArray();
88         
89         for(int I = 0;I < optArr.length;I++)
90             m_valueOptionSet.add(new Character JavaDoc(optArr[I]));
91             
92         m_commandLineProcessed = false;
93         }
94         
95     public String JavaDoc[] getNonOptions()
96         {
97         processCommandLine();
98         return ((String JavaDoc[])m_nonOptions.toArray(new String JavaDoc[0]));
99         }
100         
101     public void validateCommandLine()
102             //throws InvalidOptionException
103
{
104         
105         }
106         
107     public boolean isSet(char option)
108         {
109         processCommandLine();
110         return (m_optionMap.containsKey(new Character JavaDoc(option)));
111         }
112         
113     public String JavaDoc getOptionValue(char option)
114         {
115         processCommandLine();
116         return ((String JavaDoc)m_optionMap.get(new Character JavaDoc(option)));
117         }
118     }
Popular Tags