From 02ee982cf4ceabd64dbe966dc3771d272d53a085 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 8 Jan 2022 05:51:26 -0800 Subject: Centralize common command-line switches parsing --- .../Data/ICommandLineCommand.cs | 7 +- src/wix/WixToolset.Converters/FixupCommandBase.cs | 6 +- .../ExtensionCacheManagerCommand.cs | 6 +- .../WixToolset.Core/CommandLine/BuildCommand.cs | 82 ++++---------------- src/wix/WixToolset.Core/CommandLine/CommandLine.cs | 87 +++++++++++++++++++++- .../WixToolset.Core/CommandLine/CompileCommand.cs | 19 +++-- .../CommandLine/DecompileCommand.cs | 82 ++++---------------- src/wix/WixToolset.Core/CommandLine/HelpCommand.cs | 17 ++++- .../WixToolset.Core/CommandLine/VersionCommand.cs | 9 ++- src/wix/heat/HeatCommand.cs | 10 ++- src/wix/heat/HelpCommand.cs | 15 +++- 11 files changed, 177 insertions(+), 163 deletions(-) (limited to 'src') diff --git a/src/api/wix/WixToolset.Extensibility/Data/ICommandLineCommand.cs b/src/api/wix/WixToolset.Extensibility/Data/ICommandLineCommand.cs index b03a18f4..b6c9ef3e 100644 --- a/src/api/wix/WixToolset.Extensibility/Data/ICommandLineCommand.cs +++ b/src/api/wix/WixToolset.Extensibility/Data/ICommandLineCommand.cs @@ -11,10 +11,15 @@ namespace WixToolset.Extensibility.Data /// public interface ICommandLineCommand { + /// + /// Indicates the command-line should show help for the command. + /// + bool ShowHelp { get; set; } + /// /// Indicates the command-line should show the command-line logo. /// - bool ShowLogo { get; } + bool ShowLogo { get; set; } /// /// Indicates the command-line parsing can stop. diff --git a/src/wix/WixToolset.Converters/FixupCommandBase.cs b/src/wix/WixToolset.Converters/FixupCommandBase.cs index 21282d07..71d4dad7 100644 --- a/src/wix/WixToolset.Converters/FixupCommandBase.cs +++ b/src/wix/WixToolset.Converters/FixupCommandBase.cs @@ -23,11 +23,11 @@ namespace WixToolset.Converters this.SearchPatterns = new List(); } - public bool ShowLogo { get; private set; } + public bool ShowHelp { get; set; } - public bool StopParsing { get; private set; } + public bool ShowLogo { get; set; } - protected bool ShowHelp { get; set; } + public bool StopParsing { get; set; } protected CustomTableTarget CustomTableSetting { get; set; } diff --git a/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs b/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs index 94ee4f22..d37ee341 100644 --- a/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs +++ b/src/wix/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs @@ -30,11 +30,11 @@ namespace WixToolset.Core.ExtensionCache private IMessaging Messaging { get; } - public bool ShowLogo { get; private set; } + public bool ShowHelp { get; set; } - public bool StopParsing { get; private set; } + public bool ShowLogo { get; set; } - private bool ShowHelp { get; set; } + public bool StopParsing { get; set; } private bool Global { get; set; } diff --git a/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs b/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs index 47b7afa8..3ea47279 100644 --- a/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs +++ b/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs @@ -26,8 +26,19 @@ namespace WixToolset.Core.CommandLine this.commandLine = new CommandLine(this.ServiceProvider, this.Messaging); } - public bool ShowLogo => this.commandLine.ShowLogo; + public bool ShowHelp + { + get { return this.commandLine.ShowHelp; } + set { this.commandLine.ShowHelp = value; } + } + + public bool ShowLogo + { + get { return this.commandLine.ShowLogo; } + set { this.commandLine.ShowLogo = value; } + } + // Stop parsing when we've decided to show help. public bool StopParsing => this.commandLine.ShowHelp; private IServiceProvider ServiceProvider { get; } @@ -515,9 +526,9 @@ namespace WixToolset.Core.CommandLine public PdbType PdbType { get; private set; } - public bool ShowLogo { get; private set; } + public bool ShowLogo { get; set; } - public bool ShowHelp { get; private set; } + public bool ShowHelp { get; set; } public string IntermediateFolder { get; private set; } @@ -554,12 +565,6 @@ namespace WixToolset.Core.CommandLine var parameter = arg.Substring(1).ToLowerInvariant(); switch (parameter) { - case "?": - case "h": - case "help": - this.ShowHelp = true; - return true; - case "arch": case "platform": { @@ -672,15 +677,6 @@ namespace WixToolset.Core.CommandLine return true; } - case "nologo": - this.ShowLogo = false; - return true; - - case "v": - case "verbose": - this.Messaging.ShowVerboseMessages = true; - return true; - case "sval": this.SuppressValidation = true; return true; @@ -690,22 +686,6 @@ namespace WixToolset.Core.CommandLine return true; } - if (parameter.StartsWith("sw")) - { - this.ParseSuppressWarning(parameter, "sw".Length, parser); - return true; - } - else if (parameter.StartsWith("suppresswarning")) - { - this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser); - return true; - } - else if (parameter.StartsWith("wx")) - { - this.ParseWarningAsError(parameter, "wx".Length, parser); - return true; - } - return false; } else @@ -851,40 +831,6 @@ namespace WixToolset.Core.CommandLine return true; } - - private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser) - { - var paramArg = parameter.Substring(offset); - if (paramArg.Length == 0) - { - this.Messaging.SuppressAllWarnings = true; - } - else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0) - { - this.Messaging.SuppressWarningMessage(suppressWarning); - } - else - { - parser.ReportErrorArgument(parameter, ErrorMessages.IllegalSuppressWarningId(paramArg)); - } - } - - private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser) - { - var paramArg = parameter.Substring(offset); - if (paramArg.Length == 0) - { - this.Messaging.WarningsAsError = true; - } - else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0) - { - this.Messaging.ElevateWarningMessage(elevateWarning); - } - else - { - parser.ReportErrorArgument(parameter, ErrorMessages.IllegalWarningIdAsError(paramArg)); - } - } } } } diff --git a/src/wix/WixToolset.Core/CommandLine/CommandLine.cs b/src/wix/WixToolset.Core/CommandLine/CommandLine.cs index 73a82dfc..8913828b 100644 --- a/src/wix/WixToolset.Core/CommandLine/CommandLine.cs +++ b/src/wix/WixToolset.Core/CommandLine/CommandLine.cs @@ -4,6 +4,7 @@ namespace WixToolset.Core.CommandLine { using System; using System.Collections.Generic; + using WixToolset.Data; using WixToolset.Extensibility; using WixToolset.Extensibility.Data; using WixToolset.Extensibility.Services; @@ -21,10 +22,16 @@ namespace WixToolset.Core.CommandLine internal class CommandLine : ICommandLine { - public CommandLine(IServiceProvider serviceProvider) => this.ServiceProvider = serviceProvider; + public CommandLine(IServiceProvider serviceProvider) + { + this.ServiceProvider = serviceProvider; + this.Messaging = serviceProvider.GetService(); + } private IServiceProvider ServiceProvider { get; } + private IMessaging Messaging { get; } + public ICommandLineCommand CreateCommand(string[] args) { var arguments = this.ServiceProvider.GetService(); @@ -105,7 +112,8 @@ namespace WixToolset.Core.CommandLine } else if (parser.IsSwitch(arg)) { - if (!command.TryParseArgument(parser, arg) && !TryParseCommandLineArgumentWithExtension(arg, parser, extensions)) + if (!command.TryParseArgument(parser, arg) && !TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && + !this.TryParseStandardCommandLineSwitch(command, parser, arg)) { parser.ReportErrorArgument(arg); } @@ -195,5 +203,80 @@ namespace WixToolset.Core.CommandLine return false; } + + private bool TryParseStandardCommandLineSwitch(ICommandLineCommand command, ICommandLineParser parser, string arg) + { + var parameter = arg.Substring(1).ToLowerInvariant(); + + switch (parameter) + { + case "?": + case "h": + case "help": + command.ShowHelp = true; + return true; + + case "nologo": + command.ShowLogo = false; + return true; + + case "v": + case "verbose": + this.Messaging.ShowVerboseMessages = true; + return true; + } + + if (parameter.StartsWith("sw")) + { + this.ParseSuppressWarning(parameter, "sw".Length, parser); + return true; + } + else if (parameter.StartsWith("suppresswarning")) + { + this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser); + return true; + } + else if (parameter.StartsWith("wx")) + { + this.ParseWarningAsError(parameter, "wx".Length, parser); + return true; + } + + return false; + } + + private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser) + { + var paramArg = parameter.Substring(offset); + if (paramArg.Length == 0) + { + this.Messaging.SuppressAllWarnings = true; + } + else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0) + { + this.Messaging.SuppressWarningMessage(suppressWarning); + } + else + { + parser.ReportErrorArgument(parameter, ErrorMessages.IllegalSuppressWarningId(paramArg)); + } + } + + private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser) + { + var paramArg = parameter.Substring(offset); + if (paramArg.Length == 0) + { + this.Messaging.WarningsAsError = true; + } + else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0) + { + this.Messaging.ElevateWarningMessage(elevateWarning); + } + else + { + parser.ReportErrorArgument(parameter, ErrorMessages.IllegalWarningIdAsError(paramArg)); + } + } } } diff --git a/src/wix/WixToolset.Core/CommandLine/CompileCommand.cs b/src/wix/WixToolset.Core/CommandLine/CompileCommand.cs index 6e31b241..73e5bcbe 100644 --- a/src/wix/WixToolset.Core/CommandLine/CompileCommand.cs +++ b/src/wix/WixToolset.Core/CommandLine/CompileCommand.cs @@ -30,11 +30,17 @@ namespace WixToolset.Core.CommandLine this.Platform = platform; } + public bool ShowHelp { get; set; } + + public bool ShowLogo { get; set; } + + public bool StopParsing { get; } + private IServiceProvider ServiceProvider { get; } - public IMessaging Messaging { get; } + private IMessaging Messaging { get; } - public IExtensionManager ExtensionManager { get; } + private IExtensionManager ExtensionManager { get; } private IEnumerable SourceFiles { get; } @@ -44,11 +50,10 @@ namespace WixToolset.Core.CommandLine public IReadOnlyCollection IncludeSearchPaths { get; } - public bool ShowLogo => throw new NotImplementedException(); - - public bool StopParsing => throw new NotImplementedException(); - - public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => throw new NotImplementedException(); + public bool TryParseArgument(ICommandLineParser parseHelper, string argument) + { + throw new NotImplementedException(); + } public Task ExecuteAsync(CancellationToken _) { diff --git a/src/wix/WixToolset.Core/CommandLine/DecompileCommand.cs b/src/wix/WixToolset.Core/CommandLine/DecompileCommand.cs index fc0ab0c9..22853f86 100644 --- a/src/wix/WixToolset.Core/CommandLine/DecompileCommand.cs +++ b/src/wix/WixToolset.Core/CommandLine/DecompileCommand.cs @@ -23,8 +23,19 @@ namespace WixToolset.Core.CommandLine this.commandLine = new CommandLine(this.Messaging); } - public bool ShowLogo => this.commandLine.ShowLogo; + public bool ShowHelp + { + get { return this.commandLine.ShowHelp; } + set { this.commandLine.ShowHelp = value; } + } + + public bool ShowLogo + { + get { return this.commandLine.ShowLogo; } + set { this.commandLine.ShowLogo = value; } + } + // Stop parsing when we've decided to show help. public bool StopParsing => this.commandLine.ShowHelp; private IServiceProvider ServiceProvider { get; } @@ -90,9 +101,9 @@ namespace WixToolset.Core.CommandLine public Platform Platform { get; private set; } - public bool ShowLogo { get; private set; } + public bool ShowLogo { get; set; } - public bool ShowHelp { get; private set; } + public bool ShowHelp { get; set; } public string IntermediateFolder { get; private set; } @@ -105,12 +116,6 @@ namespace WixToolset.Core.CommandLine var parameter = arg.Substring(1); switch (parameter.ToLowerInvariant()) { - case "?": - case "h": - case "help": - this.ShowHelp = true; - return true; - case "intermediatefolder": this.IntermediateFolder = parser.GetNextArgumentAsDirectoryOrError(arg); return true; @@ -119,31 +124,6 @@ namespace WixToolset.Core.CommandLine case "out": this.OutputFile = parser.GetNextArgumentAsFilePathOrError(arg); return true; - - case "nologo": - this.ShowLogo = false; - return true; - - case "v": - case "verbose": - this.Messaging.ShowVerboseMessages = true; - return true; - } - - if (parameter.StartsWith("sw")) - { - this.ParseSuppressWarning(parameter, "sw".Length, parser); - return true; - } - else if (parameter.StartsWith("suppresswarning")) - { - this.ParseSuppressWarning(parameter, "suppresswarning".Length, parser); - return true; - } - else if (parameter.StartsWith("wx")) - { - this.ParseWarningAsError(parameter, "wx".Length, parser); - return true; } } else @@ -217,40 +197,6 @@ namespace WixToolset.Core.CommandLine { return String.IsNullOrEmpty(this.OutputFile) ? Path.ChangeExtension(this.DecompileFilePath, ".wxs") : this.OutputFile; } - - private void ParseSuppressWarning(string parameter, int offset, ICommandLineParser parser) - { - var paramArg = parameter.Substring(offset); - if (paramArg.Length == 0) - { - this.Messaging.SuppressAllWarnings = true; - } - else if (Int32.TryParse(paramArg, out var suppressWarning) && suppressWarning > 0) - { - this.Messaging.SuppressWarningMessage(suppressWarning); - } - else - { - parser.ReportErrorArgument(parameter, ErrorMessages.IllegalSuppressWarningId(paramArg)); - } - } - - private void ParseWarningAsError(string parameter, int offset, ICommandLineParser parser) - { - var paramArg = parameter.Substring(offset); - if (paramArg.Length == 0) - { - this.Messaging.WarningsAsError = true; - } - else if (Int32.TryParse(paramArg, out var elevateWarning) && elevateWarning > 0) - { - this.Messaging.ElevateWarningMessage(elevateWarning); - } - else - { - parser.ReportErrorArgument(parameter, ErrorMessages.IllegalWarningIdAsError(paramArg)); - } - } } } } diff --git a/src/wix/WixToolset.Core/CommandLine/HelpCommand.cs b/src/wix/WixToolset.Core/CommandLine/HelpCommand.cs index 4d192d43..9a879cc8 100644 --- a/src/wix/WixToolset.Core/CommandLine/HelpCommand.cs +++ b/src/wix/WixToolset.Core/CommandLine/HelpCommand.cs @@ -25,7 +25,17 @@ namespace WixToolset.Core.CommandLine this.Branding = branding; } - public bool ShowLogo => true; + public bool ShowHelp + { + get => true; + set { } + } + + public bool ShowLogo + { + get => true; + set { } + } public bool StopParsing => true; @@ -61,6 +71,9 @@ namespace WixToolset.Core.CommandLine return Task.FromResult(-1); } - public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments + public bool TryParseArgument(ICommandLineParser parseHelper, string argument) + { + return true; // eat any arguments + } } } diff --git a/src/wix/WixToolset.Core/CommandLine/VersionCommand.cs b/src/wix/WixToolset.Core/CommandLine/VersionCommand.cs index b9dacd3a..7e08b66e 100644 --- a/src/wix/WixToolset.Core/CommandLine/VersionCommand.cs +++ b/src/wix/WixToolset.Core/CommandLine/VersionCommand.cs @@ -10,7 +10,9 @@ namespace WixToolset.Core.CommandLine internal class VersionCommand : ICommandLineCommand { - public bool ShowLogo => true; + public bool ShowHelp { get; set; } + + public bool ShowLogo { get; set; } public bool StopParsing => true; @@ -25,6 +27,9 @@ namespace WixToolset.Core.CommandLine return Task.FromResult(0); } - public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments + public bool TryParseArgument(ICommandLineParser parseHelper, string argument) + { + return true; // eat any arguments + } } } diff --git a/src/wix/heat/HeatCommand.cs b/src/wix/heat/HeatCommand.cs index 99f27521..56277004 100644 --- a/src/wix/heat/HeatCommand.cs +++ b/src/wix/heat/HeatCommand.cs @@ -28,6 +28,12 @@ namespace WixToolset.Harvesters this.ExtensionOptions.Add(harvestType); } + public bool ShowHelp { get; set; } + + public bool ShowLogo { get; set; } + + public bool StopParsing { get; private set; } + private string ExtensionArgument { get; set; } private List ExtensionOptions { get; } = new List(); @@ -44,10 +50,6 @@ namespace WixToolset.Harvesters private IServiceProvider ServiceProvider { get; } - public bool ShowLogo { get; private set; } - - public bool StopParsing { get; private set; } - public Task ExecuteAsync(CancellationToken cancellationToken) { var exitCode = this.Harvest(); diff --git a/src/wix/heat/HelpCommand.cs b/src/wix/heat/HelpCommand.cs index bd2bcc24..d991b4fa 100644 --- a/src/wix/heat/HelpCommand.cs +++ b/src/wix/heat/HelpCommand.cs @@ -24,7 +24,13 @@ namespace WixToolset.Harvesters private IList Extensions { get; } - public bool ShowLogo => false; + public bool ShowHelp { get; set; } + + public bool ShowLogo + { + get => false; + set { } + } public bool StopParsing => true; @@ -39,12 +45,15 @@ namespace WixToolset.Harvesters var wixcopAssembly = typeof(HelpCommand).Assembly; var fv = FileVersionInfo.GetVersionInfo(wixcopAssembly.Location); - Console.WriteLine("WiX Toolset Harvester version {0}", fv.FileVersion); + Console.WriteLine("WiX Toolset Harvester version {0}", fv.ProductVersion); Console.WriteLine("Copyright (C) .NET Foundation and contributors. All rights reserved."); Console.WriteLine(); } - public bool TryParseArgument(ICommandLineParser parser, string argument) => true; + public bool TryParseArgument(ICommandLineParser parser, string argument) + { + return true; + } private int DisplayHelp() { -- cgit v1.2.3-55-g6feb