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 --- 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 +- 4 files changed, 54 insertions(+), 32 deletions(-) (limited to 'src/WixToolset.Core/CommandLine') 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); } -- cgit v1.2.3-55-g6feb