Skip to content

Commit cebb008

Browse files
authored
Added candidate naming convention to the style guide (#1601)
1 parent ab3d835 commit cebb008

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

book/custom_commands.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ It's common practice in Nushell to separate the words of the command with `-` fo
267267
::: tip
268268
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_:
269269

270-
````nu
270+
```nu
271271
let name = "foo"
272272
def $name [] { foo }
273273
:::
274+
```
274275

275276
### Subcommands
276277

@@ -280,7 +281,7 @@ You can also define subcommands of commands using a space. For example, if we wa
280281
def "str mycommand" [] {
281282
"hello"
282283
}
283-
````
284+
```
284285

285286
Now we can call our custom command as if it were a built-in subcommand of [`str`](/commands/docs/str.md):
286287

book/style_guide.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,141 @@ let selectedProfile = (
180180
})
181181
```
182182

183+
## Naming Convention
184+
185+
### Abbreviations and Acronyms
186+
187+
**It's recommended** to use full concise words over abbreviations and acronyms, unless they are well-known and/or
188+
commonly used.
189+
190+
Correct:
191+
192+
```nu
193+
query-user --id 123
194+
195+
$user.name | str downcase
196+
```
197+
198+
Incorrect:
199+
200+
```nu
201+
qry-usr --id 123
202+
203+
$user.name | string downcase
204+
```
205+
206+
### Case
207+
208+
#### Commands
209+
210+
**It's recommended to** use kebab-case for command names with multiple words.
211+
212+
Correct:
213+
214+
```nu
215+
fetch-user --id 123
216+
```
217+
218+
Incorrect:
219+
220+
```nu
221+
fetch_user --id 123
222+
fetchUser --id 123
223+
```
224+
225+
See also [Naming Commands](custom_commands.md#naming-commands).
226+
227+
#### Sub-Commands
228+
229+
Sub commands are commands that are logically grouped under a parent command and separated by a space.
230+
**It's recommended to** use kebab-case for the sub-command name.
231+
232+
Correct:
233+
234+
```nu
235+
date now
236+
237+
date list-timezone
238+
239+
def "login basic-auth" [username: string password: string] {
240+
# ...
241+
}
242+
```
243+
244+
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+
183318
## Options and Parameters of Custom Commands
184319

185320
- **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

Comments
 (0)