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);