简体   繁体   中英

zsh completion for custom script

I wrote a small script that runs the whole system rsync from https://wiki.archlinux.org/index.php/full_system_backup_with_rsync which takes a few command line options and the hostname of the system as arguments. I'd like to give this the type of autocompletion given to things like ssh. However, I don't really want to enter a user, since root is pretty much the only user that can do this.

Ideally, I'd like to type wholesystem.sh fi and have it fill in fileserver.domain.com

Here's my custom completion file. The options do work, but the host fill in obviously doesn't and I'm just lost where to go from here.

#compdef wholesystem.sh
typeset -A opt_args
_arguments \
  '-h[help]' \
  '-s[silent]' \
  '-v[verbose]' \
  '-d[dry run]' \
  ':remote system:_user_at_host:'

I could either have it pull from ~/.ssh/known_hosts which I'm fine with, or... The backup system saves backups in ~/WholeSystems/host.domain.com/, and I wouldn't mind if it got its ideas from there.

I suggest something like that:

#compdef wholesystem.sh

_wholesystem.sh() {
  typeset -A opt_args
  _arguments \
    '-h[help]' \
    '-s[silent]' \
    '-v[verbose]' \
    '-d[dry run]' \
    ':remote system:_wholesystem_hosts'
}

_wholesystem_hosts() {
[...]
}

_wholesystem.sh

The content of _wholesystem_hosts depends on which kind of hosts you want. For instance, if you want the same hosts as ssh , you can copy the _ssh_hosts code from .../functions/Completion/Unix/_ssh .

I don't know whether this is a good way to do, but this seems to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM