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.
32 using Microsoft.Build.Framework;
34 namespace Microsoft.Build.Utilities
36 public class CommandLineBuilder
38 StringBuilder commandLine;
40 public CommandLineBuilder ()
42 commandLine = new StringBuilder ();
45 public void AppendFileNameIfNotNull (string fileName)
50 AppendSpaceIfNotEmpty ();
51 commandLine.Append (fileName);
54 public void AppendFileNameIfNotNull (ITaskItem fileItem)
59 AppendSpaceIfNotEmpty ();
60 commandLine.Append (fileItem.ToString());
63 public void AppendFileNamesIfNotNull (string[] fileNames,
66 if (delimiter == null)
67 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
69 if (fileNames == null)
72 bool appendDelimiter = false;
73 AppendSpaceIfNotEmpty ();
74 for (int i = 0; i < fileNames.Length; i++) {
75 if (fileNames [i] == null)
77 if (appendDelimiter) {
78 commandLine.Append (delimiter);
79 commandLine.Append (fileNames [i]);
81 commandLine.Append (fileNames [i]);
82 appendDelimiter = true;
87 public void AppendFileNamesIfNotNull (ITaskItem[] fileItems,
90 if (delimiter == null)
91 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
93 if (fileItems == null)
96 bool appendDelimiter = false;
97 AppendSpaceIfNotEmpty ();
98 for (int i = 0; i < fileItems.Length; i++) {
99 if (fileItems [i] == null)
101 if (appendDelimiter) {
102 commandLine.Append (delimiter);
103 commandLine.Append (fileItems [i].ToString ());
105 commandLine.Append (fileItems [i].ToString ());
106 appendDelimiter = true;
111 protected void AppendFileNameWithQuoting (string fileName)
113 if (IsQuotingRequired (fileName))
114 commandLine.AppendFormat ("\"{0}\"",fileName);
116 commandLine.Append (fileName);
119 protected void AppendSpaceIfNotEmpty ()
121 if (commandLine.Length != 0)
122 commandLine.Append (' ');
125 public void AppendSwitch (string switchName)
127 if (switchName == null)
128 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
130 AppendSpaceIfNotEmpty ();
131 commandLine.Append (switchName);
134 public void AppendSwitchIfNotNull (string switchName,
137 if (switchName == null)
138 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
140 if (parameter == null)
143 AppendSpaceIfNotEmpty ();
144 commandLine.AppendFormat ("{0}{1}",switchName,
148 public void AppendSwitchIfNotNull (string switchName,
151 if (switchName == null)
152 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
154 if (parameter == null)
157 AppendSpaceIfNotEmpty ();
158 commandLine.AppendFormat ("{0}{1}",switchName,
159 parameter.ToString ());
162 public void AppendSwitchIfNotNull (string switchName,
166 if (switchName == null)
167 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
169 if (delimiter == null)
170 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
172 if (parameters == null)
175 AppendSpaceIfNotEmpty ();
176 commandLine.AppendFormat ("{0}",switchName);
177 bool appendDelimiter = false;
178 for (int i = 0; i < parameters.Length; i++) {
179 if (parameters [i] == null)
181 if (appendDelimiter) {
182 commandLine.Append (delimiter);
183 commandLine.Append (parameters [i]);
185 commandLine.Append (parameters [i]);
186 appendDelimiter = true;
191 public void AppendSwitchIfNotNull (string switchName,
192 ITaskItem[] parameters,
195 if (switchName == null)
196 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
198 if (delimiter == null)
199 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
201 if (parameters == null)
204 AppendSpaceIfNotEmpty ();
205 commandLine.AppendFormat ("{0}",switchName);
206 bool appendDelimiter = false;
207 for (int i = 0; i < parameters.Length; i++) {
208 if (parameters [i] == null)
210 if (appendDelimiter) {
211 commandLine.Append (delimiter);
212 commandLine.Append (parameters [i].ToString ());
214 commandLine.Append (parameters [i].ToString ());
215 appendDelimiter = true;
220 public void AppendSwitchUnquotedIfNotNull (string switchName,
223 if (switchName == null)
224 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
226 if (parameter == null)
229 AppendSpaceIfNotEmpty ();
230 commandLine.AppendFormat ("{0}{1}", switchName, parameter);
233 public void AppendSwitchUnquotedIfNotNull (string switchName,
236 if (switchName == null)
237 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
239 if (parameter == null)
242 AppendSpaceIfNotEmpty ();
243 commandLine.AppendFormat ("{0}{1}", switchName, parameter.ItemSpec);
246 public void AppendSwitchUnquotedIfNotNull (string switchName,
250 if (switchName == null)
251 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
253 if (delimiter == null)
254 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
256 if (parameters == null)
259 AppendSpaceIfNotEmpty ();
260 commandLine.AppendFormat ("{0}",switchName);
261 bool appendDelimiter = false;
262 for (int i = 0; i < parameters.Length; i++) {
263 if (parameters [i] == null)
265 if (appendDelimiter) {
266 commandLine.Append (delimiter);
267 commandLine.Append (parameters [i]);
269 commandLine.Append (parameters [i]);
270 appendDelimiter = true;
275 public void AppendSwitchUnquotedIfNotNull (string switchName,
276 ITaskItem[] parameters,
279 if (switchName == null)
280 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
282 if (delimiter == null)
283 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
285 if (parameters == null)
288 AppendSpaceIfNotEmpty ();
289 commandLine.AppendFormat ("{0}",switchName);
290 bool appendDelimiter = false;
291 for (int i = 0; i < parameters.Length; i++) {
292 if (parameters [i] == null)
294 if (appendDelimiter) {
295 commandLine.Append (delimiter);
296 commandLine.Append (parameters [i].ToString ());
298 commandLine.Append (parameters [i].ToString ());
299 appendDelimiter = true;
304 protected void AppendTextUnquoted (string textToAppend)
306 commandLine.Append (textToAppend);
309 protected void AppendTextWithQuoting (string textToAppend)
311 if (IsQuotingRequired (textToAppend))
312 commandLine.AppendFormat ("\"{0}\"",textToAppend);
314 commandLine.Append (textToAppend);
317 protected virtual bool IsQuotingRequired (string parameter)
320 // FIXME: change this to regex
321 foreach (char c in parameter) {
322 if (c == ' ' || c == '\t' || c == '\u000b' || c == '\u000c')
329 protected virtual void VerifyThrowNoEmbeddedDoubleQuotes (string switchName,
334 public override string ToString ()
336 return commandLine.ToString ();
339 protected StringBuilder CommandLine {