Merge pull request #11142 from detroyejr/fix-alias-flags

Allow flag aliases
This commit is contained in:
Eelco Dolstra 2024-07-22 15:53:41 +02:00 committed by GitHub
commit 40f80e1b5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 0 deletions

View file

@ -304,18 +304,21 @@ template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::s
{ {
args.addFlag({ args.addFlag({
.longName = name, .longName = name,
.aliases = aliases,
.description = "Enable sandboxing.", .description = "Enable sandboxing.",
.category = category, .category = category,
.handler = {[this]() { override(smEnabled); }} .handler = {[this]() { override(smEnabled); }}
}); });
args.addFlag({ args.addFlag({
.longName = "no-" + name, .longName = "no-" + name,
.aliases = aliases,
.description = "Disable sandboxing.", .description = "Disable sandboxing.",
.category = category, .category = category,
.handler = {[this]() { override(smDisabled); }} .handler = {[this]() { override(smDisabled); }}
}); });
args.addFlag({ args.addFlag({
.longName = "relaxed-" + name, .longName = "relaxed-" + name,
.aliases = aliases,
.description = "Enable sandboxing, but allow builds to disable it.", .description = "Enable sandboxing, but allow builds to disable it.",
.category = category, .category = category,
.handler = {[this]() { override(smRelaxed); }} .handler = {[this]() { override(smRelaxed); }}

View file

@ -81,6 +81,7 @@ void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
{ {
args.addFlag({ args.addFlag({
.longName = name, .longName = name,
.aliases = aliases,
.description = fmt("Set the `%s` setting.", name), .description = fmt("Set the `%s` setting.", name),
.category = category, .category = category,
.labels = {"value"}, .labels = {"value"},
@ -91,6 +92,7 @@ void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
if (isAppendable()) if (isAppendable())
args.addFlag({ args.addFlag({
.longName = "extra-" + name, .longName = "extra-" + name,
.aliases = aliases,
.description = fmt("Append to the `%s` setting.", name), .description = fmt("Append to the `%s` setting.", name),
.category = category, .category = category,
.labels = {"value"}, .labels = {"value"},

View file

@ -292,6 +292,7 @@ template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string &
{ {
args.addFlag({ args.addFlag({
.longName = name, .longName = name,
.aliases = aliases,
.description = fmt("Enable the `%s` setting.", name), .description = fmt("Enable the `%s` setting.", name),
.category = category, .category = category,
.handler = {[this] { override(true); }}, .handler = {[this] { override(true); }},
@ -299,6 +300,7 @@ template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string &
}); });
args.addFlag({ args.addFlag({
.longName = "no-" + name, .longName = "no-" + name,
.aliases = aliases,
.description = fmt("Disable the `%s` setting.", name), .description = fmt("Disable the `%s` setting.", name),
.category = category, .category = category,
.handler = {[this] { override(false); }}, .handler = {[this] { override(false); }},

View file

@ -140,6 +140,18 @@ nix build --impure -f multiple-outputs.nix --json e --no-link | jq --exit-status
(.outputs | keys == ["a_a", "b"])) (.outputs | keys == ["a_a", "b"]))
' '
# Make sure that the 3 types of aliases work
# BaseSettings<T>, BaseSettings<bool>, and BaseSettings<SandboxMode>.
nix build --impure -f multiple-outputs.nix --json e --no-link \
--build-max-jobs 3 \
--gc-keep-outputs \
--build-use-sandbox | \
jq --exit-status '
(.[0] |
(.drvPath | match(".*multiple-outputs-e.drv")) and
(.outputs | keys == ["a_a", "b"]))
'
# Make sure that `--stdin` works and does not apply any defaults # Make sure that `--stdin` works and does not apply any defaults
printf "" | nix build --no-link --stdin --json | jq --exit-status '. == []' printf "" | nix build --no-link --stdin --json | jq --exit-status '. == []'
printf "%s\n" "$drv^*" | nix build --no-link --stdin --json | jq --exit-status '.[0]|has("drvPath")' printf "%s\n" "$drv^*" | nix build --no-link --stdin --json | jq --exit-status '.[0]|has("drvPath")'

View file

@ -58,3 +58,7 @@ fi
# Test that unknown settings are warned about # Test that unknown settings are warned about
out="$(expectStderr 0 nix eval --option foobar baz --expr '""' --raw)" out="$(expectStderr 0 nix eval --option foobar baz --expr '""' --raw)"
[[ "$(echo "$out" | grep foobar | wc -l)" = 1 ]] [[ "$(echo "$out" | grep foobar | wc -l)" = 1 ]]
# Test flag alias
out="$(nix eval --expr '{}' --build-cores 1)"
[[ "$(echo "$out" | wc -l)" = 1 ]]