|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.installer.utils; |
|
20 |
|
21 import com.nokia.mj.impl.installer.utils.InstallerException; |
|
22 import com.nokia.mj.impl.installer.utils.Log; |
|
23 import com.nokia.mj.impl.utils.Tokenizer; |
|
24 |
|
25 import java.util.Hashtable; |
|
26 |
|
27 /** |
|
28 * Class for handling command line arguments. |
|
29 */ |
|
30 public class Args |
|
31 { |
|
32 private Hashtable iArgs = null; |
|
33 |
|
34 public Args(String[] aArgs) |
|
35 { |
|
36 iArgs = new Hashtable(); |
|
37 for (int i = 0; i < aArgs.length; i++) |
|
38 { |
|
39 if (aArgs[i].startsWith("-")) |
|
40 { |
|
41 String arg = aArgs[i].substring(1); |
|
42 String value = null; |
|
43 int separator = arg.indexOf("="); |
|
44 if (separator > 0) |
|
45 { |
|
46 value = arg.substring(separator+1); |
|
47 arg = arg.substring(0, separator); |
|
48 } |
|
49 else |
|
50 { |
|
51 value = ""; // Arg is present without value. |
|
52 } |
|
53 //if (i+1 < aArgs.length) { |
|
54 // value = aArgs[i+1]; |
|
55 //} else { |
|
56 // value = ""; // Arg is present without value. |
|
57 //} |
|
58 iArgs.put(arg, value); |
|
59 //Log.log("Args: " + arg + "=" + value); |
|
60 } |
|
61 } |
|
62 } |
|
63 |
|
64 /** |
|
65 * Returns command line argument with the specified name. |
|
66 * If argument is not found, returns null. |
|
67 */ |
|
68 public String get(String aName) |
|
69 { |
|
70 String arg = (String)iArgs.get(aName); |
|
71 if (arg != null) |
|
72 { |
|
73 if (arg.length() == 0) |
|
74 { |
|
75 Log.log("Args: " + aName); |
|
76 } |
|
77 else |
|
78 { |
|
79 Log.log("Args: " + aName + "=" + arg); |
|
80 } |
|
81 } |
|
82 return arg; |
|
83 } |
|
84 |
|
85 /** |
|
86 * Returns command line argument with the specified name. |
|
87 * If argument is not found, returns default value. |
|
88 */ |
|
89 public String get(String aName, String aDefault) |
|
90 { |
|
91 String arg = get(aName); |
|
92 if (arg == null) |
|
93 { |
|
94 arg = aDefault; |
|
95 if (arg != null) |
|
96 { |
|
97 if (arg.length() == 0) |
|
98 { |
|
99 Log.log("Args: " + aName + " (default)"); |
|
100 } |
|
101 else |
|
102 { |
|
103 Log.log("Args: " + aName + "=" + arg + " (default)"); |
|
104 } |
|
105 } |
|
106 } |
|
107 return arg; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Returns boolean type command line argument with the specified name. |
|
112 * If argument is not found, returns null. |
|
113 */ |
|
114 public Boolean getBoolean(String aName) |
|
115 { |
|
116 Boolean arg = null; |
|
117 if (valueContains(aName, "no")) |
|
118 { |
|
119 Log.log("Args: " + aName + " disabled"); |
|
120 arg = new Boolean(false); |
|
121 } |
|
122 else if (valueContains(aName, "yes")) |
|
123 { |
|
124 Log.log("Args: " + aName + " enabled"); |
|
125 arg = new Boolean(true); |
|
126 } |
|
127 return arg; |
|
128 } |
|
129 |
|
130 /** |
|
131 * Returns boolean type command line argument with the specified name. |
|
132 * If argument is not found, returns default value. |
|
133 */ |
|
134 public Boolean getBoolean(String aName, Boolean aDefault) |
|
135 { |
|
136 Boolean arg = getBoolean(aName); |
|
137 if (arg == null) |
|
138 { |
|
139 arg = aDefault; |
|
140 } |
|
141 return arg; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Returns boolean type command line argument with the specified name. |
|
146 * If argument is not found, returns default value. |
|
147 */ |
|
148 public boolean getBoolean(String aName, boolean aDefault) |
|
149 { |
|
150 Boolean arg = getBoolean(aName); |
|
151 if (arg == null) |
|
152 { |
|
153 return aDefault; |
|
154 } |
|
155 return arg.booleanValue(); |
|
156 } |
|
157 |
|
158 /** |
|
159 * Returns true if command line argument with specified name |
|
160 * exists, and if its value contains the specified value part. |
|
161 * Value parts are separated with commas, for example argument |
|
162 * "-arg=foo,bar" has two value parts. |
|
163 */ |
|
164 public boolean valueContains(String aName, String aValuePart) |
|
165 { |
|
166 String value = get(aName); |
|
167 if (value == null || value.length() == 0) |
|
168 { |
|
169 return false; |
|
170 } |
|
171 String[] tokens = Tokenizer.split(value, ","); |
|
172 for (int i = 0; i < tokens.length; i++) |
|
173 { |
|
174 if (tokens[i].equals(aValuePart)) |
|
175 { |
|
176 return true; |
|
177 } |
|
178 } |
|
179 return false; |
|
180 } |
|
181 |
|
182 /** |
|
183 * Returns drive id from given string. |
|
184 */ |
|
185 public static int parseDrive(String aDrive) throws InstallerException |
|
186 { |
|
187 int drive = -1; |
|
188 try |
|
189 { |
|
190 drive = Integer.parseInt(aDrive); |
|
191 } |
|
192 catch (NumberFormatException nfe) |
|
193 { |
|
194 // Drive argument is not a number, assume it is a string |
|
195 // of length one indicating drive letter. |
|
196 } |
|
197 if (drive == -1) |
|
198 { |
|
199 if (aDrive.length() != 1) |
|
200 { |
|
201 InstallerException.internalError( |
|
202 "Invalid drive argument: " + aDrive); |
|
203 } |
|
204 drive = aDrive.toLowerCase().charAt(0) - 'a'; |
|
205 } |
|
206 Log.log("Parsed drive " + aDrive + " --> " + drive); |
|
207 return drive; |
|
208 } |
|
209 } |