Home Explore Blog CI



nushell

3rd chunk of `book/style_guide.md`
685baac0e09bd4b4d816662236949630c77822fe8b53202f0000000100000f14
    $el.status == UP
}

[1 2 3 4] | reduce {|elt, acc|
    $elt + $acc
}

{x: 1, y: 2}

[
  {name: "Teresa", age: 24},
  {name: "Thomas", age: 26}
]

let selectedProfile = (for it in ($credentials | transpose name credentials) {
    echo $it.name
})
```

Incorrect:

```nu
# too many spaces before "|el|": no space is allowed (like in one-line format)
[[status]; [UP] [UP]] | all { |el|
    # too few "\n" before "}": one "\n" is required
    $el.status == UP}

# too many spaces before "2": one space is required (like in one-line format)
[1  2 3 4] | reduce {|elt, acc|
    $elt + $acc
}

{
   # too many "\n" before "x": one-line format required as no nested lists or record exist
   x: 1,
   y: 2
}

# too few "\n" before "{": multi-line format required as there are two nested records
[{name: "Teresa", age: 24},
  {name: "Thomas", age: 26}]

let selectedProfile = (
    # too many "\n" before "foo": no "\n" is allowed
    for it in ($credentials | transpose name credentials) {
        echo $it.name
})
```

## Naming Convention

### Abbreviations and Acronyms

**It's recommended** to use full concise words over abbreviations and acronyms, unless they are well-known and/or
commonly used.

Correct:

```nu
query-user --id 123

$user.name | str downcase
```

Incorrect:

```nu
qry-usr --id 123

$user.name | string downcase
```

### Case

#### Commands

**It's recommended to** use kebab-case for command names with multiple words.

Correct:

```nu
fetch-user --id 123
```

Incorrect:

```nu
fetch_user --id 123
fetchUser --id 123
```

See also [Naming Commands](custom_commands.md#naming-commands).

#### Sub-Commands

Sub commands are commands that are logically grouped under a parent command and separated by a space.
**It's recommended to** use kebab-case for the sub-command name.

Correct:

```nu
date now

date list-timezone

def "login basic-auth" [username: string password: string] {
    # ...
}
```

See also [Naming Sub-Commands](custom_commands.md#subcommands).

#### Flags

**It's recommended to** use kebab-case for flag names.

Correct:

```nu
def greet [name: string, --all-caps] {
    # ...
}
```

Incorrect:

```nu
def greet [name: string, --all_caps] {
    # ...
}
```

::: tip
Notice that the name used to access the flag is accessed by replacing the dash with an underscore in the resulting
variable name.

See [Flags](custom_commands.md#flags).
:::

#### Variables and Command Parameters

**It's recommended to** use snake_case for variable names, including command parameters.

Correct:

```nu
let user_id = 123

def fetch-user [user_id: int] {
  # ...
}
```

Incorrect:

```nu
let user-id = 123
let userId = 123

def fetch-user [user-id: int] {
  # ...
}
```

#### Environment Variables

**It's recommended to** use SCREAMING_SNAKE_CASE for environment variable names.

Correct:

```nu
$env.ENVIRONMENT_CODE = "prod"

$env.APP_VERSION = "1.0.0"
```

Incorrect:

```nu
$env.ENVIRONMENT-CODE = "prod"

$env.app_version = "1.0.0"
```

## Options and Parameters of Custom Commands

- **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.
- **It's recommended to** use positional parameters unless they can't be used due to rules listed here or technical restrictions.
  For instance, when there are several kinds of optional parameters (but at least one parameter should be provided)
  use options. Great example of this is `ansi gradient` command where at least foreground or background must be passed.
- **It's recommended to** provide both long and short options.

## Documentation

- **It's recommended to** provide documentation for all exported entities (like custom commands) and their
  inputs (like custom command parameters and options).

Title: Nushell Naming Conventions and Custom Command Options
Summary
This section outlines Nushell's naming conventions, covering abbreviations, case usage for commands, sub-commands, flags, variables, command parameters, and environment variables. It advocates for kebab-case for commands and flags, snake_case for variables and parameters, and SCREAMING_SNAKE_CASE for environment variables. It also gives tips for deciding whether to use positional parameters or options for a custom command, and recommends providing both long and short versions of options. It concludes with the importance of documenting exported entities and their inputs.