Get the FREE Ultimate OpenClaw Setup Guide →

shell-completion-generator

npx machina-cli add skill a5c-ai/babysitter/shell-completion-generator --openclaw
Files (1)
SKILL.md
9.1 KB

Shell Completion Generator

Generate comprehensive shell completion scripts for bash, zsh, and fish shells from CLI command definitions.

Capabilities

  • Generate bash completion scripts
  • Generate zsh completion with descriptions
  • Generate fish completion scripts
  • Support for subcommands and nested commands
  • Dynamic completion for arguments
  • File/directory path completion
  • Custom completion functions

Usage

Invoke this skill when you need to:

  • Add shell completions to a CLI application
  • Generate completions from command schemas
  • Create custom completion logic
  • Support multiple shells

Inputs

ParameterTypeRequiredDescription
cliNamestringYesName of the CLI executable
commandsarrayYesCommand definitions with options
shellsarrayNoTarget shells (default: all)
outputDirstringNoOutput directory for scripts
dynamicobjectNoDynamic completion configurations

Command Definition Structure

{
  "commands": [
    {
      "name": "deploy",
      "description": "Deploy application to environment",
      "options": [
        {
          "flags": ["-e", "--env"],
          "description": "Target environment",
          "type": "choice",
          "choices": ["dev", "staging", "prod"]
        },
        {
          "flags": ["-c", "--config"],
          "description": "Config file path",
          "type": "file",
          "extensions": [".json", ".yaml", ".yml"]
        }
      ],
      "arguments": [
        {
          "name": "service",
          "description": "Service to deploy",
          "type": "dynamic",
          "source": "services-list"
        }
      ],
      "subcommands": [
        {
          "name": "status",
          "description": "Check deployment status"
        }
      ]
    }
  ]
}

Output Structure

completions/
├── bash/
│   └── <cliName>.bash       # Bash completion script
├── zsh/
│   └── _<cliName>           # Zsh completion function
├── fish/
│   └── <cliName>.fish       # Fish completion script
└── install.sh               # Installation helper script

Generated Code Patterns

Bash Completion Script

#!/bin/bash
# Completion script for mycli

_mycli_completions() {
    local cur prev words cword
    _init_completion || return

    local commands="deploy rollback status config"

    case "${prev}" in
        mycli)
            COMPREPLY=($(compgen -W "${commands}" -- "${cur}"))
            return
            ;;
        deploy)
            COMPREPLY=($(compgen -W "--env --config --dry-run" -- "${cur}"))
            return
            ;;
        --env|-e)
            COMPREPLY=($(compgen -W "dev staging prod" -- "${cur}"))
            return
            ;;
        --config|-c)
            _filedir '@(json|yaml|yml)'
            return
            ;;
    esac

    # Handle subcommands
    if [[ ${words[1]} == "deploy" ]]; then
        case "${prev}" in
            status)
                # Dynamic completion for services
                local services=$(_mycli_get_services)
                COMPREPLY=($(compgen -W "${services}" -- "${cur}"))
                return
                ;;
        esac
    fi

    COMPREPLY=($(compgen -W "${commands}" -- "${cur}"))
}

# Dynamic completion helper
_mycli_get_services() {
    mycli services list --quiet 2>/dev/null
}

complete -F _mycli_completions mycli

Zsh Completion Script

#compdef mycli

_mycli() {
    local -a commands
    commands=(
        'deploy:Deploy application to environment'
        'rollback:Rollback to previous version'
        'status:Check deployment status'
        'config:Manage configuration'
    )

    local -a deploy_options
    deploy_options=(
        '(-e --env)'{-e,--env}'[Target environment]:environment:(dev staging prod)'
        '(-c --config)'{-c,--config}'[Config file path]:config file:_files -g "*.{json,yaml,yml}"'
        '--dry-run[Preview changes without applying]'
    )

    _arguments -C \
        '1: :->command' \
        '*:: :->args'

    case $state in
        command)
            _describe -t commands 'mycli commands' commands
            ;;
        args)
            case $words[1] in
                deploy)
                    _arguments $deploy_options \
                        '1:service:_mycli_services'
                    ;;
                rollback)
                    _arguments \
                        '(-v --version)'{-v,--version}'[Version to rollback]:version:_mycli_versions'
                    ;;
            esac
            ;;
    esac
}

# Dynamic completion for services
_mycli_services() {
    local -a services
    services=(${(f)"$(mycli services list --quiet 2>/dev/null)"})
    _describe -t services 'services' services
}

_mycli "$@"

Fish Completion Script

# Completions for mycli

# Disable file completions by default
complete -c mycli -f

# Main commands
complete -c mycli -n __fish_use_subcommand -a deploy -d 'Deploy application to environment'
complete -c mycli -n __fish_use_subcommand -a rollback -d 'Rollback to previous version'
complete -c mycli -n __fish_use_subcommand -a status -d 'Check deployment status'
complete -c mycli -n __fish_use_subcommand -a config -d 'Manage configuration'

# Deploy options
complete -c mycli -n '__fish_seen_subcommand_from deploy' -s e -l env -d 'Target environment' -xa 'dev staging prod'
complete -c mycli -n '__fish_seen_subcommand_from deploy' -s c -l config -d 'Config file path' -r -F
complete -c mycli -n '__fish_seen_subcommand_from deploy' -l dry-run -d 'Preview changes'

# Deploy subcommands
complete -c mycli -n '__fish_seen_subcommand_from deploy' -a status -d 'Check deployment status'

# Dynamic service completion
function __mycli_services
    mycli services list --quiet 2>/dev/null
end

complete -c mycli -n '__fish_seen_subcommand_from deploy; and not __fish_seen_subcommand_from status' -a '(__mycli_services)' -d 'Service'

Completion Types

TypeDescriptionExample
choiceFixed list of valuesenvironments, formats
fileFile path completionconfig files
directoryDirectory path completionoutput paths
dynamicRuntime-generated valuesservices, branches
commandSubcommand completionnested commands
noneNo completionfree-form text

Dynamic Completion Sources

{
  "dynamic": {
    "services-list": {
      "command": "mycli services list --quiet",
      "cache": 60
    },
    "git-branches": {
      "command": "git branch --format='%(refname:short)'",
      "cache": 10
    },
    "docker-images": {
      "command": "docker images --format '{{.Repository}}:{{.Tag}}'",
      "cache": 30
    }
  }
}

Installation Script

#!/bin/bash
# install.sh - Install shell completions for mycli

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

install_bash() {
    local dest="${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions"
    mkdir -p "$dest"
    cp "$SCRIPT_DIR/bash/mycli.bash" "$dest/mycli"
    echo "Installed bash completion to $dest/mycli"
}

install_zsh() {
    local dest="${ZDOTDIR:-$HOME}/.zfunc"
    mkdir -p "$dest"
    cp "$SCRIPT_DIR/zsh/_mycli" "$dest/_mycli"
    echo "Add 'fpath=(~/.zfunc \$fpath)' to .zshrc if not present"
    echo "Installed zsh completion to $dest/_mycli"
}

install_fish() {
    local dest="${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions"
    mkdir -p "$dest"
    cp "$SCRIPT_DIR/fish/mycli.fish" "$dest/mycli.fish"
    echo "Installed fish completion to $dest/mycli.fish"
}

case "$1" in
    bash) install_bash ;;
    zsh) install_zsh ;;
    fish) install_fish ;;
    all|"")
        install_bash
        install_zsh
        install_fish
        ;;
    *)
        echo "Usage: $0 [bash|zsh|fish|all]"
        exit 1
        ;;
esac

Workflow

  1. Parse command definitions - Extract commands, options, arguments
  2. Identify completion types - Map types to shell completion methods
  3. Generate bash script - Create complete-function based script
  4. Generate zsh script - Create _compdef function with descriptions
  5. Generate fish script - Create complete commands
  6. Create install script - Helper for users to install

Best Practices Applied

  • Descriptions in zsh completions
  • File extension filtering
  • Caching for dynamic completions
  • Subcommand handling
  • Option grouping (-e|--env)
  • Context-aware completions

References

Target Processes

  • shell-completion-scripts
  • cli-documentation-generation
  • cli-application-bootstrap

Source

git clone https://github.com/a5c-ai/babysitter/blob/main/plugins/babysitter/skills/babysit/process/specializations/cli-mcp-development/skills/shell-completion-generator/SKILL.mdView on GitHub

Overview

Shell Completion Generator creates comprehensive completion scripts for bash, zsh, and fish from CLI command definitions. It supports subcommands, nested commands, dynamic argument values, and file path completions, helping users discover and use CLIs more efficiently.

How This Skill Works

The tool ingests a CLI schema (commands, options, arguments, subcommands) and emits shell-specific completion scripts for bash, zsh, and fish, including dynamic and file completions. It outputs a structured set of files under completions/ with per-shell scripts and an install.sh helper to deploy them across environments.

When to Use It

  • When you need to ship cross-shell completions for a new CLI by defining its command schema.
  • When your CLI includes subcommands or nested commands that require accurate hints.
  • When you want dynamic argument suggestions (e.g., service lists) and file path completions.
  • When you want a standard install.sh to install completions across shells.
  • When generating completions from a JSON-like command schema to speed up onboarding.

Quick Start

  1. Step 1: Define your CLI schema (commands, options, arguments, subcommands) in the input JSON as shown.
  2. Step 2: Run the shell-completion-generator to produce bash, zsh, and fish scripts (and install.sh).
  3. Step 3: Place the generated files under completions/ and run install.sh to install them.

Best Practices

  • Define a clean command schema with commands, options (flags), types, and descriptions.
  • Specify file option extensions to enable proper file path completion.
  • Test completions in bash, zsh, and fish to ensure behavior and descriptions are accurate.
  • Leverage dynamic sources for arguments to keep completions up to date.
  • Use the provided install.sh to simplify deployment across environments.

Example Use Cases

  • Deploy command with -e/--env choices and -c/--config file completion for deployment.
  • Argument 'service' completed dynamically from a 'services-list' source.
  • Status subcommand under deploy demonstrating nested command completion.
  • Zsh completion included with descriptions for each command.
  • Fish completion script generated under completions/fish with a cross-shell install script.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers