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)
49 AppendSpaceIfNotEmpty ();
50 commandLine.Append (fileName);
53 public void AppendFileNameIfNotNull (ITaskItem fileItem)
57 AppendSpaceIfNotEmpty ();
58 commandLine.Append (fileItem.ToString());
61 public void AppendFileNamesIfNotNull (string[] fileNames,
64 if (fileNames == null)
66 bool appendDelimiter = false;
67 AppendSpaceIfNotEmpty ();
68 for (int i = 0; i < fileNames.Length; i++) {
69 if (fileNames [i] == null)
71 if (appendDelimiter) {
72 commandLine.Append (delimiter);
73 commandLine.Append (fileNames [i]);
75 commandLine.Append (fileNames [i]);
76 appendDelimiter = true;
81 public void AppendFileNamesIfNotNull (ITaskItem[] fileItems,
84 if (fileItems == null)
86 bool appendDelimiter = false;
87 AppendSpaceIfNotEmpty ();
88 for (int i = 0; i < fileItems.Length; i++) {
89 if (fileItems [i] == null)
91 if (appendDelimiter) {
92 commandLine.Append (delimiter);
93 commandLine.Append (fileItems [i].ToString ());
95 commandLine.Append (fileItems [i].ToString ());
96 appendDelimiter = true;
101 protected void AppendFileNamesWithQuoting (string fileName)
103 if (IsQuotingRequired (fileName))
104 commandLine.AppendFormat ("\"{0}\"",fileName);
106 commandLine.Append (fileName);
109 protected void AppendSpaceIfNotEmpty ()
111 if (commandLine.Length != 0)
112 commandLine.Append (' ');
115 public void AppendSwitch (string switchName)
117 if (switchName == null)
119 AppendSpaceIfNotEmpty ();
120 commandLine.Append (switchName);
123 public void AppendSwitchIfNotNull (string switchName,
126 if (switchName == null || parameter == null)
128 AppendSpaceIfNotEmpty ();
129 commandLine.AppendFormat ("{0}{1}",switchName,
133 public void AppendSwitchIfNotNull (string switchName,
136 if (switchName == null || parameter == null)
138 AppendSpaceIfNotEmpty ();
139 commandLine.AppendFormat ("{0}{1}",switchName,
140 parameter.ToString ());
143 public void AppendSwitchIfNotNull (string switchName,
147 if (switchName == null || parameters == null)
149 AppendSpaceIfNotEmpty ();
150 commandLine.AppendFormat ("{0}",switchName);
151 bool appendDelimiter = false;
152 for (int i = 0; i < parameters.Length; i++) {
153 if (parameters [i] == null)
155 if (appendDelimiter) {
156 commandLine.Append (delimiter);
157 commandLine.Append (parameters [i]);
159 commandLine.Append (parameters [i]);
160 appendDelimiter = true;
165 public void AppendSwitchIfNotNull (string switchName,
166 ITaskItem[] parameters,
169 if (switchName == null || parameters == null)
171 AppendSpaceIfNotEmpty ();
172 commandLine.AppendFormat ("{0}",switchName);
173 bool appendDelimiter = false;
174 for (int i = 0; i < parameters.Length; i++) {
175 if (parameters [i] == null)
177 if (appendDelimiter) {
178 commandLine.Append (delimiter);
179 commandLine.Append (parameters [i].ToString ());
181 commandLine.Append (parameters [i].ToString ());
182 appendDelimiter = true;
187 public void AppendSwitchUnquotedIfNotNull (string switchName,
190 if (switchName == null || parameter == null)
192 AppendSpaceIfNotEmpty ();
193 commandLine.AppendFormat ("{0}{1}", switchName, parameter);
196 public void AppendSwitchUnquotedIfNotNull (string switchName,
199 if (switchName == null || parameter == null)
201 AppendSpaceIfNotEmpty ();
202 commandLine.AppendFormat ("{0}{1}", switchName, parameter.GetMetadata ("Include"));
205 public void AppendSwitchUnquotedIfNotNull (string switchName,
209 if (switchName == null || delimiter == null || parameters == null)
211 AppendSpaceIfNotEmpty ();
212 commandLine.AppendFormat ("{0}",switchName);
213 bool appendDelimiter = false;
214 for (int i = 0; i < parameters.Length; i++) {
215 if (parameters [i] == null)
217 if (appendDelimiter) {
218 commandLine.Append (delimiter);
219 commandLine.Append (parameters [i]);
221 commandLine.Append (parameters [i]);
222 appendDelimiter = true;
227 public void AppendSwitchUnquotedIfNotNull (string switchName,
228 ITaskItem[] parameters,
231 if (switchName == null || delimiter == null || parameters == null)
233 AppendSpaceIfNotEmpty ();
234 commandLine.AppendFormat ("{0}",switchName);
235 bool appendDelimiter = false;
236 for (int i = 0; i < parameters.Length; i++) {
237 if (parameters [i] == null)
239 if (appendDelimiter) {
240 commandLine.Append (delimiter);
241 commandLine.Append (parameters [i].ToString ());
243 commandLine.Append (parameters [i].ToString ());
244 appendDelimiter = true;
249 protected void AppendTextUnquoted (string textToAppend)
251 commandLine.Append (textToAppend);
254 protected void AppendTextWithQuoting (string textToAppend)
256 if (IsQuotingRequired (textToAppend))
257 commandLine.AppendFormat ("\"{0}\"",textToAppend);
259 commandLine.Append (textToAppend);
262 protected virtual bool IsQuotingRequired (string parameter)
265 // FIXME: change this to regex
266 foreach (char c in parameter) {
267 if (c == ' ' || c == '\t' || c == '\u000b' || c == '\u000c')
273 public override string ToString ()
275 return commandLine.ToString ();
278 protected StringBuilder CommandLine {