_dropbox() { local cur cmds cmdOpts opt helpCmds optBase i COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} cmds='filestatus help ls puburl start status stop' if [[ $COMP_CWORD -eq 1 ]] ; then COMPREPLY=( $( compgen -W "$cmds" -- $cur ) ) return 0 fi # if not typing an option, or if the previous option required a # parameter, then fallback on ordinary filename expansion helpCmds='help|--help|h|\?' if [[ ${COMP_WORDS[1]} != @($helpCmds) ]] && \ [[ "$cur" != -* ]] ; then return 0 fi cmdOpts= case ${COMP_WORDS[1]} in filestatus|help|ls|puburl|start|status|stop) cmdOpts="-h --help" ;; help|h|\?) cmdOpts="$cmds $qOpts" ;; *) ;; esac cmdOpts="$cmdOpts --help -h" # take out options already given for (( i=2; i<=$COMP_CWORD-1; ++i )) ; do opt=${COMP_WORDS[$i]} case $opt in --*) optBase=${opt/=*/} ;; -*) optBase=${opt:0:2} ;; esac cmdOpts=" $cmdOpts " cmdOpts=${cmdOpts/ ${optBase} / } # take out alternatives case $optBase in -h) cmdOpts=${cmdOpts/ --help / } ;; --help) cmdOpts=${cmdOpts/ -h / } ;; esac # skip next option if this one requires a parameter if [[ $opt == @($optsParam) ]] ; then ((++i)) fi done COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) ) return 0 } complete -F _dropbox -o default dropbox