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/custom_commands.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -267,10 +267,11 @@ It's common practice in Nushell to separate the words of the command with `-` fo
267
267
::: tip
268
268
Because `def` is a parser keyword, the command name must be known at parse time. This means that command names may not be a variable or constant. For example, the following is _not allowed_:
269
269
270
-
````nu
270
+
```nu
271
271
let name = "foo"
272
272
def $name [] { foo }
273
273
:::
274
+
```
274
275
275
276
### Subcommands
276
277
@@ -280,7 +281,7 @@ You can also define subcommands of commands using a space. For example, if we wa
280
281
def "str mycommand" [] {
281
282
"hello"
282
283
}
283
-
````
284
+
```
284
285
285
286
Now we can call our custom command as if it were a built-in subcommand of [`str`](/commands/docs/str.md):
See also [Naming Sub-Commands](custom_commands.md#subcommands).
245
+
246
+
#### Flags
247
+
248
+
**It's recommended to** use kebab-case for flag names.
249
+
250
+
Correct:
251
+
252
+
```nu
253
+
def greet [name: string, --all-caps] {
254
+
# ...
255
+
}
256
+
```
257
+
258
+
Incorrect:
259
+
260
+
```nu
261
+
def greet [name: string, --all_caps] {
262
+
# ...
263
+
}
264
+
```
265
+
266
+
::: tip
267
+
Notice that the name used to access the flag is accessed by replacing the dash with an underscore in the resulting
268
+
variable name.
269
+
270
+
See [Flags](custom_commands.md#flags).
271
+
:::
272
+
273
+
#### Variables and Command Parameters
274
+
275
+
**It's recommended to** use snake_case for variable names, including command parameters.
276
+
277
+
Correct:
278
+
279
+
```nu
280
+
let user_id = 123
281
+
282
+
def fetch-user [user_id: int] {
283
+
# ...
284
+
}
285
+
```
286
+
287
+
Incorrect:
288
+
289
+
```nu
290
+
let user-id = 123
291
+
let userId = 123
292
+
293
+
def fetch-user [user-id: int] {
294
+
# ...
295
+
}
296
+
```
297
+
298
+
#### Environment Variables
299
+
300
+
**It's recommended to** use SCREAMING_SNAKE_CASE for environment variable names.
301
+
302
+
Correct:
303
+
304
+
```nu
305
+
$env.ENVIRONMENT_CODE = "prod"
306
+
307
+
$env.APP_VERSION = "1.0.0"
308
+
```
309
+
310
+
Incorrect:
311
+
312
+
```nu
313
+
$env.ENVIRONMENT-CODE = "prod"
314
+
315
+
$env.app_version = "1.0.0"
316
+
```
317
+
183
318
## Options and Parameters of Custom Commands
184
319
185
320
-**It's recommended to** keep count of all positional parameters less than or equal to 2, for remaining inputs use options. Assume that command can expect source and destination parameter, like `mv`: source and target file or directory.
0 commit comments