设置
设置控制解释和执行。每个设置最多可以指定一次,可以出现在 justfile
的任何地方。
例如:
set shell := ["zsh", "-cu"]
foo:
# this line will be run as `zsh -cu 'ls **/*.txt'`
ls **/*.txt
设置一览表
名称 | 值 | 默认 | 描述 |
---|---|---|---|
allow-duplicate-recipes | boolean | False | 允许在 justfile 后面出现的配方覆盖之前的同名配方 |
dotenv-load | boolean | False | 如果有.env 环境变量文件的话,则将其加载 |
export | boolean | False | 将所有变量导出为环境变量 |
fallback | boolean | False | 如果命令行中的第一个配方没有找到,则在父目录中搜索 justfile |
ignore-comments | boolean | False | 忽略以# 开头的配方行 |
positional-arguments | boolean | False | 传递位置参数 |
shell | [COMMAND, ARGS…] | - | 设置用于调用配方和评估反引号内包裹内容的命令 |
tempdir | string | - | 在 tempdir 位置创建临时目录,而不是系统默认的临时目录 |
windows-powershell | boolean | False | 在 Windows 上使用 PowerShell 作为默认 Shell(废弃,建议使用 windows-shell ) |
windows-shell | [COMMAND, ARGS…] | - | 设置用于调用配方和评估反引号内包裹内容的命令 |
Bool 类型设置可以写成:
set NAME
这就相当于:
set NAME := true
允许重复的配方
如果 allow-duplicate-recipes
被设置为 true
,那么定义多个同名的配方就不会出错,而会使用最后的定义。默认为 false
。
set allow-duplicate-recipes
@foo:
echo foo
@foo:
echo bar
$ just foo
bar
环境变量加载
如果将 dotenv-load
设置为 true
,并且存在 .env
文件,则该环境配置文件将被加载。默认为 false
。
导出
export
设置使所有 just
变量作为环境变量被导出。默认值为 false
。
set export
a := "hello"
@foo b:
echo $a
echo $b
$ just foo goodbye
hello
goodbye
位置参数
如果 positional-arguments
为 true
,配方参数将作为位置参数传递给命令。对于行式配方,参数 $0
将是配方的名称。
例如,运行这个配方:
set positional-arguments
@foo bar:
echo $0
echo $1
将产生以下输出:
$ just foo hello
foo
hello
当使用 sh
兼容的 Shell,如 bash
或 zsh
时,$@
会展开为传给配方的位置参数,从1开始。当在双引号内使用 "$@"
时,包括空白的参数将被传递,就像它们是双引号一样。也就是说,"$@"
相当于 "$1" "$2"
……当没有位置参数时,"$@"
和 $@
将展开为空(即,它们被删除)。
这个例子的配方将逐行打印参数:
set positional-arguments
@test *args='':
bash -c 'while (( "$#" )); do echo - $1; shift; done' -- "$@"
用 两个 参数运行:
$ just test foo "bar baz"
- foo
- bar baz
Shell
shell
设置控制用于调用执行配方代码行和反引号内指令的命令。Shebang 配方不受影响。
# use python3 to execute recipe lines and backticks
set shell := ["python3", "-c"]
# use print to capture result of evaluation
foos := `print("foo" * 4)`
foo:
print("Snake snake snake snake.")
print("{{foos}}")
just
把要执行的命令作为一个参数进行传递。许多 Shell 需要一个额外的标志,通常是 -c
,以使它们评估执行第一个参数。
Windows Shell
just
在 Windows 上默认使用 sh
。要在 Windows 上使用不同的 Shell,请使用windows-shell
:
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
hello:
Write-Host "Hello, world!"
参考 powershell.just ,了解在所有平台上使用 PowerShell 的 justfile。
Windows PowerShell
set windows-powershell
使用遗留的 powershell.exe
二进制文件,不再推荐。请参阅上面的 windows-shell
设置,以通过更灵活的方式来控制在 Windows 上使用哪个 Shell。
just
在 Windows 上默认使用 sh
。要使用 powershell.exe
作为替代,请将 windows-powershell
设置为 true
。
set windows-powershell := true
hello:
Write-Host "Hello, world!"
Python 3
set shell := ["python3", "-c"]
Bash
set shell := ["bash", "-uc"]
Z Shell
set shell := ["zsh", "-uc"]
Fish
set shell := ["fish", "-c"]
Nushell
set shell := ["nu", "-c"]
如果你想设置默认的表格显示模式为 light
:
set shell := ['nu', '-m', 'light', '-c']
Nushell 使用 Rust 开发并且具备良好的跨平台能力,支持 Windows / macOS 和各种 Linux 发行版