Skip to content

Commit

Permalink
add overload for executing new process with arguments and environment…
Browse files Browse the repository at this point in the history
… variables
  • Loading branch information
smdn committed Oct 19, 2022
1 parent 03390ef commit 30e3e96
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/Smdn.Fundamental.Shell/Smdn.OperatingSystem/Shell.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2009 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Smdn.OperatingSystem;
Expand Down Expand Up @@ -47,11 +48,40 @@ public static string Execute(string command)
}

public static int Execute(string command, out string stdout)
=> Execute(command, out stdout, out _);
=> Execute(
command: command,
arguments: null,
environmentVariables: null,
out stdout,
out _
);

public static int Execute(string command, out string stdout, out string stderr)
=> Execute(
command: command,
arguments: null,
environmentVariables: null,
out stdout,
out stderr
);

#nullable enable
public static int Execute(
string command,
IEnumerable<string>? arguments,
IReadOnlyDictionary<string, string>? environmentVariables,
out string stdout,
out string stderr
)
{
var psi = CreateProcessStartInfo(command, string.Empty);
var args = arguments is null ? string.Empty : string.Join(" ", arguments);
var psi = CreateProcessStartInfo(command, args);

if (environmentVariables is not null) {
foreach (var pair in environmentVariables) {
psi.EnvironmentVariables[pair.Key] = pair.Value;
}
}

psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
Expand All @@ -65,5 +95,8 @@ public static int Execute(string command, out string stdout, out string stderr)

return process.ExitCode;
}
#pragma warning disable IDE0241
#nullable restore
#pragma warning restore IDE0241
}
#endif

0 comments on commit 30e3e96

Please sign in to comment.