Add --env and --wrapped flags to def (#10566)

This commit is contained in:
Jakub Žádník
2023-10-02 21:13:31 +03:00
committed by GitHub
parent 0d367af24a
commit eb6870cab5
20 changed files with 385 additions and 191 deletions

View File

@ -117,7 +117,7 @@ is described below:
a main command as well.
Use `export def` to make these names public to your users.
* If your command is updating environment variables, you must use
`export def-env` (instead of `export def`) to define the subcommand,
`export def --env` (instead of `export def`) to define the subcommand,
`export-env {}` to initialize the environment variables and `let-env` to
update them. For an example of a custom command which modifies
environment variables, see: `./crates/nu-std/lib/dirs.nu`.

View File

@ -23,7 +23,7 @@ export-env {
# Add one or more directories to the list.
# PWD becomes first of the newly added directories.
export def-env add [
export def --env add [
...paths: string # directory or directories to add to working list
] {
mut abspaths = []
@ -45,7 +45,7 @@ export def-env add [
export alias enter = add
# Advance to the next directory in the list or wrap to beginning.
export def-env next [
export def --env next [
N:int = 1 # number of positions to move.
] {
_fetch $N
@ -54,7 +54,7 @@ export def-env next [
export alias n = next
# Back up to the previous directory or wrap to the end.
export def-env prev [
export def --env prev [
N:int = 1 # number of positions to move.
] {
_fetch (-1 * $N)
@ -64,7 +64,7 @@ export alias p = prev
# Drop the current directory from the list, if it's not the only one.
# PWD becomes the next working directory
export def-env drop [] {
export def --env drop [] {
if ($env.DIRS_LIST | length) > 1 {
$env.DIRS_LIST = ($env.DIRS_LIST | reject $env.DIRS_POSITION)
if ($env.DIRS_POSITION >= ($env.DIRS_LIST | length)) {$env.DIRS_POSITION = 0}
@ -78,7 +78,7 @@ export def-env drop [] {
export alias dexit = drop
# Display current working directories.
export def-env show [] {
export def --env show [] {
mut out = []
for $p in ($env.DIRS_LIST | enumerate) {
let is_act_slot = $p.index == $env.DIRS_POSITION
@ -95,7 +95,7 @@ export def-env show [] {
export alias shells = show
export def-env goto [shell?: int] {
export def --env goto [shell?: int] {
if $shell == null {
return (show)
}
@ -119,7 +119,7 @@ export def-env goto [shell?: int] {
export alias g = goto
# fetch item helper
def-env _fetch [
def --env _fetch [
offset: int, # signed change to position
--forget_current # true to skip saving PWD
--always_cd # true to always cd

View File

@ -34,7 +34,7 @@ use log.nu
# ```nushell
# >_ std path add {linux: "foo", windows: "bar", darwin: "baz"}
# ```
export def-env "path add" [
export def --env "path add" [
--ret (-r) # return $env.PATH, useful in pipelines to avoid scoping.
--append (-a) # append to $env.PATH instead of prepending to.
...paths # the paths to add to $env.PATH.

View File

@ -3,8 +3,8 @@ use std assert
use std log
# A couple of nuances to understand when testing module that exports environment:
# Each 'use' for that module in the test script will execute the def-env block.
# PWD at the time of the `use` will be what the export def-env block will see.
# Each 'use' for that module in the test script will execute the def --env block.
# PWD at the time of the `use` will be what the export def --env block will see.
#[before-each]
def before-each [] {
@ -43,11 +43,11 @@ def dirs_command [] {
# must capture value of $in before executing `use`s
let $c = $in
# must set PWD *before* doing `use` that will run the def-env block in dirs module.
# must set PWD *before* doing `use` that will run the def --env block in dirs module.
cd $c.base_path
# must execute these uses for the UOT commands *after* the test and *not* just put them at top of test module.
# the def-env gets messed up
# the def --env gets messed up
use std dirs
# Stack: [BASE]
@ -92,7 +92,7 @@ def dirs_command [] {
def dirs_next [] {
# must capture value of $in before executing `use`s
let $c = $in
# must set PWD *before* doing `use` that will run the def-env block in dirs module.
# must set PWD *before* doing `use` that will run the def --env block in dirs module.
cd $c.base_path
assert equal $env.PWD $c.base_path "test setup"
@ -114,7 +114,7 @@ def dirs_next [] {
def dirs_cd [] {
# must capture value of $in before executing `use`s
let $c = $in
# must set PWD *before* doing `use` that will run the def-env block in dirs module.
# must set PWD *before* doing `use` that will run the def --env block in dirs module.
cd $c.base_path
use std dirs