-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CommandLineBuilder.cs
738 lines (653 loc) · 30.8 KB
/
CommandLineBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using System.Text.RegularExpressions;
using Microsoft.Build.Shared;
namespace Microsoft.Build.Utilities
{
/// <summary>
/// (1) Make sure values containing hyphens are quoted (RC at least requires this)
/// (2) Escape any embedded quotes.
/// -- Literal double quotes should be written in the form \" not ""
/// -- Backslashes falling just before doublequotes must be doubled.
/// -- Literal double quotes can only occur in pairs (you cannot pass a single literal double quote)
/// -- Functional double quotes (for example to handle spaces) are best put around both name and value
/// in switches like /Dname=value.
/// </summary>
/// <remarks>
///
/// Below are some quoting experiments, using the /D switch with the CL and RC preprocessor.
/// The /D switch is a little more tricky than most switches, because it has a name=value pair.
/// The table below contains what the preprocessor actually embeds when passed the switch in the
/// first column:
///
/// CL via cmd line CL via response file RC
/// /DFOO="A" A A
/// /D"FOO="A"" A A A
/// /DFOO=A A A
/// /D"FOO=A" A A
/// /DFOO=""A"" A A A
///
/// /DFOO=\"A\" "A" "A"
/// /DFOO="""A""" "A" broken "A"
/// /D"FOO=\"A\"" "A" "A"
/// /D"FOO=""A""" "A" "A"
///
/// /DFOO="A B" A B A B
/// /D"FOO=A B" A B A B
///
/// /D"FOO="A B"" broken
/// /DFOO=\"A B\" broken
/// /D"FOO=\"A B\"" "A B" "A B" "A B"
/// /D"FOO=""A B""" "A B" broken broken
///
/// From my experiments (with CL and RC only) it seems that
/// -- Literal double quotes are most reliably written in the form \" not ""
/// -- Backslashes falling just before doublequotes must be doubled.
/// -- Values containing literal double quotes must be quoted.
/// -- Literal double quotes can only occur in pairs (you cannot pass a single literal double quote)
/// -- For /Dname=value style switches, functional double quotes (for example to handle spaces) are best put around both
/// name and value (in other words, these kinds of switches don't need special treatment for their '=' signs).
/// -- Values containing hyphens should be quoted; RC requires this, and CL does not mind.
/// </remarks>
public class CommandLineBuilder
{
#region Constructors
/// <summary>
/// Default constructor
/// </summary>
public CommandLineBuilder()
{
}
/// <summary>
/// Default constructor
/// </summary>
public CommandLineBuilder(bool quoteHyphensOnCommandLine)
{
_quoteHyphens = quoteHyphensOnCommandLine;
}
/// <summary>
/// Default constructor
/// </summary>
public CommandLineBuilder(bool quoteHyphensOnCommandLine, bool useNewLineSeparator)
: this(quoteHyphensOnCommandLine)
{
_useNewLineSeparator = useNewLineSeparator;
}
#endregion
#region Properties
/// <summary>
/// Returns the length of the current command
/// </summary>
public int Length => CommandLine.Length;
/// <summary>
/// Retrieves the private StringBuilder instance for inheriting classes
/// </summary>
protected StringBuilder CommandLine { get; } = new StringBuilder();
#endregion
#region Basic methods
/// <summary>
/// Return the command-line as a string.
/// </summary>
/// <returns></returns>
public override string ToString() => CommandLine.ToString();
// Use if escaping of hyphens is supposed to take place
private static readonly string s_allowedUnquotedRegexNoHyphen =
"^" // Beginning of line
+ @"[a-z\\/:0-9\._+=]*"
+ "$";
private static readonly string s_definitelyNeedQuotesRegexWithHyphen = @"[|><\s,;\-""]+";
// Use if escaping of hyphens is not to take place
private static readonly string s_allowedUnquotedRegexWithHyphen =
"^" // Beginning of line
+ @"[a-z\\/:0-9\._\-+=]*" // Allow hyphen to be unquoted
+ "$";
private static readonly string s_definitelyNeedQuotesRegexNoHyphen = @"[|><\s,;""]+";
/// <summary>
/// Should hyphens be quoted or not
/// </summary>
private readonly bool _quoteHyphens;
/// <summary>
/// Should use new line separators instead of spaces to separate arguments.
/// </summary>
private readonly bool _useNewLineSeparator;
/// <summary>
/// Instead of defining which characters must be quoted, define
/// which characters we know its safe to not quote. This way leads
/// to more false-positives (which still work, but don't look as
/// nice coming out of the logger), but is less likely to leave a
/// security hole.
/// </summary>
private Regex _allowedUnquoted;
/// <summary>
/// Also, define the characters that we know for certain need quotes.
/// This is partly to document which characters we know can cause trouble
/// and partly as a sanity check against a bug creeping in.
/// </summary>
private Regex _definitelyNeedQuotes;
/// <summary>
/// Use a private property so that we can lazy initialize the regex
/// </summary>
private Regex DefinitelyNeedQuotes => _definitelyNeedQuotes
?? (_definitelyNeedQuotes = new Regex(_quoteHyphens ? s_definitelyNeedQuotesRegexWithHyphen : s_definitelyNeedQuotesRegexNoHyphen, RegexOptions.None));
/// <summary>
/// Use a private getter property to we can lazy initialize the regex
/// </summary>
private Regex AllowedUnquoted => _allowedUnquoted
?? (_allowedUnquoted = new Regex(_quoteHyphens ? s_allowedUnquotedRegexNoHyphen : s_allowedUnquotedRegexWithHyphen, RegexOptions.IgnoreCase));
/// <summary>
/// Checks the given switch parameter to see if it must/can be quoted.
/// </summary>
/// <param name="parameter">the string to examine for characters that require quoting</param>
/// <returns>true, if parameter should be quoted</returns>
protected virtual bool IsQuotingRequired(string parameter)
{
bool isQuotingRequired = false;
if (parameter != null)
{
#region Security Note: About cross-parameter injection
/*
If string parameters have whitespace in them, then a possible attack would
be like the following:
<Win32Icon>MyFile.ico /out:c:\windows\system32\notepad.exe</Win32Icon>
<Csc
Win32Icon="$(Win32Icon)"
...
/>
Since we just build up a command-line to pass into CSC.EXE, without quoting,
the project might overwrite notepad.exe.
If there are spaces in the parameter, then we must quote that parameter.
*/
#endregion
bool hasAllUnquotedCharacters = AllowedUnquoted.IsMatch(parameter);
bool hasSomeQuotedCharacters = DefinitelyNeedQuotes.IsMatch(parameter);
isQuotingRequired = !hasAllUnquotedCharacters;
isQuotingRequired = isQuotingRequired || hasSomeQuotedCharacters;
Debug.Assert(!hasAllUnquotedCharacters || !hasSomeQuotedCharacters,
"At least one of allowedUnquoted or definitelyNeedQuotes is wrong.");
}
return isQuotingRequired;
}
/// <summary>
/// Add a space or newline to the specified string if and only if it's not empty.
/// </summary>
/// <remarks>
/// This is a pretty obscure method and so it's only available to inherited classes.
/// </remarks>
protected void AppendSpaceIfNotEmpty()
{
if (CommandLine.Length != 0)
{
if (_useNewLineSeparator)
{
CommandLine.Append(Environment.NewLine);
}
else if(CommandLine[CommandLine.Length - 1] != ' ')
{
CommandLine.Append(" ");
}
}
}
#endregion
#region Methods for use in inherited classes, do not prepend a space before doing their thing
/// <summary>
/// Appends a string. Quotes are added if they are needed.
/// This method does not append a space to the command line before executing.
/// </summary>
/// <remarks>
/// Escapes any double quotes in the string.
/// </remarks>
/// <param name="textToAppend">The string to append</param>
protected void AppendTextWithQuoting(string textToAppend) => AppendQuotedTextToBuffer(CommandLine, textToAppend);
/// <summary>
/// Appends given text to the buffer after first quoting the text if necessary.
/// </summary>
/// <param name="buffer"></param>
/// <param name="unquotedTextToAppend"></param>
protected void AppendQuotedTextToBuffer(StringBuilder buffer, string unquotedTextToAppend)
{
ErrorUtilities.VerifyThrowArgumentNull(buffer, nameof(buffer));
if (unquotedTextToAppend != null)
{
bool addQuotes = IsQuotingRequired(unquotedTextToAppend);
if (addQuotes)
{
buffer.Append('"');
}
// Count the number of quotes
int literalQuotes = 0;
for (int i = 0; i < unquotedTextToAppend.Length; i++)
{
if (unquotedTextToAppend[i] == '"')
{
literalQuotes++;
}
}
if (literalQuotes > 0)
{
// Replace any \" sequences with \\"
unquotedTextToAppend = unquotedTextToAppend.Replace("\\\"", "\\\\\"");
// Now replace any " with \"
unquotedTextToAppend = unquotedTextToAppend.Replace("\"", "\\\"");
}
buffer.Append(unquotedTextToAppend);
// Be careful any trailing slash doesn't escape the quote we're about to add
if (addQuotes && unquotedTextToAppend.EndsWith("\\", StringComparison.Ordinal))
{
buffer.Append('\\');
}
if (addQuotes)
{
buffer.Append('"');
}
}
}
/// <summary>
/// Appends a string. No quotes are added.
/// This method does not append a space to the command line before executing.
/// </summary>
/// <example>
/// AppendTextUnquoted(@"Folder name\filename.cs") => "Folder name\\filename.cs"
/// </example>
/// <remarks>
/// In the future, this function may fixup 'textToAppend' to handle
/// literal embedded quotes.
/// </remarks>
/// <param name="textToAppend">The string to append</param>
public void AppendTextUnquoted(string textToAppend)
{
if (textToAppend != null)
{
CommandLine.Append(textToAppend);
}
}
/// <summary>
/// Appends a file name. Quotes are added if they are needed.
/// If the first character of the file name is a dash, ".\" is prepended to avoid confusing the file name with a switch
/// This method does not append a space to the command line before executing.
/// </summary>
/// <example>
/// AppendFileNameWithQuoting("-StrangeFileName.cs") => ".\-StrangeFileName.cs"
/// </example>
/// <remarks>
/// In the future, this function may fixup 'text' to handle
/// literal embedded quotes.
/// </remarks>
/// <param name="fileName">The file name to append</param>
protected void AppendFileNameWithQuoting(string fileName)
{
if (fileName != null)
{
// Don't let injection attackers escape from our quotes by sticking in
// their own quotes. Quotes are illegal.
VerifyThrowNoEmbeddedDoubleQuotes(string.Empty, fileName);
fileName = FileUtilities.FixFilePath(fileName);
if (fileName.Length != 0 && fileName[0] == '-')
{
AppendTextWithQuoting("." + Path.DirectorySeparatorChar + fileName);
}
else
{
AppendTextWithQuoting(fileName);
}
}
}
#endregion
#region Appending file names
/// <summary>
/// Appends a file name quoting it if necessary.
/// This method appends a space to the command line (if it's not currently empty) before the file name.
/// </summary>
/// <example>
/// AppendFileNameIfNotNull("-StrangeFileName.cs") => ".\-StrangeFileName.cs"
/// </example>
/// <param name="fileName">File name to append, if it's null this method has no effect</param>
public void AppendFileNameIfNotNull(string fileName)
{
if (fileName != null)
{
// Don't let injection attackers escape from our quotes by sticking in
// their own quotes. Quotes are illegal.
VerifyThrowNoEmbeddedDoubleQuotes(string.Empty, fileName);
AppendSpaceIfNotEmpty();
AppendFileNameWithQuoting(fileName);
}
}
/// <summary>
/// Appends a file name quoting it if necessary.
/// This method appends a space to the command line (if it's not currently empty) before the file name.
/// </summary>
/// <example>
/// See the string overload version
/// </example>
/// <param name="fileItem">File name to append, if it's null this method has no effect</param>
public void AppendFileNameIfNotNull(ITaskItem fileItem)
{
if (fileItem != null)
{
// Don't let injection attackers escape from our quotes by sticking in
// their own quotes. Quotes are illegal.
VerifyThrowNoEmbeddedDoubleQuotes(string.Empty, fileItem.ItemSpec);
AppendFileNameIfNotNull(fileItem.ItemSpec);
}
}
/// <summary>
/// Appends array of file name strings, quoting them if necessary, delimited by a delimiter.
/// This method appends a space to the command line (if it's not currently empty) before the file names.
/// </summary>
/// <example>
/// AppendFileNamesIfNotNull(new string[] {"Alpha.cs", "Beta.cs"}, ",") => "Alpha.cs,Beta.cs"
/// </example>
/// <param name="fileNames">File names to append, if it's null this method has no effect</param>
/// <param name="delimiter">The delimiter between file names</param>
public void AppendFileNamesIfNotNull(string[] fileNames, string delimiter)
{
ErrorUtilities.VerifyThrowArgumentNull(delimiter, nameof(delimiter));
if (fileNames?.Length > 0)
{
// Don't let injection attackers escape from our quotes by sticking in
// their own quotes. Quotes are illegal.
for (int i = 0; i < fileNames.Length; ++i)
{
VerifyThrowNoEmbeddedDoubleQuotes(string.Empty, fileNames[i]);
}
AppendSpaceIfNotEmpty();
for (int i = 0; i < fileNames.Length; ++i)
{
if (i != 0)
{
AppendTextUnquoted(delimiter);
}
AppendFileNameWithQuoting(fileNames[i]);
}
}
}
/// <summary>
/// Appends array of ITaskItem specs as file names, quoting them if necessary, delimited by a delimiter.
/// This method appends a space to the command line (if it's not currently empty) before the file names.
/// </summary>
/// <example>
/// See the string[] overload version
/// </example>
/// <param name="fileItems">Task items to append, if null this method has no effect</param>
/// <param name="delimiter">Delimiter to put between items in the command line</param>
public void AppendFileNamesIfNotNull(ITaskItem[] fileItems, string delimiter)
{
ErrorUtilities.VerifyThrowArgumentNull(delimiter, nameof(delimiter));
if (fileItems?.Length > 0)
{
// Don't let injection attackers escape from our quotes by sticking in
// their own quotes. Quotes are illegal.
for (int i = 0; i < fileItems.Length; ++i)
{
if (fileItems[i] != null)
{
VerifyThrowNoEmbeddedDoubleQuotes(string.Empty, fileItems[i].ItemSpec);
}
}
AppendSpaceIfNotEmpty();
for (int i = 0; i < fileItems.Length; ++i)
{
if (i != 0)
{
AppendTextUnquoted(delimiter);
}
if (fileItems[i] != null)
{
AppendFileNameWithQuoting(fileItems[i].ItemSpec);
}
}
}
}
#endregion
#region Appending switches with quoted parameters
/// <summary>
/// Appends a command-line switch that has no separate value, without any quoting.
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// AppendSwitch("/utf8output") => "/utf8output"
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
public void AppendSwitch(string switchName)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
AppendSpaceIfNotEmpty();
AppendTextUnquoted(switchName);
}
/// <summary>
/// Appends a command-line switch that takes a single string parameter, quoting the parameter if necessary.
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// AppendSwitchIfNotNull("/source:", "File Name.cs") => "/source:\"File Name.cs\""
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameter">Switch parameter to append, quoted if necessary. If null, this method has no effect.</param>
public void AppendSwitchIfNotNull(string switchName, string parameter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
if (parameter != null)
{
// Now, stick the parameter in.
AppendSwitch(switchName);
AppendTextWithQuoting(parameter);
}
}
/// <summary>
/// Throws if the parameter has a double-quote in it. This is used to prevent parameter
/// injection. It's virtual so that tools can override this method if they want to have quotes escaped in filenames
/// </summary>
/// <param name="switchName">Switch name for error message</param>
/// <param name="parameter">Switch parameter to scan</param>
protected virtual void VerifyThrowNoEmbeddedDoubleQuotes(string switchName, string parameter)
{
if (parameter != null)
{
if (string.IsNullOrEmpty(switchName))
{
ErrorUtilities.VerifyThrowArgument
(
-1 == parameter.IndexOf('"'),
"General.QuotesNotAllowedInThisKindOfTaskParameterNoSwitchName",
parameter
);
}
else
{
ErrorUtilities.VerifyThrowArgument
(
-1 == parameter.IndexOf('"'),
"General.QuotesNotAllowedInThisKindOfTaskParameter",
switchName,
parameter
);
}
}
}
/// <summary>
/// Append a switch [overload]
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// See the string overload version
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameter">Switch parameter to append, quoted if necessary. If null, this method has no effect.</param>
public void AppendSwitchIfNotNull(string switchName, ITaskItem parameter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
if (parameter != null)
{
AppendSwitchIfNotNull(switchName, parameter.ItemSpec);
}
}
/// <summary>
/// Appends a command-line switch that takes a string[] parameter,
/// and add double-quotes around the individual filenames if necessary.
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// AppendSwitchIfNotNull("/sources:", new string[] {"Alpha.cs", "Be ta.cs"}, ";") => "/sources:Alpha.cs;\"Be ta.cs\""
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameters">Switch parameters to append, quoted if necessary. If null, this method has no effect.</param>
/// <param name="delimiter">Delimiter to put between individual parameters, may not be null (may be empty)</param>
public void AppendSwitchIfNotNull(string switchName, string[] parameters, string delimiter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
ErrorUtilities.VerifyThrowArgumentNull(delimiter, nameof(delimiter));
if (parameters?.Length > 0)
{
AppendSwitch(switchName);
bool first = true;
foreach (string parameter in parameters)
{
if (!first)
{
AppendTextUnquoted(delimiter);
}
first = false;
AppendTextWithQuoting(parameter);
}
}
}
/// <summary>
/// Appends a command-line switch that takes a ITaskItem[] parameter,
/// and add double-quotes around the individual filenames if necessary.
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// See the string[] overload version
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameters">Switch parameters to append, quoted if necessary. If null, this method has no effect.</param>
/// <param name="delimiter">Delimiter to put between individual parameters, may not be null (may be empty)</param>
public void AppendSwitchIfNotNull(string switchName, ITaskItem[] parameters, string delimiter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
ErrorUtilities.VerifyThrowArgumentNull(delimiter, nameof(delimiter));
if (parameters?.Length > 0)
{
AppendSwitch(switchName);
bool first = true;
foreach (ITaskItem parameter in parameters)
{
if (!first)
{
AppendTextUnquoted(delimiter);
}
first = false;
if (parameter != null)
{
AppendTextWithQuoting(parameter.ItemSpec);
}
}
}
}
#endregion
#region Append switches with unquoted parameters
/// <summary>
/// Appends the literal parameter without trying to quote.
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// AppendSwitchUnquotedIfNotNull("/source:", "File Name.cs") => "/source:File Name.cs"
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameter">Switch parameter to append, not quoted. If null, this method has no effect.</param>
public void AppendSwitchUnquotedIfNotNull(string switchName, string parameter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
if (parameter != null)
{
// Now, stick the parameter in.
AppendSwitch(switchName);
AppendTextUnquoted(parameter);
}
}
/// <summary>
/// Appends the literal parameter without trying to quote.
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// See the string overload version
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameter">Switch parameter to append, not quoted. If null, this method has no effect.</param>
public void AppendSwitchUnquotedIfNotNull(string switchName, ITaskItem parameter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
if (parameter != null)
{
AppendSwitchUnquotedIfNotNull(switchName, parameter.ItemSpec);
}
}
/// <summary>
/// Appends a command-line switch that takes a string[] parameter, not quoting the individual parameters
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// AppendSwitchUnquotedIfNotNull("/sources:", new string[] {"Alpha.cs", "Be ta.cs"}, ";") => "/sources:Alpha.cs;Be ta.cs"
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameters">Switch parameters to append, not quoted. If null, this method has no effect.</param>
/// <param name="delimiter">Delimiter to put between individual parameters, may not be null (may be empty)</param>
public void AppendSwitchUnquotedIfNotNull(string switchName, string[] parameters, string delimiter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
ErrorUtilities.VerifyThrowArgumentNull(delimiter, nameof(delimiter));
if (parameters?.Length > 0)
{
AppendSwitch(switchName);
bool first = true;
foreach (string parameter in parameters)
{
if (!first)
{
AppendTextUnquoted(delimiter);
}
first = false;
AppendTextUnquoted(parameter);
}
}
}
/// <summary>
/// Appends a command-line switch that takes a ITaskItem[] parameter, not quoting the individual parameters
/// This method appends a space to the command line (if it's not currently empty) before the switch.
/// </summary>
/// <example>
/// See the string[] overload version
/// </example>
/// <param name="switchName">The switch to append to the command line, may not be null</param>
/// <param name="parameters">Switch parameters to append, not quoted. If null, this method has no effect.</param>
/// <param name="delimiter">Delimiter to put between individual parameters, may not be null (may be empty)</param>
public void AppendSwitchUnquotedIfNotNull(string switchName, ITaskItem[] parameters, string delimiter)
{
ErrorUtilities.VerifyThrowArgumentNull(switchName, nameof(switchName));
ErrorUtilities.VerifyThrowArgumentNull(delimiter, nameof(delimiter));
if (parameters?.Length > 0)
{
AppendSwitch(switchName);
bool first = true;
foreach (ITaskItem parameter in parameters)
{
if (!first)
{
AppendTextUnquoted(delimiter);
}
first = false;
if (parameter != null)
{
AppendTextUnquoted(parameter.ItemSpec);
}
}
}
}
#endregion
}
}