Skip to content
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 @@ -14,20 +14,168 @@
using System.Management.Automation.Internal;
using System.Diagnostics;
using Dbg = System.Management.Automation.Diagnostics;
using System.Management.Automation.Host;
using System.Collections.Generic;
using System.Security;

namespace Microsoft.PowerShell
{
/// <summary>
/// Null class implementation of PSHostUserInterface used when no console is available and when PowerShell
/// is run in servmode where no console is needed.
/// </summary>
internal class NullHostUserInterface : PSHostUserInterface
{
/// <summary>
/// RawUI
/// </summary>
public override PSHostRawUserInterface RawUI
{
get { return null; }
}

/// <summary>
/// Prompt
/// </summary>
/// <param name="caption"></param>
/// <param name="message"></param>
/// <param name="descriptions"></param>
/// <returns></returns>
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
throw new PSNotImplementedException();
}

/// <summary>
/// PromptForChoice
/// </summary>
/// <param name="caption"></param>
/// <param name="message"></param>
/// <param name="choices"></param>
/// <param name="defaultChoice"></param>
/// <returns></returns>
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
throw new PSNotImplementedException();
}

/// <summary>
/// PromptForCredential
/// </summary>
/// <param name="caption"></param>
/// <param name="message"></param>
/// <param name="userName"></param>
/// <param name="targetName"></param>
/// <returns></returns>
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
throw new PSNotImplementedException();
}

/// <summary>
/// PromptForCredential
/// </summary>
/// <param name="caption"></param>
/// <param name="message"></param>
/// <param name="userName"></param>
/// <param name="targetName"></param>
/// <param name="allowedCredentialTypes"></param>
/// <param name="options"></param>
/// <returns></returns>
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
throw new PSNotImplementedException();
}

/// <summary>
/// ReadLine
/// </summary>
/// <returns></returns>
public override string ReadLine()
{
throw new PSNotImplementedException();
}

/// <summary>
/// ReadLineAsSecureString
/// </summary>
/// <returns></returns>
public override SecureString ReadLineAsSecureString()
{
throw new PSNotImplementedException();
}

/// <summary>
/// Write
/// </summary>
/// <param name="value"></param>
public override void Write(string value)
{ }

/// <summary>
/// Write
/// </summary>
/// <param name="foregroundColor"></param>
/// <param name="backgroundColor"></param>
/// <param name="value"></param>
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{ }

/// <summary>
/// WriteDebugLine
/// </summary>
/// <param name="message"></param>
public override void WriteDebugLine(string message)
{ }

/// <summary>
/// WriteErrorLine
/// </summary>
/// <param name="value"></param>
public override void WriteErrorLine(string value)
{
Console.Out.WriteLine(value);
}

/// <summary>
/// WriteLine
/// </summary>
/// <param name="value"></param>
public override void WriteLine(string value)
{ }

/// <summary>
/// WriteProgress
/// </summary>
/// <param name="sourceId"></param>
/// <param name="record"></param>
public override void WriteProgress(long sourceId, ProgressRecord record)
{ }

/// <summary>
/// WriteVerboseLine
/// </summary>
/// <param name="message"></param>
public override void WriteVerboseLine(string message)
{ }

/// <summary>
/// WriteWarningLine
/// </summary>
/// <param name="message"></param>
public override void WriteWarningLine(string message)
{ }
}

internal class CommandLineParameterParser
{
internal CommandLineParameterParser(ConsoleHost p, Version ver, string bannerText, string helpText)
internal CommandLineParameterParser(PSHostUserInterface hostUI, string bannerText, string helpText)
{
Dbg.Assert(p != null, "parent ConsoleHost must be supplied");
if (hostUI == null) { throw new PSArgumentNullException("hostUI"); }
_hostUI = hostUI;

_bannerText = bannerText;
_helpText = helpText;
_parent = p;
_ui = (ConsoleHostUserInterface)p.UI;
_ver = ver;
}

internal bool AbortStartup
Expand Down Expand Up @@ -220,25 +368,25 @@ internal bool NonInteractive

private void ShowHelp()
{
_ui.WriteLine("");
_hostUI.WriteLine("");
if (_helpText == null)
{
_ui.WriteLine(CommandLineParameterParserStrings.DefaultHelp);
_hostUI.WriteLine(CommandLineParameterParserStrings.DefaultHelp);
}
else
{
_ui.Write(_helpText);
_hostUI.Write(_helpText);
}
_ui.WriteLine("");
_hostUI.WriteLine("");
}

private void DisplayBanner()
{
// If banner text is not supplied do nothing.
if (!String.IsNullOrEmpty(_bannerText))
{
_ui.WriteLine(_bannerText);
_ui.WriteLine();
_hostUI.WriteLine(_bannerText);
_hostUI.WriteLine();
}
}

Expand Down Expand Up @@ -578,9 +726,9 @@ private void ParseHelper(string[] args)
else if (MatchSwitch(switchKey, "wait", "w"))
{
// This does not need to be localized: its chk only

_ui.WriteToConsole("Waiting - type enter to continue:", false);
_ui.ReadLine();
((ConsoleHostUserInterface)_hostUI).WriteToConsole("Waiting - type enter to continue:", false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about when _hostUI is a NullHostUserInterface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this will fail. Note that this is for debugging interactive console only and should never be called without ConsoleHostUserInterface.


In reply to: 85439307 [](ancestors = 85439307)

_hostUI.ReadLine();
}

// this option is useful for testing the initial InitialSessionState experience
Expand Down Expand Up @@ -628,7 +776,7 @@ private void ParseHelper(string[] args)
}
if (moduleCount < 1)
{
_ui.WriteErrorLine("No modules specified for -module option");
_hostUI.WriteErrorLine("No modules specified for -module option");
}
}
#endif
Expand Down Expand Up @@ -717,7 +865,7 @@ private void ParseHelper(string[] args)

private void WriteCommandLineError(string msg, bool showHelp = false, bool showBanner = false)
{
_ui.WriteErrorLine(msg);
_hostUI.WriteErrorLine(msg);
_showHelp = showHelp;
_showBanner = showBanner;
_abortStartup = true;
Expand Down Expand Up @@ -755,7 +903,7 @@ private void ParseFormat(string[] args, ref int i, ref Serialization.DataFormat
++i;
if (i >= args.Length)
{
_ui.WriteErrorLine(
_hostUI.WriteErrorLine(
StringUtil.Format(
resourceStr,
sb.ToString()));
Expand All @@ -771,7 +919,7 @@ private void ParseFormat(string[] args, ref int i, ref Serialization.DataFormat
}
catch (ArgumentException)
{
_ui.WriteErrorLine(
_hostUI.WriteErrorLine(
StringUtil.Format(
CommandLineParameterParserStrings.BadFormatParameterValue,
args[i],
Expand All @@ -787,7 +935,7 @@ private void ParseExecutionPolicy(string[] args, ref int i, ref string execution
++i;
if (i >= args.Length)
{
_ui.WriteErrorLine(resourceStr);
_hostUI.WriteErrorLine(resourceStr);

_showHelp = true;
_abortStartup = true;
Expand All @@ -804,7 +952,7 @@ private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEnco
{
// we've already set the command, so squawk

_ui.WriteErrorLine(CommandLineParameterParserStrings.CommandAlreadySpecified);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.CommandAlreadySpecified);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -814,7 +962,7 @@ private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEnco
++i;
if (i >= args.Length)
{
_ui.WriteErrorLine(CommandLineParameterParserStrings.MissingCommandParameter);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.MissingCommandParameter);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -830,7 +978,7 @@ private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEnco
// decoding failed
catch
{
_ui.WriteErrorLine(CommandLineParameterParserStrings.BadCommandValue);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.BadCommandValue);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -849,7 +997,7 @@ private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEnco
{
// there are more parameters to -command than -, which is an error.

_ui.WriteErrorLine(CommandLineParameterParserStrings.TooManyParametersToCommand);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.TooManyParametersToCommand);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -858,7 +1006,7 @@ private bool ParseCommand(string[] args, ref int i, bool noexitSeen, bool isEnco

if (!Console.IsInputRedirected)
{
_ui.WriteErrorLine(CommandLineParameterParserStrings.StdinNotRedirected);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.StdinNotRedirected);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand Down Expand Up @@ -899,7 +1047,7 @@ private bool CollectArgs(string[] args, ref int i)
{
if (_collectedArgs.Count != 0)
{
_ui.WriteErrorLine(CommandLineParameterParserStrings.ArgsAlreadySpecified);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.ArgsAlreadySpecified);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -909,7 +1057,7 @@ private bool CollectArgs(string[] args, ref int i)
++i;
if (i >= args.Length)
{
_ui.WriteErrorLine(CommandLineParameterParserStrings.MissingArgsValue);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.MissingArgsValue);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -931,7 +1079,7 @@ private bool CollectArgs(string[] args, ref int i)
{
// decoding failed

_ui.WriteErrorLine(CommandLineParameterParserStrings.BadArgsValue);
_hostUI.WriteErrorLine(CommandLineParameterParserStrings.BadArgsValue);
_showHelp = true;
_abortStartup = true;
_exitCode = ConsoleHost.ExitCodeBadCommandLineParameter;
Expand All @@ -946,8 +1094,7 @@ private bool CollectArgs(string[] args, ref int i)
private bool _namedPipeServerMode;
private bool _sshServerMode;
private string _configurationName;
private ConsoleHost _parent;
private ConsoleHostUserInterface _ui;
private PSHostUserInterface _hostUI;
private bool _showHelp;
private bool _showBanner = true;
private bool _noInteractive;
Expand All @@ -969,7 +1116,6 @@ private bool CollectArgs(string[] args, ref int i)
private bool _wasCommandEncoded;
private uint _exitCode = ConsoleHost.ExitCodeSuccess;
private bool _dirty;
private Version _ver;
private Serialization.DataFormat _outFormat = Serialization.DataFormat.Text;
private Serialization.DataFormat _inFormat = Serialization.DataFormat.Text;
private Collection<CommandParameter> _collectedArgs = new Collection<CommandParameter>();
Expand Down
Loading