I just fell down the rabbit hole of writing bash completion scripts. I
wish I knew where my Sunday morning went... :)

Here's a completion script for hellanzb. Review please?

One question:
"_filedir -d" doesn't seem interested in subdirectories, it adds a space
as soon as there's an unambiguous completion. It doesn't do this without
the -d. Is that really the desired behaviour?

BTW: Are there guidelines about when bash completion scripts belong in
bash-completion and when they belong in the package they complete for? I
couldn't find any...

SR

-- 
Stefano Rivera
  http://tumbleweed.org.za/
  H: +27 21 465 6908 C: +27 72 419 8559  UCT: x3127
# hellanzb(1) completion script
# Copyright 2010, Stefano Rivera <[email protected]>

have hellanzb && _hellanzb_opts() {
        # First extract long options then short options & rpc commands
        hellanzb -h 2> /dev/null \
                | sed -nre '/^  .*, --/ {h; s/.*(--[a-z-]+).*/\1/ p; g}' \
                      -e 's/^  ([a-zA-Z-]+)[, ].*/\1/ p'
}
[ -n "${have:-}" ] && _hellanzb() {
        local cur prev
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"

        if [[ $COMP_CWORD -eq 1 ]]; then
                COMPREPLY=( $(compgen -W "$(_hellanzb_opts)" -- "$cur") )
                return
        fi
        if [[ "$prev" = "enqueue" ]]; then
                _filedir '@(nzb|NZB|zip|ZIP)'
                return
        fi

        local dir_options file_options
        file_options="-c --config -l --log-file -d --debug-file"
        if echo "" $file_options | grep -Fwq -- "$prev"; then
                _filedir
                return
        fi      
        dir_options="process -p --post-process-dir"
        if echo "" $dir_options | grep -Fwq -- "$prev"; then
                _filedir -d
                return
        fi

        # Complete commands if we haven't had one yet
        local i hellanzb_opts commands
        hellanzb_opts="$(_hellanzb_opts)"
        commands="$(echo "" $hellanzb_opts | tr ' ' '\n' | grep -v '^-')"
        for ((i=0; i < COMP_CWORD; i++)); do
                if echo "" $commands | grep -Fwq -- "${COMP_WORDS[i]}"; then
                        return
                fi
        done
        COMPREPLY=( $(compgen -W "$hellanzb_opts" -- "$cur") )
}
[ -n "${have:-}" ] && complete -F _hellanzb hellanzb
_______________________________________________
Bash-completion-devel mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/bash-completion-devel

Reply via email to