---
source: lib/src/complete/fish.rs
expression: "complete_fish_init(\"usage\")"
---
# @generated by usage-cli — auto-completion for usage shebang scripts
# Source this file from ~/.config/fish/conf.d/ (or your fish config) to enable
# <Tab> completion for any command on $PATH whose first line is a `usage`
# shebang.

function __usage_register_shebang_completions
    # `type -P` (not `-q`/`-p`) so a shell function named `usage` can't
    # satisfy the check — only a real executable on $PATH does.
    if not type -P usage &> /dev/null
        return 0
    end
    # `commandline -x` (fish 3.4+) tokenizes quoted/complex arguments more
    # accurately than `-o`. Mirror the per-binary `complete_fish` detection so
    # init-registered completions behave identically on modern fish.
    set -l cmdline_pre_cmd 'commandline -opc'
    if commandline -x >/dev/null 2>&1
        set cmdline_pre_cmd 'commandline -xpc'
    end
    set -l registered
    for dir in $PATH
        test -d $dir; or continue
        for file in $dir/*
            test -f $file -a -x $file; or continue
            # Skip files we can't read (e.g. setuid/execute-only binaries like
            # macOS's sudo/visudo) — reading them below would make fish print
            # a redirection warning on every shell startup.
            test -r $file; or continue
            # `string replace` works on every supported fish version; `path
            # basename` requires fish 3.5+ which excludes Ubuntu 22.04 LTS.
            set -l name (string replace -r '^.*/' '' -- $file)
            contains -- $name $registered; and continue
            # Cap the read at 128 chars so we don't buffer entire binary
            # executables that have no newline.
            set -l first
            read -l -n 128 first <$file 2>/dev/null
            if string match -q -- '#!*usage*' "$first"
                complete -c $name -x -a "(command usage complete-word --shell fish -f \"$file\" -- ($cmdline_pre_cmd) (commandline -t))"
                set -a registered $name
            end
        end
    end
end

__usage_register_shebang_completions
# vim: noet ci pi sts=0 sw=4 ts=4
