Improve new CLI UX by supporting short -E flag for --expr

Change-Id: I55881c846da8416a92a14deedfa5bbbf09a122fb
This commit is contained in:
Lunaphied 2024-03-21 21:48:34 -06:00
parent a7161b6c0f
commit 185ecf1f45
4 changed files with 18 additions and 2 deletions

View file

@ -0,0 +1,14 @@
---
synopsis: reintroduce shortened `-E` form for `--expr` to new CLI
# prs: cl 605
---
In the past, it was possible to supply a shorter `-E` flag instead of fully
specifying `--expr` every time you wished to provide an expression that would
be evaluated to produce the given command's input. This was retained for the
`--file` flag when the new CLI utilities were written with `-f`, but `-E` was
dropped.
We now restore the `-E` short form for better UX. This is most useful for
`nix eval` but most any command that takes an Installable argument should benefit
from it as well.

View file

@ -187,6 +187,7 @@ SourceExprCommand::SourceExprCommand()
addFlag({ addFlag({
.longName = "expr", .longName = "expr",
.shortName = 'E',
.description = "Interpret [*installables*](@docroot@/command-ref/new-cli/nix.md#installables) as attribute paths relative to the Nix expression *expr*.", .description = "Interpret [*installables*](@docroot@/command-ref/new-cli/nix.md#installables) as attribute paths relative to the Nix expression *expr*.",
.category = installablesCategory, .category = installablesCategory,
.labels = {"expr"}, .labels = {"expr"},

View file

@ -8,10 +8,10 @@ R""(
# nix eval --expr '1 + 2' # nix eval --expr '1 + 2'
``` ```
* Evaluate a Nix expression to JSON: * Evaluate a Nix expression to JSON using the short-form expression flag:
```console ```console
# nix eval --json --expr '{ x = 1; }' # nix eval --json -E '{ x = 1; }'
{"x":1} {"x":1}
``` ```

View file

@ -12,6 +12,7 @@ EOF
[[ $testStdinHeredoc == '{ bar = 4; foo = 4; }' ]] [[ $testStdinHeredoc == '{ bar = 4; foo = 4; }' ]]
nix eval --expr 'assert 1 + 2 == 3; true' nix eval --expr 'assert 1 + 2 == 3; true'
nix eval -E 'assert 1 + 2 == 3; true'
[[ $(nix eval int -f "./eval.nix") == 123 ]] [[ $(nix eval int -f "./eval.nix") == 123 ]]
[[ $(nix eval str -f "./eval.nix") == '"foo"' ]] [[ $(nix eval str -f "./eval.nix") == '"foo"' ]]