Skip to content

Commit 64b6c47

Browse files
committed
Merge pull request Azure#1602 from johanste/provide-format-output-parameter
Provide format output parameter
2 parents 1476c6f + 415273c commit 64b6c47

File tree

7 files changed

+105
-11
lines changed

7 files changed

+105
-11
lines changed

src/CLU/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockCommandRuntime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Globalization;
2121
using System.Management.Automation;
2222
using System.Management.Automation.Host;
23+
using Microsoft.CLU;
2324

2425
namespace Microsoft.Azure.Commands.Common.Test.Mocks
2526
{
@@ -285,6 +286,8 @@ public override Version Version
285286
}
286287
}
287288

289+
public override OutputFormat RequestedOutputFormat { get; set; }
290+
288291
class MockPSHostUI : PSHostUserInterface
289292
{
290293
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
using System.Management.Automation;
7+
8+
namespace Microsoft.CLU.Test
9+
{
10+
[Cmdlet(VerbsCommon.New, "String")]
11+
public class StringGenerator : PSCmdlet
12+
{
13+
public StringGenerator()
14+
{
15+
Count = 10;
16+
StringFormat = "String {0}";
17+
}
18+
19+
[Parameter()]
20+
public int Count { get; set; }
21+
22+
[Parameter()]
23+
public string StringFormat { get; set; }
24+
25+
protected override void ProcessRecord()
26+
{
27+
base.ProcessRecord();
28+
for (int i = 1; i <= Count; ++i)
29+
{
30+
WriteObject(String.Format(StringFormat, i));
31+
}
32+
}
33+
34+
}
35+
}

src/CLU/Microsoft.CLU/CommandModel/CmdletCommandModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public CommandModelErrorCode Run(ConfigurationDictionary commandConfiguration, s
4343
// The runtime host is a Cmdlet's path to accessing system features, such as Console I/O
4444
// and session state. The runtime instance is created here and passed into the binder,
4545
// which will be creating the Cmdlet instance.
46-
var runtimeHost = new System.Management.Automation.Host.CLUHost(arguments, hostStreamInfo);
46+
var runtimeHost = new System.Management.Automation.Host.CLUHost(ref arguments, hostStreamInfo);
4747

4848
// Create instance of ICommandBinder and ICommand implementation for cmdlet model
4949
var binderAndCommand = new CmdletBinderAndCommand(commandConfiguration, runtimeHost);

src/CLU/Microsoft.CLU/System.Management.Automation/CLUHost.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class CLUHost : PSHost, ICommandRuntime
2222
/// Creates an instance of CLUHost.
2323
/// </summary>
2424
/// <param name="args">The commandline arguments</param>
25-
internal CLUHost(string[] args, HostStreamInfo hostStreamInfo)
25+
internal CLUHost(ref string[] args, HostStreamInfo hostStreamInfo)
2626
{
2727
Debug.Assert(args != null);
2828
Debug.Assert(hostStreamInfo != null);
@@ -37,12 +37,29 @@ internal CLUHost(string[] args, HostStreamInfo hostStreamInfo)
3737
if (!string.IsNullOrEmpty(vpref))
3838
_doVerbose = InterpretStreamPreference(Constants.VerbosePreference, vpref, _doVerbose);
3939

40+
var argList = args.ToList();
41+
4042
// Command-line switch overrides environment variable
41-
if (args.Select(arg => arg.ToLowerInvariant()).Where(arg => arg.Equals("--debug") || arg.Equals("/debug")).Any())
43+
if (RemoveArgumentIfFound(argList, "debug"))
44+
{
4245
_doDebug = Constants.CmdletPreferencesInquire;
43-
44-
if (args.Select(arg => arg.ToLowerInvariant()).Where(arg => arg.Equals("--verbose") || arg.Equals("/verbose")).Any())
46+
}
47+
48+
if (RemoveArgumentIfFound(argList, "verbose"))
49+
{
4550
_doVerbose = Constants.CmdletPreferencesContinue;
51+
}
52+
53+
if (RemoveArgumentIfFound(argList, "json"))
54+
{
55+
this.RequestedOutputFormat = OutputFormat.JSON;
56+
}
57+
else if (RemoveArgumentIfFound(argList, "display"))
58+
{
59+
this.RequestedOutputFormat = OutputFormat.Display;
60+
}
61+
62+
args = argList.ToArray();
4663
}
4764

4865
internal CLUHost(string[] args, HostStreamInfo hostStreamInfo, string debugPreference, string verbosePreference)
@@ -82,6 +99,11 @@ internal HostStreamInfo StreamInfo
8299
/// <remarks>If this is true, your code is not doing console I/O</remarks>
83100
public override bool IsInputRedirected { get { return _hostStreamInfo.IsInputRedirected; } }
84101

102+
/// <summary>
103+
/// Command line override of requested output format...
104+
/// </summary>
105+
public override OutputFormat RequestedOutputFormat { get; set; }
106+
85107
/// <summary>
86108
/// Tells whether the STDOUT byte stream has been redirected to a file or pipe
87109
/// </summary>
@@ -514,6 +536,19 @@ private string InterpretStreamPreference(string variable, string input, string c
514536
new ChoiceDescription("&Halt Command", "Stop this command.")
515537
};
516538
private HostStreamInfo _hostStreamInfo;
539+
private bool RemoveArgumentIfFound(List<string> args, string argName)
540+
{
541+
int index = args.FindIndex(arg => arg.ToLowerInvariant().Equals($"--{argName}") || arg.ToLowerInvariant().Equals($"/{argName}"));
542+
if (index > -1)
543+
{
544+
args.RemoveAt(index);
545+
return true;
546+
}
547+
else
548+
{
549+
return false;
550+
}
551+
}
517552

518553
#endregion
519554

src/CLU/Microsoft.CLU/System.Management.Automation/Cmdlet.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ protected void WriteObject(object sendToPipeline)
6363
{
6464
if (sendToPipeline != null)
6565
{
66-
if (CommandRuntime.Host.IsOutputRedirected)
67-
{
66+
if (ShouldWriteJsonOutput())
67+
{
6868
CommandRuntime.WriteObject(sendToPipeline);
6969
}
7070
else
@@ -78,7 +78,7 @@ protected void WriteObject(object sendToPipeline, bool enumerateCollection)
7878
{
7979
if (sendToPipeline != null)
8080
{
81-
if (CommandRuntime.Host.IsOutputRedirected)
81+
if (ShouldWriteJsonOutput())
8282
{
8383
CommandRuntime.WriteObject(sendToPipeline, enumerateCollection);
8484
}
@@ -142,15 +142,21 @@ internal void FlushPipeline(LocalPackage package)
142142
formattedType = obj.GetType();
143143
if (!wroteHeader)
144144
{
145-
CommandRuntime.WriteCommandDetail(view.FormatHeader(CLUEnvironment.Console.WindowWidth));
146-
CommandRuntime.WriteCommandDetail("");
145+
CommandRuntime.WriteObject(view.FormatHeader(CLUEnvironment.Console.WindowWidth));
146+
CommandRuntime.WriteObject("");
147147
wroteHeader = true;
148148
}
149-
CommandRuntime.WriteCommandDetail(view.FormatObject(obj));
149+
CommandRuntime.WriteObject(view.FormatObject(obj));
150150
}
151151
}
152152
}
153153

154+
internal bool ShouldWriteJsonOutput()
155+
{
156+
var requestedOutputFormat = CommandRuntime.Host.RequestedOutputFormat;
157+
return (CommandRuntime.Host.IsOutputRedirected && requestedOutputFormat == OutputFormat.Auto)
158+
|| requestedOutputFormat == OutputFormat.JSON;
159+
}
154160
protected void WriteProgress(ProgressRecord progressRecord) { if (progressRecord != null) CommandRuntime.WriteProgress(progressRecord); }
155161

156162
protected void WriteVerbose(string text) { if (!string.IsNullOrEmpty(text)) CommandRuntime.WriteVerbose(text); }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.CLU
7+
{
8+
public enum OutputFormat
9+
{
10+
Auto = 0,
11+
JSON = 1,
12+
Display = 2
13+
}
14+
}

src/CLU/Microsoft.CLU/System.Management.Automation/PSHost.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ protected PSHost() { }
1414
public abstract Version Version { get; }
1515
public abstract bool IsInputRedirected { get; }
1616
public abstract bool IsOutputRedirected { get; }
17+
public abstract Microsoft.CLU.OutputFormat RequestedOutputFormat { get; set; }
1718
}
1819
}

0 commit comments

Comments
 (0)