// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #nullable enable using System.Management.Automation; using System.Management.Automation.Runspaces; namespace Microsoft.PowerShell { /// /// This class provides an entry point which is called /// to transfer control to console host implementation. /// public static class ConsoleShell { /// Entry point in to ConsoleShell. This method is called by main of minishell. /// Banner text to be displayed by ConsoleHost. /// Help text for minishell. This is displayed on 'minishell -?'. /// Commandline parameters specified by user. /// An integer value which should be used as exit code for the process. public static int Start(string? bannerText, string? helpText, string[] args) { return StartImpl( initialSessionState: InitialSessionState.CreateDefault2(), bannerText, helpText, args, issProvided: false); } /// Entry point in to ConsoleShell. Used to create a custom Powershell console application. /// InitialSessionState to be used by the ConsoleHost. /// Banner text to be displayed by ConsoleHost. /// Help text for the shell. /// Commandline parameters specified by user. /// An integer value which should be used as exit code for the process. public static int Start(InitialSessionState initialSessionState, string? bannerText, string? helpText, string[] args) { return StartImpl( initialSessionState, bannerText, helpText, args, issProvided: true); } /// /// Implementation of entry point to ConsoleShell. /// Used to create a custom Powershell console application. /// /// InitialSessionState to be used by the ConsoleHost. /// Banner text to be displayed by ConsoleHost. /// Help text for the shell. /// Commandline parameters specified by user. /// True when the InitialSessionState object is provided by caller. /// An integer value which should be used as exit code for the process. private static int StartImpl( InitialSessionState initialSessionState, string? bannerText, string? helpText, string[] args, bool issProvided) { if (initialSessionState == null) { throw PSTraceSource.NewArgumentNullException(nameof(initialSessionState)); } if (args == null) { throw PSTraceSource.NewArgumentNullException(nameof(args)); } ConsoleHost.ParseCommandLine(args); ConsoleHost.DefaultInitialSessionState = initialSessionState; return ConsoleHost.Start(bannerText, helpText, issProvided); } } }