Add '-WorkingDirectory' parameter to pwsh (#6612)

Add `-WorkingDirectory` parameter to `pwsh` to allow starting in right working directory.
This commit is contained in:
Steve Lee authored and Dongbo Wang committed 2018-04-23 14:27:47 -07:00
1 parent 84a3c9cdc7
commit 942c68e796
5 files changed
+94 -2

No files matched your search

@@ -187,7 +187,8 @@ namespace Microsoft.PowerShell
"executionpolicy",
"command",
"settingsfile",
"help"
"help",
"workingdirectory"
};
internal CommandLineParameterParser(PSHostUserInterface hostUI, string bannerText, string helpText)
@@ -387,6 +388,11 @@ namespace Microsoft.PowerShell
get { return _noInteractive; }
}
internal string WorkingDirectory
{
get { return _workingDirectory; }
}
private void ShowHelp()
{
Dbg.Assert(_helpText != null, "_helpText should not be null");
@@ -767,6 +773,18 @@ namespace Microsoft.PowerShell
_staMode = false;
}
#endif
else if (MatchSwitch(switchKey, "workingdirectory", "wo") || MatchSwitch(switchKey, "wd", "wd"))
{
++i;
if (i >= args.Length)
{
WriteCommandLineError(
CommandLineParameterParserStrings.MissingWorkingDirectoryArgument);
break;
}
_workingDirectory = args[i];
}
else
{
// The first parameter we fail to recognize marks the beginning of the file string.
@@ -1223,6 +1241,7 @@ namespace Microsoft.PowerShell
private Collection<CommandParameter> _collectedArgs = new Collection<CommandParameter>();
private string _file;
private string _executionPolicy;
private string _workingDirectory;
}
} // namespace
@@ -1752,6 +1752,31 @@ namespace Microsoft.PowerShell
TelemetryAPI.ReportStartupTelemetry(this);
#endif
// If working directory was specified, set it
if (s_cpp != null && s_cpp.WorkingDirectory != null)
{
Pipeline tempPipeline = exec.CreatePipeline();
var command = new Command("Set-Location");
command.Parameters.Add("LiteralPath", s_cpp.WorkingDirectory);
tempPipeline.Commands.Add(command);
Exception exception;
if (IsRunningAsync)
{
exec.ExecuteCommandAsyncHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter);
}
else
{
exec.ExecuteCommandHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter);
}
if (exception != null)
{
_lastRunspaceInitializationException = exception;
ReportException(exception, exec);
}
}
// If a file was specified as the argument to run, then run it...
if (s_cpp != null && s_cpp.File != null)
{
@@ -187,7 +187,7 @@
<value>Cannot process the command because -Configuration requires an argument that is a remote endpoint configuration name. Specify this argument and try again.</value>
</data>
<data name="MissingSettingsFileArgument" xml:space="preserve">
<value>Cannot process the command because -SettingsFile requires a file path. Supply a path for the SettingsFile parameter and then try the command again.</value>
<value>Cannot process the command because -SettingsFile requires an argument that is a file path.</value>
</data>
<data name="InvalidSettingsFileArgument" xml:space="preserve">
<value>Processing -SettingsFile '{0}' failed: {1}. Specify a valid path for the -SettingsFile parameter.</value>
@@ -201,4 +201,7 @@
<data name="WindowStyleArgumentNotImplemented" xml:space="preserve">
<value>Parameter -WindowStyle is not implemented on this platform.</value>
</data>
<data name="MissingWorkingDirectoryArgument" xml:space="preserve">
<value>Cannot process the command because -WorkingDirectory requires an argument that is a directory path.</value>
</data>
</root>
@@ -132,6 +132,7 @@ Type 'help' to get help.</value>
[-ExecutionPolicy &lt;ExecutionPolicy&gt;] [-InputFormat {Text | XML}]
[-Interactive] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
[-OutputFormat {Text | XML}] [-Version] [-WindowStyle &lt;style&gt;]
[-WorkingDirectory &lt;directoryPath&gt;]
pwsh[.exe] -h | -Help | -? | /?
@@ -249,6 +250,12 @@ All parameters are case-insensitive.</value>
-WindowStyle | -w
Sets the window style to Normal, Minimized, Maximized or Hidden.
-WorkingDirectory | -wd
Sets the working directory at the start of PowerShell given a valid PowerShell directory path.
Executing `Set-Location -LiteralPath &lt;path&gt;` at startup.
Example: pwsh -WorkingDirectory ~
</value>
</data>
</root>
@@ -561,6 +561,44 @@ foo
}
}
}
Context "-WorkingDirectory parameter" {
BeforeAll {
$folderName = (New-Guid).ToString() + " test";
New-Item -Path ~/$folderName -ItemType Directory
$ExitCodeBadCommandLineParameter = 64
}
AfterAll {
Remove-Item ~/$folderName -Force -ErrorAction SilentlyContinue
}
It "Can set working directory to '<value>'" -TestCases @(
@{ value = "~" ; expectedPath = $((Get-Item ~).FullName) },
@{ value = "~/$folderName"; expectedPath = $((Get-Item ~/$folderName).FullName) },
@{ value = "~\$folderName"; expectedPath = $((Get-Item ~\$folderName).FullName) }
) {
param($value, $expectedPath)
$output = & $powershell -WorkingDirectory "$value" -Command "`$pwd.Path"
$output | Should -BeExactly $expectedPath
}
It "Can use '<parameter>' to set working directory" -TestCases @(
@{ parameter = '-workingdirectory' },
@{ parameter = '-wd' },
@{ parameter = '-wo' }
) {
param($parameter)
$output = & $powershell $parameter ~ -Command "`$pwd.Path"
$output | Should -BeExactly $((Get-Item ~).FullName)
}
It "Error case if -WorkingDirectory isn't given argument as last on command line" {
$output = & $powershell -WorkingDirectory
$LASTEXITCODE | Should -Be $ExitCodeBadCommandLineParameter
$output | Should -Not -BeNullOrEmpty
}
}
}
Describe "WindowStyle argument" -Tag Feature {