You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/modules.md
+12-8Lines changed: 12 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,6 +186,8 @@ The `main` is exported only when
186
186
187
187
Importing definitions selectively (`use greetings.nu hello` or `use greetings.nu [hello hi]`) does not define the `greetings` command from `main`. You can, however, selectively import `main` using `use greetings main` (or `[main]`) which defines _only_ the `greetings` command without pulling in `hello` or `hi`.
188
188
189
+
Additionally, `main` has special behavior if used in a script file, regardless of whether it is exported or not. See the [section on scripts](scripts.html#parameterizing-scripts) for more details.
190
+
189
191
Apart from commands (`def`, `def --env`), known externals (`extern`) can also be named `main`.
190
192
191
193
## Submodules and Subcommands
@@ -197,8 +199,7 @@ Submodules are modules inside modules. They are automatically created when you c
197
199
198
200
The difference is that `export module some-module`_only_ adds the module as a submodule, while `export use some-module`_also_ re-exports the submodule's definitions. Since definitions of submodules are available when importing from a module, `export use some-module` is typically redundant, unless you want to re-export its definitions without the namespace prefix.
199
201
200
-
> **Note**
201
-
> `module` without `export` defines only a local module, it does not export a submodule.
202
+
> **Note** > `module` without `export` defines only a local module, it does not export a submodule.
202
203
203
204
Let's illustrate this with an example. Assume three files:
204
205
@@ -425,10 +426,11 @@ A common pattern in traditional shells is dumping and auto-sourcing files from a
425
426
Here we'll create a simple completion module with a submodule dedicated to some Git completions:
5. Add the parent of the `completions` directory to your NU_LIB_DIRS inside `env.nu`
458
+
455
459
```nu
456
460
$env.NU_LIB_DIRS = [
457
461
...
458
462
$nu.default-config-dir
459
463
]
460
464
```
465
+
461
466
6. import the completions to Nushell in your `config.nu`
462
-
`use completions *`
463
-
Now you've set up a directory where you can put your completion files and you should have some Git completions the next time you start Nushell
467
+
`use completions *`
468
+
Now you've set up a directory where you can put your completion files and you should have some Git completions the next time you start Nushell
464
469
465
470
> **Note**
466
471
> This will use the file name (in our example `git` from `git.nu`) as the module name. This means some completions might not work if the definition has the base command in its name.
@@ -515,5 +520,4 @@ It can be one of the following:
515
520
516
521
- Hides all the module's exports, without the prefix
517
522
518
-
> **Note**
519
-
> `hide` is not a supported keyword at the root of a module (unlike `def` etc.)
523
+
> **Note** > `hide` is not a supported keyword at the root of a module (unlike `def` etc.)
Copy file name to clipboardExpand all lines: book/scripts.md
+37-6Lines changed: 37 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ greet "world"
25
25
26
26
A script file defines the definitions for custom commands as well as the main script itself, which will run after the custom commands are defined.
27
27
28
-
In the above, first `greet` is defined by the Nushell interpreter. This allows us to later call this definition. We could have written the above as:
28
+
In the above example, first `greet` is defined by the Nushell interpreter. This allows us to later call this definition. We could have written the above as:
29
29
30
30
```nu
31
31
greet "world"
@@ -75,9 +75,9 @@ def main [x: int] {
75
75
76
76
## Argument Type Interpretation
77
77
78
-
By default, arguments provided to a script are interpreted with the type `Type::Any`, implying that they are not constrained to a specific data type and can be dynamically interpreted as fitting any of the available data types during script execution.
78
+
By default, arguments provided to a script are interpreted with the type `Type::Any`, implying that they are not constrained to a specific data type and can be dynamically interpreted as fitting any of the available data types during script execution.
79
79
80
-
In the previous example, `main [x: int]` denotes that the argument x should possess an integer data type. However, if arguments are not explicitly typed, they will be parsed according to their apparent data type.
80
+
In the previous example, `main [x: int]` denotes that the argument x should possess an integer data type. However, if arguments are not explicitly typed, they will be parsed according to their apparent data type.
81
81
82
82
For example:
83
83
@@ -103,7 +103,7 @@ Hello string +1
103
103
104
104
## Subcommands
105
105
106
-
A scriptcan have multiple sub-commands like `run`, `build`, etc. which allows to execute a specific main sub-function. The important part is to expose them correctly with `def main [] {}`. See more details in the [Custom Command](custom_commands.html#sub-commands) section.
106
+
A script's `main` command can have multiple [subcommands](custom_commands.html#subcommands) like `run`, `build`, etc. which allows executing a specific sub-function from the commandline.
107
107
108
108
For example:
109
109
@@ -117,17 +117,46 @@ def "main build" [] {
117
117
print "building"
118
118
}
119
119
120
-
# important for the command to be exposed to the outside
121
-
def main [] {}
120
+
def main [] {
121
+
print "hello from myscript!"
122
+
}
122
123
```
123
124
124
125
```nu
126
+
> nu myscript.nu
127
+
hello from myscript!
125
128
> nu myscript.nu build
126
129
building
127
130
> nu myscript.nu run
128
131
running
129
132
```
130
133
134
+
[Unlike modules](modules.html#main), `main` does _not_ need to exported in order to be visible. In the above example, our `main` command is not `export def`, however it was still executed when running `nu myscript.nu`. If we had used myscript as a module by running `use myscript.nu`, rather than running `myscript.nu` as a script, trying to execute the `myscript` command would not work since `myscript` is not exported.
135
+
136
+
It is important to note that you must define a `main` command in order for subcommands of `main` to be correctly exposed. For example, if we had just defined the `run` and `build` subcommands, they wouldn't be accessible when running the script:
137
+
138
+
```nu
139
+
# myscript.nu
140
+
def "main run" [] {
141
+
print "running"
142
+
}
143
+
144
+
def "main build" [] {
145
+
print "building"
146
+
}
147
+
```
148
+
149
+
```nu
150
+
> nu myscript.nu build
151
+
> nu myscript.nu run
152
+
```
153
+
154
+
This is a limitation of the way scripts are currently processed. If your script only has subcommands, you can add an empty `main` to expose the subcommands, like so:
155
+
156
+
```nu
157
+
def main [] {}
158
+
```
159
+
131
160
## Shebangs (`#!`)
132
161
133
162
On Linux and macOS you can optionally use a [shebang](<https://en.wikipedia.org/wiki/Shebang_(Unix)>) to tell the OS that a file should be interpreted by Nu. For example, with the following in a file named `myscript`:
@@ -141,7 +170,9 @@ On Linux and macOS you can optionally use a [shebang](<https://en.wikipedia.org/
141
170
> ./myscript
142
171
Hello World!
143
172
```
173
+
144
174
For script to have access to standard input, `nu` should be invoked with `--stdin` flag:
0 commit comments