Skip to content

Commit

Permalink
adapt completions for clab alias (#2388)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt authored Jan 14, 2025
1 parent bf7e377 commit efd529e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var rootCmd = &cobra.Command{
Use: "containerlab",
Short: "deploy container based lab environments with a user-defined interconnections",
PersistentPreRunE: preRunFn,
Aliases: []string{"clab"},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
69 changes: 50 additions & 19 deletions docs/cmd/completion.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,83 @@
# shell completions

### Description
## Description

The `completion` command generates shell completions for bash/zsh/fish shells.

### Usage
## Usage

`containerlab completion [arg]`

#### Bash completions
### Bash completions

> Ensure that bash-completion is installed on your system.
To load completions for the current session:

```bash
source <(containerlab completion bash)
```

To load completions for each session, execute once:
To load completions for each session:

/// tab | Linux

```bash
# linux
$ containerlab completion bash > /etc/bash_completion.d/containerlab
containerlab completion bash > /etc/bash_completion.d/containerlab
```

# macOS
$ containerlab completion bash > /usr/local/etc/bash_completion.d/containerlab
///
/// tab | macOS

```bash
containerlab completion bash > /usr/local/etc/bash_completion.d/containerlab
```

#### ZSH completions
If shell completion is not already enabled in your environment, users will need to enable it. To do so, execute the following once:
///

To also autocomplete for `clab` command alias, add the following to your `.bashrc` or `.bash_profile`:

```bash
echo "autoload -U compinit; compinit" >> ~/.zshrc
complete -o default -F __start_containerlab clab
```

To load completions for each session, execute once:
### ZSH completions

If shell completion is not already enabled in your environment you have to enable it by ensuring zsh completions are loaded. The following can be added to your zshrc:

```bash
autoload -U compinit; compinit
```

To load completions for each session generate the completion script and store it somewhere in your `$fpath`:

```bash
clab completion zsh | \
sed '1,2c\#compdef containerlab clab\ncompdef _containerlab containerlab clab' > \
~/.oh-my-zsh/custom/completions/_containerlab
```

/// admonition | Completion script location
type: subtle-note
`echo $fpath` will show the directories zsh reads files from. You can either use one of the available completions directories from this list or add a new directory to the list by adding this in your .zshrc file:

```bash
containerlab completion zsh > "${fpath[1]}/_containerlab"
fpath=(~/.oh-my-zsh/custom/completions $fpath)
```

!!!info
Note: `$fpath[1]` in this command refers to the first path in `$fpath`. Ensure you
use the index pointing to the completion folder, find the correct index by inspecting
the output of `echo $fpath`
And then using `~/.oh-my-zsh/custom/completions` for your completions.
///

Start a new shell for this setup to take effect.

#### Fish completions
### Fish completions

```bash
containerlab completion fish | source
```

To load completions for each session, execute once:

```
containerlab completion fish > ~/.config/fish/completions/containerlab.fish
```
```

0 comments on commit efd529e

Please sign in to comment.