diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index c5b4aaacf1f..0df9a6882cd 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -313,7 +313,12 @@ MixEnvironment::MixEnvironment() .description = "Unset the environment variable *name*.", .category = environmentVariablesCategory, .labels = {"name"}, - .handler = {[&](std::string s) { unsetVars.insert(s); }}, + .handler = {[&](std::string name) { + if (setVars.contains(name)) + throw UsageError("Cannot unset environment variable '%s' that is set with '%s'", name, "--set-env-var"); + + unsetVars.insert(name); + }}, }); addFlag({ @@ -327,9 +332,14 @@ MixEnvironment::MixEnvironment() .category = environmentVariablesCategory, .labels = {"name", "value"}, .handler = {[&](std::string name, std::string value) { + if (unsetVars.contains(name)) + throw UsageError( + "Cannot set environment variable '%s' that is unset with '%s'", name, "--unset-env-var"); + if (setVars.contains(name)) throw UsageError( "Duplicate definition of environment variable '%s' with '%s' is ambiguous", name, "--set-env-var"); + setVars.insert_or_assign(name, value); }}, });