模块

与其他许多编程语言类似,Nushell 也有模块,可以让你将自定义的命令导入到当前作用域中。 然而,由于 Nushell 也是一个 Shell,模块还允许你导入环境变量,可以用来方便地激活/停用各种环境配置。

注意! 目前对模块的实现是相当基本的,并将在未来进一步扩展。例如,目前还不能从一个模块中导入另一个模块。

基础知识

一个简单的模块可以像这样定义:

1. > module greetings {
2. export def hello [name: string] {
3. $"hello ($name)!"
4. }

6. export def hi [where: string] {
7. $"hi ($where)!"
8. }
9. }

或者在一个与你要创建的模块名相同的文件中:

1. # greetings.nu

3. export def hello [name: string] {
4. $"hello ($name)!"
5. }

7. export def hi [where: string] {
8. $"hi ($where)!"
9. }

我们在 greetings 模块中定义了 hello 和 hi 两个自定义命令。export关键字使得以后可以从模块中导入该命令。

与def类似,也可以用export关键字标记def-env(你可以在环境章节中了解更多关于def-env的信息)。

使用模块

模块本身并不做任何事情,要使用模块导出的定义,我们需要use它:

1. > use greetings

3. > greetings hello "world"
4. hello world!

6. > greetings hi "there"
7. hi there!

hello和hi命令现在可以通过greetings前缀被调用。

导入符号

一般来说,use关键词后面的任何内容都会形成一个导入模式,它控制着符号的导入方式。 导入模式可以是以下的一种:

use greetings

导入所有以模块名称为前缀的符号(我们在前面的例子中看到了这个)。

use greetings hello

hello符号将被直接导入,没有任何前缀。

use greetings [ hello, hi ]

直接导入多个符号,没有任何前缀。

use greetings *

你也可以使用模块名称和* glob 来直接导入所有的名称,且不需要任何前缀。

模块文件

Nushell 让你隐含地把一个源文件当作一个模块。 让我们先把模块定义的主体保存到一个文件中:

1. # greetings.nu

3. export def hello [name: string] {
4. $"hello ($name)!"
5. }

7. export def hi [where: string] {
8. $"hi ($where)!"
9. }

现在,你可以直接在文件上调用use:

1. > use greetings.nu

3. > greetings hello "world"
4. hello world!

6. > greetings hi "there"
7. hi there!

Nushell 会自动从文件名(”greetings”,没有”.nu”扩展名)推断出模块的名称。所以你可以通过文件名而不是模块名配合使用上述任何导入模式来完成导入。

本地自定义命令

任何在模块中定义的自定义命令,如果没有export关键字,将只在该模块的作用域内工作:

1. # greetings.nu

3. export def hello [name: string] {
4. greetings-helper "hello" "world"
5. }

7. export def hi [where: string] {
8. greetings-helper "hi" "there"
9. }

11. def greetings-helper [greeting: string, subject: string] {
12. $"($greeting) ($subject)!"
13. }

然后,在 Nushell 里我们可以从 “greetings.nu” 中导入所有定义:

1. > use greetings.nu *

3. > hello "world"
4. hello world!

6. > hi "there"
7. hi there!

9. > greetings-helper "foo" "bar"  # fails because 'greetings-helper' is not exported

环境变量

到目前为止,我们只是用模块来导入自定义命令,用同样的方法导出环境变量也是可能的。 其语法与你可能习惯的let-env或load-env等命令略有不同:

1. # greetings.nu

3. export env MYNAME { "Arthur, King of the Britons" }

5. export def hello [name: string] {
6. $"hello ($name)"
7. }

use 的工作方式与自定义命令相同:

1. > use greetings.nu

3. > $env."greetings MYNAME"
4. Arthur, King of the Britons

6. > greetings hello $env."greetings MYNAME"
7. hello Arthur, King of the Britons!

你可能注意到我们没有直接给MYNAME赋值,相反,我们给了它一个代码块({ ...}),它在我们每次调用use时都会被执行。例如,我们可以用random命令来演示这一点:

1. > module roll { export env ROLL { random dice | into string } }

3. > use roll ROLL

5. > $env.ROLL
6. 4

8. > $env.ROLL
9. 4

11. > use roll ROLL

13. > $env.ROLL
14. 6

16. > $env.ROLL
17. 6

导出符号

如上所述,你可以从模块中导出定义和环境变量。这可以让你更容易地将相关的定义分组,并导出你想公开的定义。

你还可以导出别名和外部声明(extern),并在需要时才使用这些功能。导出外部声明也让你有能力隐藏模块中的自定义自动补全命令,这样它们就不必成为全局命名空间的一部分。

下面是所有你可以导出的列表:

  • export def - 导出一个自定义命令
  • export def-env - 导出一个自定义环境命令
  • export env - 导出一个环境变量
  • export alias - 导出一个别名
  • export extern - 导出一个已知外部命令的定义

隐藏

任何自定义命令、别名或环境变量, 无论是否从模块中导入, 都可以被 “隐藏”, 以恢复之前的定义。 (注意,现在还不能从模块中导出别名,但它们仍然可以被隐藏。) 我们用hide命令来实现隐藏:

1. > def foo [] { "foo" }

3. > foo
4. foo

6. > hide foo

8. > foo  # error! command not found!

hide命令也接受导入模式,就像use那样。不过,导入模式的解释略有不同。它可以是下面中的一种:

hide foo 或者 hide greetings

  • 如果该名称是一个自定义的命令或环境变量,则直接隐藏它。否则:
  • 如果名字是一个模块的名称,则隐藏所有以模块名称为前缀的导出。

hide greetings hello

  • 隐藏带前缀的命令或环境变量

hide greetings [hello, hi]

  • 隐藏带前缀的若干个命令或环境变量

hide greetings *

  • 隐藏模块的所有的导出,不含前缀

让我们看几个例子。前面已经看到了直接隐藏一个自定义命令的例子,现在让我们试试环境变量:

1. > let-env FOO = "FOO"

3. > $env.FOO
4. FOO

6. > hide FOO

8. > $env.FOO  # error! environment variable not found!

第一种情况也适用于从一个模块导入的命令/环境变量(使用上面定义的 “greetings.nu” 文件):

1. > use greetings.nu *

3. > $env.MYNAME
4. Arthur, King of the Britons

6. > hello "world"
7. hello world!

9. > hide MYNAME

11. > $env.MYNAME  # error! environment variable not found!

13. > hide hello

15. > hello "world" # error! command not found!

最后,当名称为模块名时(假设是之前的greetings模块):

1. > use greetings.nu

3. > $env."greetings MYNAME"
4. Arthur, King of the Britons

6. > greetings hello "world"
7. hello world!

9. > hide greetings

11. > $env."greetings MYNAME"  # error! environment variable not found!

13. > greetings hello "world" # error! command not found!