8000 Implement -version parameter in console host (address part of https:/… · PowerShell/PowerShell@9e27f4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e27f4a

Browse files
JamesWTruherdaxian-dbw
authored andcommitted
Implement -version parameter in console host (address part of https:/… (#3115)
* Implement -version parameter in console host (address part of #1084) This does not support providing a specific version to run, but like most other *nix commands, -version will now return the version of the PowerShell Engine. 'powershell' is prepended to the output to match other *nix commands. We are using gitcommitid which includes more info about the build.
1 parent 16ff197 commit 9e27f4a

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,14 @@ internal bool ServerMode
316316
}
317317
}
318318

319+
internal bool ShowVersion
320+
{
321+
get
322+
{
323+
return _showVersion;
324+
}
325+
}
326+
319327
internal Serialization.DataFormat OutputFormat
320328
{
321329
get
@@ -500,7 +508,6 @@ private void ParseHelper(string[] args)
500508

501509
// chop off the first character so that we're agnostic wrt specifying / or -
502510
// in front of the switch name.
503-
504511
switchKey = switchKey.Substring(1);
505512

506513
// chop off the second dash so we're agnostic wrt specifying - or --
@@ -509,6 +516,18 @@ private void ParseHelper(string[] args)
509516
switchKey = switchKey.Substring(1);
510517
}
511518

519+
// If version is in the commandline, don't continue to look at any other parameters
520+
if (MatchSwitch(switchKey, "version", "v"))
521+
{
522+
_showVersion = true;
523+
_showBanner = false;
524+
_noInteractive = true;
525+
_skipUserInit = true;
526+
_noExit = false;
527+
break;
528+
}
529+
530+
512531
if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?"))
513532
{
514533
_showHelp = true;
@@ -1091,6 +1110,7 @@ private bool CollectArgs(string[] args, ref int i)
10911110
private bool _serverMode;
10921111
private bool _namedPipeServerMode;
10931112
private bool _sshServerMode;
1113+
private bool _showVersion;
10941114
private string _configurationName;
10951115
private PSHostUserInterface _hostUI;
10961116
private bool _showHelp;

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ internal static int Start(
190190

191191
s_cpp.Parse(tempArgs);
192192

193+
if (s_cpp.ShowVersion)
194+
{
195+
// Alternatively, we could call s_theConsoleHost.UI.WriteLine(s_theConsoleHost.Version.ToString());
196+
// or start up the engine and retrieve the information via $psversiontable.GitCommitId
197+
// but this returns the semantic version and avoids executing a script
198+
s_theConsoleHost.UI.WriteLine("powershell " + PSVersionInfo.GitCommitId);
199+
return 0;
200+
}
201+
193202
// Servermode parameter validation check.
194203
if ((s_cpp.ServerMode && s_cpp.NamedPipeServerMode) || (s_cpp.ServerMode && s_cpp.SocketServerMode) || (s_cpp.NamedPipeServerMode && s_cpp.SocketServerMode))
195204
{

src/System.Management.Automation/engine/PSVersionInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ internal static Version PSVersion
153153
}
154154
}
155155

156+
internal static string GitCommitId
157+
{
158+
get
159+
{
160+
return (string)GetPSVersionTable()["GitCommitId"];
161+
}
162+
}
163+
156164
internal static Version CLRVersion
157165
{
158166
get

test/powershell/Host/ConsoleHost.Tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
164164
$actual | Should Be $expected
165165
}
166166

167+
It "-Version should return the engine version" {
168+
$currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString()
169+
$observed = & $powershell -version
170+
$observed | should be $currentVersion
171+
}
172+
173+
It "-Version should ignore other parameters" {
174+
$currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString()
175+
$observed = & $powershell -version -command get-date
176+
# no extraneous output
177+
$observed | should be $currentVersion
178+
}
167179
}
168180

169181
Context "Pipe to/from powershell" {

0 commit comments

Comments
 (0)
0