From f4cefb9ac9a6911ee0a1ad035e6ee50b7f28e5c5 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 18 Jul 2020 14:55:58 -0700 Subject: Display command-line help from Core and extensions Closes wixtoolset/issues#6211 --- .../ExtensionCacheManagerCommand.cs | 21 ++++++++--- .../ExtensionCacheManagerExtensionCommandLine.cs | 6 ++- src/WixToolset.Core/CommandLine/CommandLine.cs | 4 +- .../CommandLine/CommandLineParser.cs | 36 +++++++----------- src/WixToolset.Core/CommandLine/HelpCommand.cs | 43 +++++++++++++++++++--- src/WixToolset.Core/CommandLine/VersionCommand.cs | 3 +- .../ExtensibilityServices/Messaging.cs | 22 +++-------- 7 files changed, 79 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs b/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs index 24f17d06..67c1504b 100644 --- a/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs +++ b/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerCommand.cs @@ -96,12 +96,15 @@ namespace WixToolset.Core.ExtensionCache switch (parameter.ToLowerInvariant()) { case "?": + case "h": + case "-help": this.ShowHelp = true; this.ShowLogo = true; this.StopParsing = true; return true; case "nologo": + case "-nologo": this.ShowLogo = false; return true; @@ -158,13 +161,21 @@ namespace WixToolset.Core.ExtensionCache private static void DisplayHelp() { - Console.WriteLine(" usage: wix.exe extension add|remove|list [extensionRef]"); Console.WriteLine(); - Console.WriteLine(" -g add/remove the extension for the current user"); - Console.WriteLine(" -nologo suppress displaying the logo information"); - Console.WriteLine(" -? this help information"); + Console.WriteLine("Usage: wix extension add|remove|list [extensionRef]"); Console.WriteLine(); - Console.WriteLine(" extensionRef format: extensionId/version (the version is optional)"); + Console.WriteLine("Options:"); + Console.WriteLine(" -h|--help Show command line help."); + Console.WriteLine(" -g|--global Add/remove the extension for the current user."); + Console.WriteLine(" --nologo Suppress displaying the logo information."); + Console.WriteLine(); + Console.WriteLine("Commands:"); + Console.WriteLine(); + Console.WriteLine(" add Add extension to the cache."); + Console.WriteLine(" list List extensions in the cache."); + Console.WriteLine(" remove Remove extension from the cache."); + Console.WriteLine(); + Console.WriteLine(" extensionRef format: extensionId/version (the version is optional)"); } } } diff --git a/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerExtensionCommandLine.cs b/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerExtensionCommandLine.cs index 81e96718..a27e2a1f 100644 --- a/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerExtensionCommandLine.cs +++ b/src/WixToolset.Core.ExtensionCache/ExtensionCacheManagerExtensionCommandLine.cs @@ -21,8 +21,10 @@ namespace WixToolset.Core.ExtensionCache private IWixToolsetServiceProvider ServiceProvider { get; } - // TODO: Do something with CommandLineSwitches - public override IEnumerable CommandLineSwitches => base.CommandLineSwitches; + public override IEnumerable CommandLineSwitches => new ExtensionCommandLineSwitch[] + { + new ExtensionCommandLineSwitch { Switch = "extension", Description = "Manage extension cache." }, + }; public override bool TryParseCommand(ICommandLineParser parser, string argument, out ICommandLineCommand command) { diff --git a/src/WixToolset.Core/CommandLine/CommandLine.cs b/src/WixToolset.Core/CommandLine/CommandLine.cs index 02bc32e9..5439eb39 100644 --- a/src/WixToolset.Core/CommandLine/CommandLine.cs +++ b/src/WixToolset.Core/CommandLine/CommandLine.cs @@ -118,7 +118,7 @@ namespace WixToolset.Core.CommandLine extension.PostParse(); } - return command ?? new HelpCommand(); + return command ?? new HelpCommand(extensions); } private bool TryParseCommand(string arg, ICommandLineParser parser, IEnumerable extensions, out ICommandLineCommand command) @@ -134,7 +134,7 @@ namespace WixToolset.Core.CommandLine case "h": case "help": case "-help": - command = new HelpCommand(); + command = new HelpCommand(extensions); break; case "version": diff --git a/src/WixToolset.Core/CommandLine/CommandLineParser.cs b/src/WixToolset.Core/CommandLine/CommandLineParser.cs index 11e5751d..2ee1e9ae 100644 --- a/src/WixToolset.Core/CommandLine/CommandLineParser.cs +++ b/src/WixToolset.Core/CommandLine/CommandLineParser.cs @@ -27,7 +27,7 @@ namespace WixToolset.Core.CommandLine public bool IsSwitch(string arg) { - return !String.IsNullOrEmpty(arg) && ('/' == arg[0] || '-' == arg[0]); + return !String.IsNullOrEmpty(arg) && '-' == arg[0]; } public string GetArgumentAsFilePathOrError(string argument, string fileType) @@ -74,7 +74,7 @@ namespace WixToolset.Core.CommandLine public string GetNextArgumentAsDirectoryOrError(string commandLineSwitch) { - if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && this.TryGetDirectory(commandLineSwitch, this.Messaging, arg, out var directory)) + if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && this.TryGetDirectory(commandLineSwitch, arg, out var directory)) { return directory; } @@ -85,7 +85,7 @@ namespace WixToolset.Core.CommandLine public bool GetNextArgumentAsDirectoryOrError(string commandLineSwitch, IList directories) { - if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && this.TryGetDirectory(commandLineSwitch, this.Messaging, arg, out var directory)) + if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && this.TryGetDirectory(commandLineSwitch, arg, out var directory)) { directories.Add(directory); return true; @@ -124,7 +124,14 @@ namespace WixToolset.Core.CommandLine public bool TryGetNextSwitchOrArgument(out string arg) { - return TryDequeue(this.RemainingArguments, out arg); + if (this.RemainingArguments.Count > 0) + { + arg = this.RemainingArguments.Dequeue(); + return true; + } + + arg = null; + return false; } private bool TryGetNextNonSwitchArgumentOrError(out string arg) @@ -139,24 +146,7 @@ namespace WixToolset.Core.CommandLine return result; } - private static bool IsValidArg(string arg) - { - return !(String.IsNullOrEmpty(arg) || '/' == arg[0] || '-' == arg[0]); - } - - private static bool TryDequeue(Queue q, out string arg) - { - if (q.Count > 0) - { - arg = q.Dequeue(); - return true; - } - - arg = null; - return false; - } - - private bool TryGetDirectory(string commandlineSwitch, IMessaging messageHandler, string arg, out string directory) + private bool TryGetDirectory(string commandlineSwitch, string arg, out string directory) { directory = null; @@ -174,7 +164,7 @@ namespace WixToolset.Core.CommandLine { path = null; - if (!IsValidArg(arg)) + if (String.IsNullOrEmpty(arg) || '-' == arg[0]) { this.Messaging.Write(ErrorMessages.FilePathRequired(commandlineSwitch)); } diff --git a/src/WixToolset.Core/CommandLine/HelpCommand.cs b/src/WixToolset.Core/CommandLine/HelpCommand.cs index 78845189..3af442aa 100644 --- a/src/WixToolset.Core/CommandLine/HelpCommand.cs +++ b/src/WixToolset.Core/CommandLine/HelpCommand.cs @@ -3,27 +3,60 @@ namespace WixToolset.Core.CommandLine { using System; + using System.Collections.Generic; + using System.Linq; using System.Threading; using System.Threading.Tasks; + using WixToolset.Extensibility; using WixToolset.Extensibility.Data; using WixToolset.Extensibility.Services; internal class HelpCommand : ICommandLineCommand { + private static readonly ExtensionCommandLineSwitch[] BuiltInSwitches = new ExtensionCommandLineSwitch[] + { + new ExtensionCommandLineSwitch { Switch = "build", Description = "Build a wixlib, package or bundle." }, + new ExtensionCommandLineSwitch { Switch = "decompile", Description = "Decompile a package or bundle into source code." }, + }; + + public HelpCommand(IEnumerable extensions) + { + this.Extensions = extensions; + } + public bool ShowLogo => true; public bool StopParsing => true; + private IEnumerable Extensions { get; } + public Task ExecuteAsync(CancellationToken _) { - Console.WriteLine("TODO: Show list of available commands"); + var commandLineSwitches = new List(BuiltInSwitches); + commandLineSwitches.AddRange(this.Extensions.SelectMany(e => e.CommandLineSwitches).OrderBy(s => s.Switch, StringComparer.Ordinal)); + + Console.WriteLine(); + Console.WriteLine("Usage: wix [option]"); + Console.WriteLine("Usage: wix [command]"); + Console.WriteLine(); + Console.WriteLine("Options:"); + Console.WriteLine(" -h|--help Show command line help."); + Console.WriteLine(" --version Display WiX Toolset version in use."); + Console.WriteLine(); + + Console.WriteLine("Commands:"); + foreach (var commandLineSwitch in commandLineSwitches) + { + Console.WriteLine(" {0,-17} {1}", commandLineSwitch.Switch, commandLineSwitch.Description); + } + + Console.WriteLine(); + Console.WriteLine("Run 'wix [command] --help' for more information on a command."); + AppCommon.DisplayToolFooter(); return Task.FromResult(-1); } - public bool TryParseArgument(ICommandLineParser parseHelper, string argument) - { - return true; // eat any arguments - } + public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments } } diff --git a/src/WixToolset.Core/CommandLine/VersionCommand.cs b/src/WixToolset.Core/CommandLine/VersionCommand.cs index 6ce2a89d..01a7d0e6 100644 --- a/src/WixToolset.Core/CommandLine/VersionCommand.cs +++ b/src/WixToolset.Core/CommandLine/VersionCommand.cs @@ -16,8 +16,7 @@ namespace WixToolset.Core.CommandLine public Task ExecuteAsync(CancellationToken cancellationToken) { - Console.WriteLine("wix version {0}", ThisAssembly.AssemblyInformationalVersion); - Console.WriteLine(); + Console.WriteLine(ThisAssembly.AssemblyInformationalVersion); return Task.FromResult(0); } diff --git a/src/WixToolset.Core/ExtensibilityServices/Messaging.cs b/src/WixToolset.Core/ExtensibilityServices/Messaging.cs index 266d842a..afcd9244 100644 --- a/src/WixToolset.Core/ExtensibilityServices/Messaging.cs +++ b/src/WixToolset.Core/ExtensibilityServices/Messaging.cs @@ -2,10 +2,7 @@ namespace WixToolset.Core.ExtensibilityServices { - using System; using System.Collections.Generic; - using System.Globalization; - using System.Text; using WixToolset.Data; using WixToolset.Extensibility; using WixToolset.Extensibility.Services; @@ -13,8 +10,8 @@ namespace WixToolset.Core.ExtensibilityServices internal class Messaging : IMessaging { private IMessageListener listener; - private HashSet suppressedWarnings = new HashSet(); - private HashSet warningsAsErrors = new HashSet(); + private readonly HashSet suppressedWarnings = new HashSet(); + private readonly HashSet warningsAsErrors = new HashSet(); public bool EncounteredError { get; private set; } @@ -26,20 +23,11 @@ namespace WixToolset.Core.ExtensibilityServices public bool WarningsAsError { get; set; } - public void ElevateWarningMessage(int warningNumber) - { - this.warningsAsErrors.Add(warningNumber); - } + public void ElevateWarningMessage(int warningNumber) => this.warningsAsErrors.Add(warningNumber); - public void SetListener(IMessageListener listener) - { - this.listener = listener; - } + public void SetListener(IMessageListener listener) => this.listener = listener; - public void SuppressWarningMessage(int warningNumber) - { - this.suppressedWarnings.Add(warningNumber); - } + public void SuppressWarningMessage(int warningNumber) => this.suppressedWarnings.Add(warningNumber); public void Write(Message message) { -- cgit v1.2.3-55-g6feb