Centralize common command-line switches parsing
Rob Mensching committed
Jan 8, 2022 at 05:51 UTC
02ee982cf4ceabd64dbe966dc3771d272d53a085
11 files changed
+177
-163
src/api/wix/WixToolset.Extensibility/Data/ICommandLineCommand.cs
+6
-1
@@ -11,10 +11,15 @@ namespace WixToolset.Extensibility.Data
11
/// </summary>
12
public interface ICommandLineCommand
13
{
14
+ /// <summary>
15
+ /// Indicates the command-line should show help for the command.
16
+ /// </summary>
17
+ bool ShowHelp { get; set; }
18
+
19
/// <summary>
20
/// Indicates the command-line should show the command-line logo.
21
/// </summary>
17
- bool ShowLogo { get; }
22
+ bool ShowLogo { get; set; }
23
24
/// <summary>
25
/// Indicates the command-line parsing can stop.
src/wix/WixToolset.Converters/FixupCommandBase.cs
+3
-3
@@ -23,11 +23,11 @@ namespace WixToolset.Converters
23
this.SearchPatterns = new List<string>();
24
}
25
26
- public bool ShowLogo { get; private set; }
26
+ public bool ShowHelp { get; set; }
27
28
- public bool StopParsing { get; private set; }
28
+ public bool ShowLogo { get; set; }
29
30
- protected bool ShowHelp { get; set; }
30
+ public bool StopParsing { get; set; }
31
32
protected CustomTableTarget CustomTableSetting { get; set; }
33
src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs
+3
-3
@@ -30,11 +30,11 @@ namespace WixToolset.Core.ExtensionCache
30
31
private IMessaging Messaging { get; }
32
33
- public bool ShowLogo { get; private set; }
33
+ public bool ShowHelp { get; set; }
34
35
- public bool StopParsing { get; private set; }
35
+ public bool ShowLogo { get; set; }
36
37
- private bool ShowHelp { get; set; }
37
+ public bool StopParsing { get; set; }
38
39
private bool Global { get; set; }
40
src/wix/WixToolset.Core/CommandLine/BuildCommand.cs
+14
-68
@@ -26,8 +26,19 @@ namespace WixToolset.Core.CommandLine
26
this.commandLine = new CommandLine(this.ServiceProvider, this.Messaging);
27
}
28
29
- public bool ShowLogo => this.commandLine.ShowLogo;
29
+ public bool ShowHelp
30
+ {
31
+ get { return this.commandLine.ShowHelp; }
32
+ set { this.commandLine.ShowHelp = value; }
33
+ }
34
+
35
+ public bool ShowLogo
36
+ {
37
+ get { return this.commandLine.ShowLogo; }
38
+ set { this.commandLine.ShowLogo = value; }
39
+ }
40
41
+ // Stop parsing when we've decided to show help.
42
public bool StopParsing => this.commandLine.ShowHelp;
43
44
private IServiceProvider ServiceProvider { get; }
@@ -515,9 +526,9 @@ namespace WixToolset.Core.CommandLine
526
527
public PdbType PdbType { get; private set; }
528
518
- public bool ShowLogo { get; private set; }
529
+ public bool ShowLogo { get; set; }
530
520
- public bool ShowHelp { get; private set; }
531
+ public bool ShowHelp { get; set; }
532
533
public string IntermediateFolder { get; private set; }
534
@@ -554,12 +565,6 @@ namespace WixToolset.Core.CommandLine
565
var parameter = arg.Substring(1).ToLowerInvariant();
566
switch (parameter)
567
{
557
- case "?":
558
- case "h":
559
- case "help":
560
- this.ShowHelp = true;
561
- return true;
562
-
568
case "arch":
569
case "platform":
570
{
@@ -672,15 +677,6 @@ namespace WixToolset.Core.CommandLine
677
return true;
678
}
679
675
- case "nologo":
676
- this.ShowLogo = false;
677
- return true;
678
-
679
- case "v":
680
- case "verbose":
681
- this.Messaging.ShowVerboseMessages = true;
682
- return true;
683
-
680
case "sval":
681
this.SuppressValidation = true;
682
return true;
@@ -690,22 +686,6 @@ namespace WixToolset.Core.CommandLine
686
return true;
687
}
688
693
- if (parameter.StartsWith("sw"))
694
- {
695
- this.ParseSuppressWarning(parameter, "sw".Length, parser);
696
- return true;
697
- }
698
- else if (parameter.StartsWith("suppresswarning"))
699
- {
700
- this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser);
701
- return true;
702
- }
703
- else if (parameter.StartsWith("wx"))
704
- {
705
- this.ParseWarningAsError(parameter, "wx".Length, parser);
706
- return true;
707
- }
708
-
689
return false;
690
}
691
else
@@ -851,40 +831,6 @@ namespace WixToolset.Core.CommandLine
831
832
return true;
833
}
854
-
855
- private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser)
856
- {
857
- var paramArg = parameter.Substring(offset);
858
- if (paramArg.Length == 0)
859
- {
860
- this.Messaging.SuppressAllWarnings = true;
861
- }
862
- else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0)
863
- {
864
- this.Messaging.SuppressWarningMessage(suppressWarning);
865
- }
866
- else
867
- {
868
- parser.ReportErrorArgument(parameter, ErrorMessages.IllegalSuppressWarningId(paramArg));
869
- }
870
- }
871
-
872
- private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser)
873
- {
874
- var paramArg = parameter.Substring(offset);
875
- if (paramArg.Length == 0)
876
- {
877
- this.Messaging.WarningsAsError = true;
878
- }
879
- else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0)
880
- {
881
- this.Messaging.ElevateWarningMessage(elevateWarning);
882
- }
883
- else
884
- {
885
- parser.ReportErrorArgument(parameter, ErrorMessages.IllegalWarningIdAsError(paramArg));
886
- }
887
- }
834
}
835
}
836
}
src/wix/WixToolset.Core/CommandLine/CommandLine.cs
+85
-2
@@ -4,6 +4,7 @@ namespace WixToolset.Core.CommandLine
4
{
5
using System;
6
using System.Collections.Generic;
7
+ using WixToolset.Data;
8
using WixToolset.Extensibility;
9
using WixToolset.Extensibility.Data;
10
using WixToolset.Extensibility.Services;
@@ -21,10 +22,16 @@ namespace WixToolset.Core.CommandLine
22
23
internal class CommandLine : ICommandLine
24
{
24
- public CommandLine(IServiceProvider serviceProvider) => this.ServiceProvider = serviceProvider;
25
+ public CommandLine(IServiceProvider serviceProvider)
26
+ {
27
+ this.ServiceProvider = serviceProvider;
28
+ this.Messaging = serviceProvider.GetService<IMessaging>();
29
+ }
30
31
private IServiceProvider ServiceProvider { get; }
32
33
+ private IMessaging Messaging { get; }
34
+
35
public ICommandLineCommand CreateCommand(string[] args)
36
{
37
var arguments = this.ServiceProvider.GetService<ICommandLineArguments>();
@@ -105,7 +112,8 @@ namespace WixToolset.Core.CommandLine
112
}
113
else if (parser.IsSwitch(arg))
114
{
108
- if (!command.TryParseArgument(parser, arg) && !TryParseCommandLineArgumentWithExtension(arg, parser, extensions))
115
+ if (!command.TryParseArgument(parser, arg) && !TryParseCommandLineArgumentWithExtension(arg, parser, extensions) &&
116
+ !this.TryParseStandardCommandLineSwitch(command, parser, arg))
117
{
118
parser.ReportErrorArgument(arg);
119
}
@@ -195,5 +203,80 @@ namespace WixToolset.Core.CommandLine
203
204
return false;
205
}
206
+
207
+ private bool TryParseStandardCommandLineSwitch(ICommandLineCommand command, ICommandLineParser parser, string arg)
208
+ {
209
+ var parameter = arg.Substring(1).ToLowerInvariant();
210
+
211
+ switch (parameter)
212
+ {
213
+ case "?":
214
+ case "h":
215
+ case "help":
216
+ command.ShowHelp = true;
217
+ return true;
218
+
219
+ case "nologo":
220
+ command.ShowLogo = false;
221
+ return true;
222
+
223
+ case "v":
224
+ case "verbose":
225
+ this.Messaging.ShowVerboseMessages = true;
226
+ return true;
227
+ }
228
+
229
+ if (parameter.StartsWith("sw"))
230
+ {
231
+ this.ParseSuppressWarning(parameter, "sw".Length, parser);
232
+ return true;
233
+ }
234
+ else if (parameter.StartsWith("suppresswarning"))
235
+ {
236
+ this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser);
237
+ return true;
238
+ }
239
+ else if (parameter.StartsWith("wx"))
240
+ {
241
+ this.ParseWarningAsError(parameter, "wx".Length, parser);
242
+ return true;
243
+ }
244
+
245
+ return false;
246
+ }
247
+
248
+ private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser)
249
+ {
250
+ var paramArg = parameter.Substring(offset);
251
+ if (paramArg.Length == 0)
252
+ {
253
+ this.Messaging.SuppressAllWarnings = true;
254
+ }
255
+ else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0)
256
+ {
257
+ this.Messaging.SuppressWarningMessage(suppressWarning);
258
+ }
259
+ else
260
+ {
261
+ parser.ReportErrorArgument(parameter, ErrorMessages.IllegalSuppressWarningId(paramArg));
262
+ }
263
+ }
264
+
265
+ private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser)
266
+ {
267
+ var paramArg = parameter.Substring(offset);
268
+ if (paramArg.Length == 0)
269
+ {
270
+ this.Messaging.WarningsAsError = true;
271
+ }
272
+ else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0)
273
+ {
274
+ this.Messaging.ElevateWarningMessage(elevateWarning);
275
+ }
276
+ else
277
+ {
278
+ parser.ReportErrorArgument(parameter, ErrorMessages.IllegalWarningIdAsError(paramArg));
279
+ }
280
+ }
281
}
282
}
src/wix/WixToolset.Core/CommandLine/CompileCommand.cs
+12
-7
@@ -30,11 +30,17 @@ namespace WixToolset.Core.CommandLine
30
this.Platform = platform;
31
}
32
33
+ public bool ShowHelp { get; set; }
34
+
35
+ public bool ShowLogo { get; set; }
36
+
37
+ public bool StopParsing { get; }
38
+
39
private IServiceProvider ServiceProvider { get; }
40
35
- public IMessaging Messaging { get; }
41
+ private IMessaging Messaging { get; }
42
37
- public IExtensionManager ExtensionManager { get; }
43
+ private IExtensionManager ExtensionManager { get; }
44
45
private IEnumerable<SourceFile> SourceFiles { get; }
46
@@ -44,11 +50,10 @@ namespace WixToolset.Core.CommandLine
50
51
public IReadOnlyCollection<string> IncludeSearchPaths { get; }
52
47
- public bool ShowLogo => throw new NotImplementedException();
48
-
49
- public bool StopParsing => throw new NotImplementedException();
50
-
51
- public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => throw new NotImplementedException();
53
+ public bool TryParseArgument(ICommandLineParser parseHelper, string argument)
54
+ {
55
+ throw new NotImplementedException();
56
+ }
57
58
public Task<int> ExecuteAsync(CancellationToken _)
59
{
src/wix/WixToolset.Core/CommandLine/DecompileCommand.cs
+14
-68
@@ -23,8 +23,19 @@ namespace WixToolset.Core.CommandLine
23
this.commandLine = new CommandLine(this.Messaging);
24
}
25
26
- public bool ShowLogo => this.commandLine.ShowLogo;
26
+ public bool ShowHelp
27
+ {
28
+ get { return this.commandLine.ShowHelp; }
29
+ set { this.commandLine.ShowHelp = value; }
30
+ }
31
+
32
+ public bool ShowLogo
33
+ {
34
+ get { return this.commandLine.ShowLogo; }
35
+ set { this.commandLine.ShowLogo = value; }
36
+ }
37
38
+ // Stop parsing when we've decided to show help.
39
public bool StopParsing => this.commandLine.ShowHelp;
40
41
private IServiceProvider ServiceProvider { get; }
@@ -90,9 +101,9 @@ namespace WixToolset.Core.CommandLine
101
102
public Platform Platform { get; private set; }
103
93
- public bool ShowLogo { get; private set; }
104
+ public bool ShowLogo { get; set; }
105
95
- public bool ShowHelp { get; private set; }
106
+ public bool ShowHelp { get; set; }
107
108
public string IntermediateFolder { get; private set; }
109
@@ -105,12 +116,6 @@ namespace WixToolset.Core.CommandLine
116
var parameter = arg.Substring(1);
117
switch (parameter.ToLowerInvariant())
118
{
108
- case "?":
109
- case "h":
110
- case "help":
111
- this.ShowHelp = true;
112
- return true;
113
-
119
case "intermediatefolder":
120
this.IntermediateFolder = parser.GetNextArgumentAsDirectoryOrError(arg);
121
return true;
@@ -119,31 +124,6 @@ namespace WixToolset.Core.CommandLine
124
case "out":
125
this.OutputFile = parser.GetNextArgumentAsFilePathOrError(arg);
126
return true;
122
-
123
- case "nologo":
124
- this.ShowLogo = false;
125
- return true;
126
-
127
- case "v":
128
- case "verbose":
129
- this.Messaging.ShowVerboseMessages = true;
130
- return true;
131
- }
132
-
133
- if (parameter.StartsWith("sw"))
134
- {
135
- this.ParseSuppressWarning(parameter, "sw".Length, parser);
136
- return true;
137
- }
138
- else if (parameter.StartsWith("suppresswarning"))
139
- {
140
- this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser);
141
- return true;
142
- }
143
- else if (parameter.StartsWith("wx"))
144
- {
145
- this.ParseWarningAsError(parameter, "wx".Length, parser);
146
- return true;
127
}
128
}
129
else
@@ -217,40 +197,6 @@ namespace WixToolset.Core.CommandLine
197
{
198
return String.IsNullOrEmpty(this.OutputFile) ? Path.ChangeExtension(this.DecompileFilePath, ".wxs") : this.OutputFile;
199
}
220
-
221
- private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser)
222
- {
223
- var paramArg = parameter.Substring(offset);
224
- if (paramArg.Length == 0)
225
- {
226
- this.Messaging.SuppressAllWarnings = true;
227
- }
228
- else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0)
229
- {
230
- this.Messaging.SuppressWarningMessage(suppressWarning);
231
- }
232
- else
233
- {
234
- parser.ReportErrorArgument(parameter, ErrorMessages.IllegalSuppressWarningId(paramArg));
235
- }
236
- }
237
-
238
- private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser)
239
- {
240
- var paramArg = parameter.Substring(offset);
241
- if (paramArg.Length == 0)
242
- {
243
- this.Messaging.WarningsAsError = true;
244
- }
245
- else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0)
246
- {
247
- this.Messaging.ElevateWarningMessage(elevateWarning);
248
- }
249
- else
250
- {
251
- parser.ReportErrorArgument(parameter, ErrorMessages.IllegalWarningIdAsError(paramArg));
252
- }
253
- }
200
}
201
}
202
}
src/wix/WixToolset.Core/CommandLine/HelpCommand.cs
+15
-2
@@ -25,7 +25,17 @@ namespace WixToolset.Core.CommandLine
25
this.Branding = branding;
26
}
27
28
- public bool ShowLogo => true;
28
+ public bool ShowHelp
29
+ {
30
+ get => true;
31
+ set { }
32
+ }
33
+
34
+ public bool ShowLogo
35
+ {
36
+ get => true;
37
+ set { }
38
+ }
39
40
public bool StopParsing => true;
41
@@ -61,6 +71,9 @@ namespace WixToolset.Core.CommandLine
71
return Task.FromResult(-1);
72
}
73
64
- public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments
74
+ public bool TryParseArgument(ICommandLineParser parseHelper, string argument)
75
+ {
76
+ return true; // eat any arguments
77
+ }
78
}
79
}
src/wix/WixToolset.Core/CommandLine/VersionCommand.cs
+7
-2
@@ -10,7 +10,9 @@ namespace WixToolset.Core.CommandLine
10
11
internal class VersionCommand : ICommandLineCommand
12
{
13
- public bool ShowLogo => true;
13
+ public bool ShowHelp { get; set; }
14
+
15
+ public bool ShowLogo { get; set; }
16
17
public bool StopParsing => true;
18
@@ -25,6 +27,9 @@ namespace WixToolset.Core.CommandLine
27
return Task.FromResult(0);
28
}
29
28
- public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments
30
+ public bool TryParseArgument(ICommandLineParser parseHelper, string argument)
31
+ {
32
+ return true; // eat any arguments
33
+ }
34
}
35
}
src/wix/heat/HeatCommand.cs
+6
-4
@@ -28,6 +28,12 @@ namespace WixToolset.Harvesters
28
this.ExtensionOptions.Add(harvestType);
29
}
30
31
+ public bool ShowHelp { get; set; }
32
+
33
+ public bool ShowLogo { get; set; }
34
+
35
+ public bool StopParsing { get; private set; }
36
+
37
private string ExtensionArgument { get; set; }
38
39
private List<string> ExtensionOptions { get; } = new List<string>();
@@ -44,10 +50,6 @@ namespace WixToolset.Harvesters
50
51
private IServiceProvider ServiceProvider { get; }
52
47
- public bool ShowLogo { get; private set; }
48
-
49
- public bool StopParsing { get; private set; }
50
-
53
public Task<int> ExecuteAsync(CancellationToken cancellationToken)
54
{
55
var exitCode = this.Harvest();
src/wix/heat/HelpCommand.cs
+12
-3
@@ -24,7 +24,13 @@ namespace WixToolset.Harvesters
24
25
private IList<IHeatExtension> Extensions { get; }
26
27
- public bool ShowLogo => false;
27
+ public bool ShowHelp { get; set; }
28
+
29
+ public bool ShowLogo
30
+ {
31
+ get => false;
32
+ set { }
33
+ }
34
35
public bool StopParsing => true;
36
@@ -39,12 +45,15 @@ namespace WixToolset.Harvesters
45
var wixcopAssembly = typeof(HelpCommand).Assembly;
46
var fv = FileVersionInfo.GetVersionInfo(wixcopAssembly.Location);
47
42
- Console.WriteLine("WiX Toolset Harvester version {0}", fv.FileVersion);
48
+ Console.WriteLine("WiX Toolset Harvester version {0}", fv.ProductVersion);
49
Console.WriteLine("Copyright (C) .NET Foundation and contributors. All rights reserved.");
50
Console.WriteLine();
51
}
52
47
- public bool TryParseArgument(ICommandLineParser parser, string argument) => true;
53
+ public bool TryParseArgument(ICommandLineParser parser, string argument)
54
+ {
55
+ return true;
56
+ }
57
58
private int DisplayHelp()
59
{