private void DisplayBanner(PSHostUserInterface hostUI, string?bannerText) { if (_showBanner && !_showHelp) { // If banner text is not supplied do nothing. if (!string.IsNullOrEmpty(bannerText)) { hostUI.WriteLine(bannerText); } if (UpdatesNotification.CanNotifyUpdates) { UpdatesNotification.ShowUpdateNotification(hostUI); } } }
private void ParseHelper(string[] args) { Dbg.Assert(args != null, "Argument 'args' to ParseHelper should never be null"); bool noexitSeen = false; for (int i = 0; i < args.Length; ++i) { (string SwitchKey, bool ShouldBreak)switchKeyResults = GetSwitchKey(args, ref i, this, ref noexitSeen); if (switchKeyResults.ShouldBreak) { break; } string switchKey = switchKeyResults.SwitchKey; // If version is in the commandline, don't continue to look at any other parameters if (MatchSwitch(switchKey, "version", "v")) { _showVersion = true; _showBanner = false; _noInteractive = true; _skipUserInit = true; _noExit = false; break; } if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?")) { _showHelp = true; _showExtendedHelp = true; _abortStartup = true; } else if (MatchSwitch(switchKey, "login", "l")) { // This handles -Login on Windows only, where it does nothing. // On *nix, -Login is handled much earlier to improve startup performance. } else if (MatchSwitch(switchKey, "noexit", "noe")) { _noExit = true; noexitSeen = true; } else if (MatchSwitch(switchKey, "noprofile", "nop")) { _skipUserInit = true; } else if (MatchSwitch(switchKey, "nologo", "nol")) { _showBanner = false; } else if (MatchSwitch(switchKey, "noninteractive", "noni")) { _noInteractive = true; } else if (MatchSwitch(switchKey, "socketservermode", "so")) { _socketServerMode = true; } else if (MatchSwitch(switchKey, "servermode", "s")) { _serverMode = true; } else if (MatchSwitch(switchKey, "namedpipeservermode", "nam")) { _namedPipeServerMode = true; } else if (MatchSwitch(switchKey, "sshservermode", "sshs")) { _sshServerMode = true; } else if (MatchSwitch(switchKey, "interactive", "i")) { _noInteractive = false; } else if (MatchSwitch(switchKey, "configurationname", "config")) { ++i; if (i >= args.Length) { WriteCommandLineError( CommandLineParameterParserStrings.MissingConfigurationNameArgument); break; } _configurationName = args[i]; } else if (MatchSwitch(switchKey, "custompipename", "cus")) { ++i; if (i >= args.Length) { WriteCommandLineError( CommandLineParameterParserStrings.MissingCustomPipeNameArgument); break; } if (!Platform.IsWindows) { int maxNameLength = (Platform.IsLinux ? MaxPipePathLengthLinux : MaxPipePathLengthMacOS) - Path.GetTempPath().Length; if (args[i].Length > maxNameLength) { WriteCommandLineError( string.Format( CommandLineParameterParserStrings.CustomPipeNameTooLong, maxNameLength, args[i], args[i].Length)); break; } } _customPipeName = args[i]; } else if (MatchSwitch(switchKey, "command", "c")) { if (!ParseCommand(args, ref i, noexitSeen, false)) { break; } } else if (MatchSwitch(switchKey, "windowstyle", "w")) { #if UNIX WriteCommandLineError( CommandLineParameterParserStrings.WindowStyleArgumentNotImplemented); break; #else ++i; if (i >= args.Length) { WriteCommandLineError( CommandLineParameterParserStrings.MissingWindowStyleArgument); break; } try { ProcessWindowStyle style = (ProcessWindowStyle)LanguagePrimitives.ConvertTo( args[i], typeof(ProcessWindowStyle), CultureInfo.InvariantCulture); ConsoleControl.SetConsoleMode(style); } catch (PSInvalidCastException e) { WriteCommandLineError( string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidWindowStyleArgument, args[i], e.Message)); break; } #endif } else if (MatchSwitch(switchKey, "file", "f")) { if (!ParseFile(args, ref i, noexitSeen)) { break; } } #if DEBUG // this option is useful when debugging ConsoleHost remotely using VS remote debugging, as you can only // attach to an already running process with that debugger. else if (MatchSwitch(switchKey, "wait", "w")) { // This does not need to be localized: its chk only ((ConsoleHostUserInterface)_hostUI).WriteToConsole("Waiting - type enter to continue:", false); _hostUI.ReadLine(); } // this option is useful for testing the initial InitialSessionState experience else if (MatchSwitch(switchKey, "iss", "iss")) { // Just toss this option, it was processed earlier... } // this option is useful for testing the initial InitialSessionState experience // this is independent of the normal wait switch because configuration processing // happens so early in the cycle... else if (MatchSwitch(switchKey, "isswait", "isswait")) { // Just toss this option, it was processed earlier... } else if (MatchSwitch(switchKey, "modules", "mod")) { if (ConsoleHost.DefaultInitialSessionState == null) { WriteCommandLineError( "The -module option can only be specified with the -iss option.", showHelp: true, showBanner: false); break; } ++i; int moduleCount = 0; // Accumulate the arguments to this script... while (i < args.Length) { string arg = args[i]; if (!string.IsNullOrEmpty(arg) && CharExtensions.IsDash(arg[0])) { break; } else { ConsoleHost.DefaultInitialSessionState.ImportPSModule(new string[] { arg }); moduleCount++; } ++i; } if (moduleCount < 1) { _hostUI.WriteErrorLine("No modules specified for -module option"); } } #endif else if (MatchSwitch(switchKey, "outputformat", "o") || MatchSwitch(switchKey, "of", "o")) { ParseFormat(args, ref i, ref _outFormat, CommandLineParameterParserStrings.MissingOutputFormatParameter); _outputFormatSpecified = true; } else if (MatchSwitch(switchKey, "inputformat", "in") || MatchSwitch(switchKey, "if", "if")) { ParseFormat(args, ref i, ref _inFormat, CommandLineParameterParserStrings.MissingInputFormatParameter); } else if (MatchSwitch(switchKey, "executionpolicy", "ex") || MatchSwitch(switchKey, "ep", "ep")) { ParseExecutionPolicy(args, ref i, ref _executionPolicy, CommandLineParameterParserStrings.MissingExecutionPolicyParameter); } else if (MatchSwitch(switchKey, "encodedcommand", "e") || MatchSwitch(switchKey, "ec", "e")) { _wasCommandEncoded = true; if (!ParseCommand(args, ref i, noexitSeen, true)) { break; } } else if (MatchSwitch(switchKey, "encodedarguments", "encodeda") || MatchSwitch(switchKey, "ea", "ea")) { if (!CollectArgs(args, ref i)) { break; } } else if (MatchSwitch(switchKey, "settingsfile", "settings")) { // Parse setting file arg and write error if (!TryParseSettingFileHelper(args, ++i, this)) { break; } } else if (MatchSwitch(switchKey, "sta", "s")) { if (!Platform.IsWindowsDesktop) { WriteCommandLineError( CommandLineParameterParserStrings.STANotImplemented); break; } if (_staMode.HasValue) { // -sta and -mta are mutually exclusive. WriteCommandLineError( CommandLineParameterParserStrings.MtaStaMutuallyExclusive); break; } _staMode = true; } else if (MatchSwitch(switchKey, "mta", "mta")) { if (!Platform.IsWindowsDesktop) { WriteCommandLineError( CommandLineParameterParserStrings.MTANotImplemented); break; } if (_staMode.HasValue) { // -sta and -mta are mutually exclusive. WriteCommandLineError( CommandLineParameterParserStrings.MtaStaMutuallyExclusive); break; } _staMode = false; } else if (MatchSwitch(switchKey, "workingdirectory", "wo") || MatchSwitch(switchKey, "wd", "wd")) { ++i; if (i >= args.Length) { WriteCommandLineError( CommandLineParameterParserStrings.MissingWorkingDirectoryArgument); break; } _workingDirectory = args[i]; } #if !UNIX else if (MatchSwitch(switchKey, "removeworkingdirectorytrailingcharacter", "removeworkingdirectorytrailingcharacter")) { _removeWorkingDirectoryTrailingCharacter = true; } #endif else { // The first parameter we fail to recognize marks the beginning of the file string. --i; if (!ParseFile(args, ref i, noexitSeen)) { break; } } } if (_showHelp) { ShowHelp(); } if (_showBanner && !_showHelp) { DisplayBanner(); if (UpdatesNotification.CanNotifyUpdates) { UpdatesNotification.ShowUpdateNotification(_hostUI); } } Dbg.Assert( ((_exitCode == ConsoleHost.ExitCodeBadCommandLineParameter) && _abortStartup) || (_exitCode == ConsoleHost.ExitCodeSuccess), "if exit code is failure, then abortstartup should be true"); }