|  | 
|  | 1 | +# bash completion for copy command | 
|  | 2 | +# SPDX-License-Identifier: ISC | 
|  | 3 | + | 
|  | 4 | +_copy_completion() | 
|  | 5 | +{ | 
|  | 6 | +    local cur prev opts | 
|  | 7 | +    COMPREPLY=() | 
|  | 8 | +    cur="${COMP_WORDS[COMP_CWORD]}" | 
|  | 9 | +    prev="${COMP_WORDS[COMP_CWORD-1]}" | 
|  | 10 | + | 
|  | 11 | +    # Options for the copy command | 
|  | 12 | +    opts="-h -n -q -s -t -u -v" | 
|  | 13 | + | 
|  | 14 | +    local datastores_dst="running-config startup-config" | 
|  | 15 | +    local datastores_src="running-config startup-config factory-config" | 
|  | 16 | + | 
|  | 17 | +    case "${prev}" in | 
|  | 18 | +        -t) | 
|  | 19 | +            # Timeout expects a number, no completion | 
|  | 20 | +            return 0 | 
|  | 21 | +            ;; | 
|  | 22 | +        -u) | 
|  | 23 | +            # Username, could complete with getent passwd but keep it simple | 
|  | 24 | +            return 0 | 
|  | 25 | +            ;; | 
|  | 26 | +    esac | 
|  | 27 | + | 
|  | 28 | +    # If current word starts with -, complete options | 
|  | 29 | +    if [[ ${cur} == -* ]]; then | 
|  | 30 | +        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | 
|  | 31 | +        return 0 | 
|  | 32 | +    fi | 
|  | 33 | + | 
|  | 34 | +    # Determine position (source or destination) | 
|  | 35 | +    # Count non-option arguments before current position | 
|  | 36 | +    local arg_count=0 | 
|  | 37 | +    local i | 
|  | 38 | +    for ((i=1; i < COMP_CWORD; i++)); do | 
|  | 39 | +        case "${COMP_WORDS[i]}" in | 
|  | 40 | +            -h|-n|-q|-s|-v) | 
|  | 41 | +                # Flag without argument | 
|  | 42 | +                ;; | 
|  | 43 | +            -t|-u) | 
|  | 44 | +                # Flag with argument, skip next word | 
|  | 45 | +                ((i++)) | 
|  | 46 | +                ;; | 
|  | 47 | +            -*) | 
|  | 48 | +                # Unknown flag, might have argument | 
|  | 49 | +                ;; | 
|  | 50 | +            *) | 
|  | 51 | +                # Non-option argument | 
|  | 52 | +                ((arg_count++)) | 
|  | 53 | +                ;; | 
|  | 54 | +        esac | 
|  | 55 | +    done | 
|  | 56 | + | 
|  | 57 | +    # Helper function to add non-hidden files/dirs, filtering out dotfiles unless explicitly requested | 
|  | 58 | +    _add_files() { | 
|  | 59 | +        local IFS=$'\n' | 
|  | 60 | +        local files | 
|  | 61 | + | 
|  | 62 | +        # If user is explicitly typing a dotfile path, include dotfiles | 
|  | 63 | +        if [[ ${cur} == .* || ${cur} == */.* ]]; then | 
|  | 64 | +            files=( $(compgen -f -- "${cur}") ) | 
|  | 65 | +        else | 
|  | 66 | +            # Only show non-hidden files and directories | 
|  | 67 | +            files=( $(compgen -f -- "${cur}" | grep -v '/\.[^/]*$' | grep -v '^\.[^/]') ) | 
|  | 68 | +        fi | 
|  | 69 | + | 
|  | 70 | +        COMPREPLY+=( "${files[@]}" ) | 
|  | 71 | +    } | 
|  | 72 | + | 
|  | 73 | +    case ${arg_count} in | 
|  | 74 | +        0) | 
|  | 75 | +            # First argument (source): complete with files and all datastores including factory-config | 
|  | 76 | +            COMPREPLY=( $(compgen -W "${datastores_src}" -- ${cur}) ) | 
|  | 77 | +            _add_files | 
|  | 78 | +            ;; | 
|  | 79 | +        1) | 
|  | 80 | +            # Second argument (destination): complete with files and limited datastores (no factory-config) | 
|  | 81 | +            COMPREPLY=( $(compgen -W "${datastores_dst}" -- ${cur}) ) | 
|  | 82 | +            _add_files | 
|  | 83 | +            ;; | 
|  | 84 | +        *) | 
|  | 85 | +            # No more arguments expected | 
|  | 86 | +            return 0 | 
|  | 87 | +            ;; | 
|  | 88 | +    esac | 
|  | 89 | + | 
|  | 90 | +    return 0 | 
|  | 91 | +} | 
|  | 92 | + | 
|  | 93 | +complete -F _copy_completion copy | 
0 commit comments