From 18288192a681a1ab9cec84dbb7fdf4981e241994 Mon Sep 17 00:00:00 2001 From: "gitauto-ai[bot]" <161652217+gitauto-ai[bot]@users.noreply.github.com> Date: Mon, 18 Nov 2024 00:12:09 +0000 Subject: [PATCH] Update src/dotnet_mytool/Program.cs --- src/dotnet_mytool/Program.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/dotnet_mytool/Program.cs b/src/dotnet_mytool/Program.cs index c0764b5..ed47325 100644 --- a/src/dotnet_mytool/Program.cs +++ b/src/dotnet_mytool/Program.cs @@ -14,6 +14,24 @@ static int Main(string[] args) new Argument("variable", "The environment variable in the format VAR_NAME=value"), new Option("--target", () => "User", "The target scope: User or Machine") }; + setEnvCommand.Handler = CommandHandler.Create((variable, target) => + { + var parts = variable.Split('='); + if (parts.Length != 2) + + static void SetEnvironmentVariable(string variable, string value, string target) + { + EnvironmentVariableTarget envTarget = target.Equals("Machine", StringComparison.OrdinalIgnoreCase) + ? EnvironmentVariableTarget.Machine + : EnvironmentVariableTarget.User; + + Environment.SetEnvironmentVariable(variable, value, envTarget); + Console.WriteLine($"Environment variable {variable} set to {value} for {target}."); + } + { + Console.WriteLine("Invalid format. Use VAR_NAME=value."); + return; + } rootCommand.AddCommand(setEnvCommand);