2 // CommandLineBuilder.cs: Builds command line options string
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
32 using System.Text.RegularExpressions;
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.Utilities
37 public class CommandLineBuilder
39 StringBuilder commandLine;
42 static CommandLineBuilder ()
44 chars = new char [] {' ', '\t', '\n', '\u000b', '\u000c', '\'', '\"', ';'};
47 public CommandLineBuilder ()
49 commandLine = new StringBuilder ();
52 public void AppendFileNameIfNotNull (string fileName)
57 VerifyThrowNoEmbeddedDoubleQuotes (null, fileName);
58 AppendSpaceIfNotEmpty ();
59 AppendFileNameWithQuoting (fileName);
62 public void AppendFileNameIfNotNull (ITaskItem fileItem)
67 string filename = fileItem.ToString ();
68 VerifyThrowNoEmbeddedDoubleQuotes (null, filename);
69 AppendSpaceIfNotEmpty ();
70 AppendFileNameWithQuoting (filename);
73 public void AppendFileNamesIfNotNull (string[] fileNames,
76 if (delimiter == null)
77 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
79 if (fileNames == null)
82 bool appendDelimiter = false;
83 AppendSpaceIfNotEmpty ();
84 for (int i = 0; i < fileNames.Length; i++) {
85 string filename = fileNames [i];
88 VerifyThrowNoEmbeddedDoubleQuotes (null, filename);
89 if (appendDelimiter) {
90 commandLine.Append (delimiter);
91 AppendFileNameWithQuoting (filename);
93 AppendFileNameWithQuoting (filename);
94 appendDelimiter = true;
99 public void AppendFileNamesIfNotNull (ITaskItem[] fileItems,
102 if (delimiter == null)
103 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
105 if (fileItems == null)
108 bool appendDelimiter = false;
109 AppendSpaceIfNotEmpty ();
110 for (int i = 0; i < fileItems.Length; i++) {
111 string filename = fileItems [i].ToString ();
112 if (fileItems [i] == null)
115 VerifyThrowNoEmbeddedDoubleQuotes (null, filename);
116 if (appendDelimiter) {
117 commandLine.Append (delimiter);
118 AppendFileNameWithQuoting (filename);
120 AppendFileNameWithQuoting (filename);
121 appendDelimiter = true;
126 protected void AppendFileNameWithQuoting (string fileName)
128 if (fileName == null)
131 if (IsQuotingRequired (fileName))
132 commandLine.AppendFormat ("\"{0}\"",fileName);
134 commandLine.Append (fileName);
137 protected void AppendSpaceIfNotEmpty ()
139 if (commandLine.Length != 0)
140 commandLine.Append (' ');
143 public void AppendSwitch (string switchName)
145 if (switchName == null)
146 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
148 AppendSpaceIfNotEmpty ();
149 commandLine.Append (switchName);
152 public void AppendSwitchIfNotNull (string switchName,
155 if (switchName == null)
156 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
158 if (parameter == null)
161 VerifyThrowNoEmbeddedDoubleQuotes (switchName, parameter);
162 AppendSpaceIfNotEmpty ();
163 commandLine.Append (switchName);
164 AppendTextWithQuoting (parameter);
167 public void AppendSwitchIfNotNull (string switchName,
170 if (switchName == null)
171 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
173 if (parameter == null)
176 string value = parameter.ToString ();
177 VerifyThrowNoEmbeddedDoubleQuotes (switchName, value);
178 AppendSpaceIfNotEmpty ();
179 commandLine.Append (switchName);
180 AppendTextWithQuoting (value);
183 public void AppendSwitchIfNotNull (string switchName,
187 if (switchName == null)
188 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
190 if (delimiter == null)
191 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
193 if (parameters == null)
196 AppendSpaceIfNotEmpty ();
197 commandLine.AppendFormat ("{0}",switchName);
198 bool appendDelimiter = false;
199 for (int i = 0; i < parameters.Length; i++) {
200 string value = parameters [i];
204 VerifyThrowNoEmbeddedDoubleQuotes (switchName, value);
205 if (appendDelimiter) {
206 commandLine.Append (delimiter);
207 AppendTextWithQuoting (value);
209 AppendTextWithQuoting (value);
210 appendDelimiter = true;
215 public void AppendSwitchIfNotNull (string switchName,
216 ITaskItem[] parameters,
219 if (switchName == null)
220 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
222 if (delimiter == null)
223 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
225 if (parameters == null)
228 AppendSpaceIfNotEmpty ();
229 commandLine.AppendFormat ("{0}",switchName);
230 bool appendDelimiter = false;
231 for (int i = 0; i < parameters.Length; i++) {
232 string value = parameters [i].ToString ();
236 VerifyThrowNoEmbeddedDoubleQuotes (switchName, value);
237 if (appendDelimiter) {
238 commandLine.Append (delimiter);
239 AppendTextWithQuoting (value);
241 AppendTextWithQuoting (value);
242 appendDelimiter = true;
247 public void AppendSwitchUnquotedIfNotNull (string switchName,
250 if (switchName == null)
251 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
253 if (parameter == null)
256 AppendSpaceIfNotEmpty ();
257 commandLine.AppendFormat ("{0}{1}", switchName, parameter);
260 public void AppendSwitchUnquotedIfNotNull (string switchName,
263 if (switchName == null)
264 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
266 if (parameter == null)
269 AppendSpaceIfNotEmpty ();
270 commandLine.AppendFormat ("{0}{1}", switchName, parameter.ItemSpec);
273 public void AppendSwitchUnquotedIfNotNull (string switchName,
277 if (switchName == null)
278 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
280 if (delimiter == null)
281 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
283 if (parameters == null)
286 AppendSpaceIfNotEmpty ();
287 commandLine.AppendFormat ("{0}",switchName);
288 bool appendDelimiter = false;
289 for (int i = 0; i < parameters.Length; i++) {
290 if (parameters [i] == null)
292 if (appendDelimiter) {
293 commandLine.Append (delimiter);
294 commandLine.Append (parameters [i]);
296 commandLine.Append (parameters [i]);
297 appendDelimiter = true;
302 public void AppendSwitchUnquotedIfNotNull (string switchName,
303 ITaskItem[] parameters,
306 if (switchName == null)
307 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
309 if (delimiter == null)
310 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
312 if (parameters == null)
315 AppendSpaceIfNotEmpty ();
316 commandLine.AppendFormat ("{0}",switchName);
317 bool appendDelimiter = false;
318 for (int i = 0; i < parameters.Length; i++) {
319 if (parameters [i] == null)
321 if (appendDelimiter) {
322 commandLine.Append (delimiter);
323 commandLine.Append (parameters [i].ToString ());
325 commandLine.Append (parameters [i].ToString ());
326 appendDelimiter = true;
332 void AppendTextUnquoted (string textToAppend)
334 commandLine.Append (textToAppend);
337 protected void AppendTextWithQuoting (string textToAppend)
339 if (textToAppend == null)
342 if (IsQuotingRequired (textToAppend))
343 commandLine.AppendFormat ("\"{0}\"",textToAppend);
345 commandLine.Append (textToAppend);
348 protected virtual bool IsQuotingRequired (string parameter)
350 return parameter != null && parameter.IndexOfAny (chars) >= 0;
353 protected virtual void VerifyThrowNoEmbeddedDoubleQuotes (string switchName,
356 if (parameter != null && parameter.IndexOf ('"') >= 0)
357 throw new ArgumentException (
358 String.Format ("Illegal quote passed to the command line switch named \"{0}\". The value was [{1}].",
359 switchName, parameter));
362 public override string ToString ()
364 return commandLine.ToString ();
367 protected StringBuilder CommandLine {