Skip to content

Provide format output parameter #1602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Host;
using Microsoft.CLU;

namespace Microsoft.Azure.Commands.Common.Test.Mocks
{
Expand Down Expand Up @@ -285,6 +286,8 @@ public override Version Version
}
}

public override OutputFormat RequestedOutputFormat { get; set; }

class MockPSHostUI : PSHostUserInterface
{
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
Expand Down
35 changes: 35 additions & 0 deletions src/CLU/Microsoft.CLU.Test/StringGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using System.Management.Automation;

namespace Microsoft.CLU.Test
{
[Cmdlet(VerbsCommon.New, "String")]
public class StringGenerator : PSCmdlet
{
public StringGenerator()
{
Count = 10;
StringFormat = "String {0}";
}

[Parameter()]
public int Count { get; set; }

[Parameter()]
public string StringFormat { get; set; }

protected override void ProcessRecord()
{
base.ProcessRecord();
for (int i = 1; i <= Count; ++i)
{
WriteObject(String.Format(StringFormat, i));
}
}

}
}
2 changes: 1 addition & 1 deletion src/CLU/Microsoft.CLU/CommandModel/CmdletCommandModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CommandModelErrorCode Run(ConfigurationDictionary commandConfiguration, s
// The runtime host is a Cmdlet's path to accessing system features, such as Console I/O
// and session state. The runtime instance is created here and passed into the binder,
// which will be creating the Cmdlet instance.
var runtimeHost = new System.Management.Automation.Host.CLUHost(arguments, hostStreamInfo);
var runtimeHost = new System.Management.Automation.Host.CLUHost(ref arguments, hostStreamInfo);

// Create instance of ICommandBinder and ICommand implementation for cmdlet model
var binderAndCommand = new CmdletBinderAndCommand(commandConfiguration, runtimeHost);
Expand Down
43 changes: 39 additions & 4 deletions src/CLU/Microsoft.CLU/System.Management.Automation/CLUHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class CLUHost : PSHost, ICommandRuntime
/// Creates an instance of CLUHost.
/// </summary>
/// <param name="args">The commandline arguments</param>
internal CLUHost(string[] args, HostStreamInfo hostStreamInfo)
internal CLUHost(ref string[] args, HostStreamInfo hostStreamInfo)
{
Debug.Assert(args != null);
Debug.Assert(hostStreamInfo != null);
Expand All @@ -37,12 +37,29 @@ internal CLUHost(string[] args, HostStreamInfo hostStreamInfo)
if (!string.IsNullOrEmpty(vpref))
_doVerbose = InterpretStreamPreference(Constants.VerbosePreference, vpref, _doVerbose);

var argList = args.ToList();

// Command-line switch overrides environment variable
if (args.Select(arg => arg.ToLowerInvariant()).Where(arg => arg.Equals("--debug") || arg.Equals("/debug")).Any())
if (RemoveArgumentIfFound(argList, "debug"))
{
_doDebug = Constants.CmdletPreferencesInquire;

if (args.Select(arg => arg.ToLowerInvariant()).Where(arg => arg.Equals("--verbose") || arg.Equals("/verbose")).Any())
}

if (RemoveArgumentIfFound(argList, "verbose"))
{
_doVerbose = Constants.CmdletPreferencesContinue;
}

if (RemoveArgumentIfFound(argList, "json"))
{
this.RequestedOutputFormat = OutputFormat.JSON;
}
else if (RemoveArgumentIfFound(argList, "display"))
{
this.RequestedOutputFormat = OutputFormat.Display;
}

args = argList.ToArray();
}

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

/// <summary>
/// Command line override of requested output format...
/// </summary>
public override OutputFormat RequestedOutputFormat { get; set; }

/// <summary>
/// Tells whether the STDOUT byte stream has been redirected to a file or pipe
/// </summary>
Expand Down Expand Up @@ -514,6 +536,19 @@ private string InterpretStreamPreference(string variable, string input, string c
new ChoiceDescription("&Halt Command", "Stop this command.")
};
private HostStreamInfo _hostStreamInfo;
private bool RemoveArgumentIfFound(List<string> args, string argName)
{
int index = args.FindIndex(arg => arg.ToLowerInvariant().Equals($"--{argName}") || arg.ToLowerInvariant().Equals($"/{argName}"));
if (index > -1)
{
args.RemoveAt(index);
return true;
}
else
{
return false;
}
}

#endregion

Expand Down
18 changes: 12 additions & 6 deletions src/CLU/Microsoft.CLU/System.Management.Automation/Cmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ protected void WriteObject(object sendToPipeline)
{
if (sendToPipeline != null)
{
if (CommandRuntime.Host.IsOutputRedirected)
{
if (ShouldWriteJsonOutput())
{
CommandRuntime.WriteObject(sendToPipeline);
}
else
Expand All @@ -78,7 +78,7 @@ protected void WriteObject(object sendToPipeline, bool enumerateCollection)
{
if (sendToPipeline != null)
{
if (CommandRuntime.Host.IsOutputRedirected)
if (ShouldWriteJsonOutput())
{
CommandRuntime.WriteObject(sendToPipeline, enumerateCollection);
}
Expand Down Expand Up @@ -142,15 +142,21 @@ internal void FlushPipeline(LocalPackage package)
formattedType = obj.GetType();
if (!wroteHeader)
{
CommandRuntime.WriteCommandDetail(view.FormatHeader(CLUEnvironment.Console.WindowWidth));
CommandRuntime.WriteCommandDetail("");
CommandRuntime.WriteObject(view.FormatHeader(CLUEnvironment.Console.WindowWidth));
CommandRuntime.WriteObject("");
wroteHeader = true;
}
CommandRuntime.WriteCommandDetail(view.FormatObject(obj));
CommandRuntime.WriteObject(view.FormatObject(obj));
}
}
}

internal bool ShouldWriteJsonOutput()
{
var requestedOutputFormat = CommandRuntime.Host.RequestedOutputFormat;
return (CommandRuntime.Host.IsOutputRedirected && requestedOutputFormat == OutputFormat.Auto)
|| requestedOutputFormat == OutputFormat.JSON;
}
protected void WriteProgress(ProgressRecord progressRecord) { if (progressRecord != null) CommandRuntime.WriteProgress(progressRecord); }

protected void WriteVerbose(string text) { if (!string.IsNullOrEmpty(text)) CommandRuntime.WriteVerbose(text); }
Expand Down
14 changes: 14 additions & 0 deletions src/CLU/Microsoft.CLU/System.Management.Automation/OutputFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.CLU
{
public enum OutputFormat
{
Auto = 0,
JSON = 1,
Display = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ protected PSHost() { }
public abstract Version Version { get; }
public abstract bool IsInputRedirected { get; }
public abstract bool IsOutputRedirected { get; }
public abstract Microsoft.CLU.OutputFormat RequestedOutputFormat { get; set; }
}
}