-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Labels
enhancementNew feature or requestNew feature or requestgitautoGitAuto label to trigger the app in a issue.GitAuto label to trigger the app in a issue.good first issueGood for newcomersGood for newcomershacktoberfestParticipation in the Hacktoberfest eventParticipation in the Hacktoberfest eventhelp wantedExtra attention is neededExtra attention is neededπ documentationTasks related to writing or updating documentationTasks related to writing or updating documentationπ medium effortA task that can be completed in a few hoursA task that can be completed in a few hoursπ WIPWork in progressWork in progressπ§ͺ testsTasks related to testingTasks related to testing
Description
Description:
I would like to request a feature that adds a new command to a .NET tool project, allowing users to set environment variables via the command-line interface (CLI). The command should accept two arguments:
- The variable value (required).
- An optional target specifying where the variable should be set (
UserorMachinescope).
The tool should apply the environment variable to the appropriate scope based on the input.
Why is this needed?
This feature would allow users to easily set environment variables through a .NET CLI tool, simplifying environment setup, especially in CI/CD pipelines or automated scripts. Having the ability to specify the target (User or Machine) will give flexibility based on the user's needs.
Suggested Implementation:
- The command should set the environment variable for the current user or machine depending on the target specified.
- If no target is specified, the environment variable should be set for the current user by default.
Example Command Usage:
dotnet mytool set-env MY_VARIABLE=my_value --target UserIf --target is not provided, it will default to User.
Example Code:
using System;
using Microsoft.Win32;
public class EnvVarSetter
{
public static void SetEnvironmentVariable(string variable, string value, string target = "User")
{
EnvironmentVariableTarget envTarget;
if (string.Equals(target, "Machine", StringComparison.OrdinalIgnoreCase))
{
envTarget = EnvironmentVariableTarget.Machine;
}
else
{
envTarget = EnvironmentVariableTarget.User;
}
Environment.SetEnvironmentVariable(variable, value, envTarget);
Console.WriteLine($"Environment variable '{variable}' set to '{value}' for {envTarget}.");
}
}
// Usage example from the CLI
class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: set-env VAR_NAME=value [--target User|Machine]");
return;
}
string[] variableParts = args[0].Split('=');
if (variableParts.Length != 2)
{
Console.WriteLine("Invalid format. Please use VAR_NAME=value.");
return;
}
string variableName = variableParts[0];
string variableValue = variableParts[1];
string target = args.Length > 1 && args[1].StartsWith("--target") ? args[1].Split('=')[1] : "User";
EnvVarSetter.SetEnvironmentVariable(variableName, variableValue, target);
}
}Additional Context:
- This feature will only work on platforms where setting environment variables is allowed (e.g., Windows via
EnvironmentVariableTarget). - The default behavior is to set the environment variable for the current user if no target is provided.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestgitautoGitAuto label to trigger the app in a issue.GitAuto label to trigger the app in a issue.good first issueGood for newcomersGood for newcomershacktoberfestParticipation in the Hacktoberfest eventParticipation in the Hacktoberfest eventhelp wantedExtra attention is neededExtra attention is neededπ documentationTasks related to writing or updating documentationTasks related to writing or updating documentationπ medium effortA task that can be completed in a few hoursA task that can be completed in a few hoursπ WIPWork in progressWork in progressπ§ͺ testsTasks related to testingTasks related to testing
Projects
Status
In Progress