From 8ddba4d072887211cf745e8740092f200eff98b5 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 26 Aug 2016 12:30:38 -0700 Subject: [PATCH] Allow setting output and error streams to make CommnadLineApplication easier to test. --- .../CommandLine/CommandLineApplication.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs index f1c91519ec5..2144009987e 100644 --- a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs +++ b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs @@ -4,6 +4,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -43,6 +44,8 @@ public CommandLineApplication(bool throwOnUnexpectedArg = true) public Func LongVersionGetter { get; set; } public Func ShortVersionGetter { get; set; } public readonly List Commands; + public TextWriter Out { get; set; } = Console.Out; + public TextWriter Error { get; set; } = Console.Error; public IEnumerable GetOptions() { @@ -327,7 +330,7 @@ public void ShowHint() { if (OptionHelp != null) { - Console.WriteLine(string.Format("Specify --{0} for a list of available options and commands.", OptionHelp.LongName)); + Out.WriteLine(string.Format("Specify --{0} for a list of available options and commands.", OptionHelp.LongName)); } } @@ -339,7 +342,7 @@ public void ShowHelp(string commandName = null) cmd.IsShowingInformation = true; } - Console.WriteLine(GetHelpText(commandName)); + Out.WriteLine(GetHelpText(commandName)); } public virtual string GetHelpText(string commandName = null) @@ -451,8 +454,8 @@ public void ShowVersion() cmd.IsShowingInformation = true; } - Console.WriteLine(FullName); - Console.WriteLine(LongVersionGetter()); + Out.WriteLine(FullName); + Out.WriteLine(LongVersionGetter()); } public string GetFullNameAndVersion() @@ -468,8 +471,8 @@ public void ShowRootCommandFullNameAndVersion() rootCmd = rootCmd.Parent; } - Console.WriteLine(rootCmd.GetFullNameAndVersion()); - Console.WriteLine(); + Out.WriteLine(rootCmd.GetFullNameAndVersion()); + Out.WriteLine(); } private void HandleUnexpectedArg(CommandLineApplication command, string[] args, int index, string argTypeName)