completions/sftp.ps1

<#
 # sftp completion
 #>

Import-Module NativeCommandCompleter.psm -ErrorAction SilentlyContinue

$msg = data { ConvertFrom-StringData @'
    sftp = OpenSSH secure file transfer
    ipv4 = Forces sftp to use IPv4 addresses only
    ipv6 = Forces sftp to use IPv6 addresses only
    attemptContinue = Attempt to continue interrupted transfers
    bufferSize = Specify the size of the buffer that sftp uses when transferring files
    batchfile = Batch file
    ciphers = Specifies the ciphers to be used for encrypting the data transfers
    compression = Enables compression
    configFile = Specifies an alternative per-user configuration file
    directConnect = Connect directly to a local SFTP server
    jumpHost = Destination to jumpHost
    forwardAgent = Allows forwarding of ssh-agent to the remote system
    identity = Selects the file from which the identity for public key authentication is read
    limitBandwidth = Limits the used bandwidth, specified in Kbit/s
    noQuiet = Disables quiet mode
    sshOption = Can be used to pass options to ssh
    port = Specifies the port to connect to on the remote host
    preserveTimes = Preserves modification times, access times, and modes from the original files
    quiet = Quiet mode
    recursiveCopy = Recursively copy entire directories when uploading and downloading
    requests = Specify how many requests may be outstanding at any one time
    program = Program to use for the encrypted connection
    subsystem = Specifies the SSH2 subsystem or the path for an sftp server on the remote host
    verbose = Raise logging level
    fsync = Flush file to disk after transfer
    sftpOption = Specify an option that controls aspects of SFTP protocol behaviour.
'@
 }
Import-LocalizedData -BindingVariable localizedMessages -ErrorAction SilentlyContinue;
foreach ($key in $localizedMessages.Keys) { $msg[$key] = $localizedMessages[$key] }

Register-NativeCompleter -Name sftp -Description $msg.sftp -Parameters @(
    New-ParamCompleter -ShortName '4' -Description $msg.ipv4
    New-ParamCompleter -ShortName '6' -Description $msg.ipv6
    New-ParamCompleter -ShortName A -Description $msg.forwardAgent
    New-ParamCompleter -ShortName a -Description $msg.attemptContinue
    New-ParamCompleter -ShortName B -Description $msg.bufferSize -Arguments @{ Name = 'buffer_size' }
    New-ParamCompleter -ShortName b -Description $msg.batchfile -Arguments @{ Name = 'batchfile'; Type = 'File' }
    New-ParamCompleter -ShortName c -Description $msg.ciphers -Arguments @{ Name = 'cipher_spec' }
    New-ParamCompleter -ShortName C -Description $msg.compression
    New-ParamCompleter -ShortName D -Description $msg.directConnect -Arguments @{ Name = 'sftp_server_path' }
    New-ParamCompleter -ShortName F -Description $msg.configFile -Arguments @{ Name = 'ssh_config'; Type = 'File' }
    New-ParamCompleter -ShortName f -Description $msg.fsync
    New-ParamCompleter -ShortName i -Description $msg.identity -Arguments @{ Name = 'identity_file'; Type = 'File' }
    New-ParamCompleter -ShortName J -Description $msg.jumpHost -Arguments @{ Name = 'destination' }
    New-ParamCompleter -ShortName l -Description $msg.limitBandwidth -Arguments @{ Name = 'limit' }
    New-ParamCompleter -ShortName N -Description $msg.noQuiet
    New-ParamCompleter -ShortName o -Description $msg.sshOption -Arguments @{ Name = 'ssh_option' }
    New-ParamCompleter -ShortName P -Description $msg.port -Arguments @{ Name = 'port' }
    New-ParamCompleter -ShortName p -Description $msg.preserveTimes
    New-ParamCompleter -ShortName q -Description $msg.quiet
    New-ParamCompleter -ShortName R -Description $msg.requests -Arguments @{ Name = 'num_requests' }
    New-ParamCompleter -ShortName r -Description $msg.recursiveCopy
    New-ParamCompleter -ShortName S -Description $msg.program -Arguments @{ Name = 'program' }
    New-ParamCompleter -ShortName s -Description $msg.subsystem -Arguments @{ Name = 'subsystem' }
    New-ParamCompleter -ShortName v -Description $msg.verbose
    New-ParamCompleter -ShortName X -Description $msg.sftpOption -Arguments @{ Name = 'sftp_option' }
) -NoFileCompletions -Arguments @{
    Name = 'destination'
    Script = {
        $configFile = $this.BoundParameters["F"] ?? '~/.ssh/config'
        if (-not (Test-Path $configFile)) { return }
        Get-Content ~/.ssh/config | ForEach-Object {
            if ($_ -match '^\s*Host\s+(.+)$') {
                $hosts = $Matches[1] -split '\s+'
                foreach ($h in $hosts) {
                    if ($h -notmatch '[*?]' -and $h -like "$wordToComplete*") {
                        $h
                    }
                }
            }
        }
    }
}