[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: referring to arguments in bash aliases



On Thu, Mar 04, 2004 at 08:15:51AM -0500, Timothy Stone wrote:
> 
> It's a start right? ;) Am I headed down the right path? Did I read to 
> much between the lines in the man pages for bash? Nothing on the web 
> dives this far into aliases in bash. Much seems to skip arguments in the 
> alias' replacement text.

Thats because aliases don't do arguments and function do. In fact,
AFAIK, any bash construct works in a function so they can be quite
involved and flexible if you want. 

An rpm enhancing function from the bash_completeion proejct that
does tab completion of command line:

# rpm(8) completion
# 
_rpm()
{
     local cur prev

     COMPREPLY=()
     cur=${COMP_WORDS[COMP_CWORD]}
     prev=${COMP_WORDS[COMP_CWORD-1]}

     if [ $COMP_CWORD -eq 1 ]; then
          # first parameter on line
          case "$cur" in
          -b*)
               COMPREPLY=( $( compgen -W '-ba -bb -bc -bi -bl -bp -bs'\
                           -- $cur ) )
               ;;
          -t*)
               COMPREPLY=( $( compgen -W '-ta -tb -tc -it -tl -tp -ts'\
                           -- $cur ) )
               ;;
          --*)
               COMPREPLY=( $( compgen -W '--help --version --initdb \
               --checksig --recompile --rebuild --resign --addsign \
               --rebuilddb --showrc --setperms --setugids --tarbuild \
               --eval --install --upgrade --query --freshen --erase \
               --verify --querytags --rmsource --rmspec --clean \
               --import' -- $cur ) )
               ;;
          *)
               COMPREPLY=( $( compgen -W '-b -e -F -i -q -t -U -V' \
                           -- $cur ) )
               ;;
          esac

     return 0
     fi

     case "$prev" in
     --@(@(db|exclude)path|prefix|relocate|root))
          _filedir -d
          return 0
          ;;
     --eval)
          # get a list of macros
          COMPREPLY=( $( sed -ne 's|^\(%'${cur#\%}'[^ '$'\t'']*\).*$|\1|p' \
                      /usr/lib/rpm/macros ) )
          return 0
          ;;
     --pipe)
          COMPREPLY=( $( compgen -c -- $cur ) )
          return 0
          ;;
     --rcfile)
          _filedir
          return 0
          ;;
     --specfile)
          # complete on .spec files
          _filedir spec
          return 0
          ;;
     --whatprovides)
          if [[ "$cur" == */* ]]; then
               _filedir
          else
          # complete on capabilities
               COMPREPLY=( $( rpm -qa --queryformat \
                         '%{providename}\n' | grep "^$cur" ) )
          fi
          return 0
          ;;
     --whatrequires)
          # complete on capabilities
          COMPREPLY=( $( rpm -qa --queryformat '%{requirename}\n' | \
                    grep "^$cur" ) )
          return 0
          ;;
     esac

     case "${COMP_WORDS[1]}" in
     -@([iFU]*|-install|-freshen|-upgrade))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--percent --force --test \
               --replacepkgs --replacefiles --root --excludedocs \
               --includedocs --noscripts --rcfile --ignorearch \
               --dbpath --prefix --ignoreos --nodeps --allfiles \
               --ftpproxy --ftpport --justdb --httpproxy --httpport \
               --noorder --relocate --badreloc --notriggers \
               --excludepath --ignoresize --oldpackage --define \
               --eval --pipe --queryformat --repackage --nosuggests \
               --nodigest --nosignature' -- $cur ) )
          else
               _filedir 'rpm'
          fi
          ;;
     -@(e|-erase))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--allmatches --noscripts \
               --notriggers --nodeps --test --repackage' -- $cur ) )
          else
               rpm_installed_packages
          fi
          ;;
     -@(q*|-query))
          # check whether we're doing file completion
          if [ "${COMP_LINE#* -*([^ -])f}" != "$COMP_LINE" ]; then
               _filedir
          elif [ "${COMP_LINE#* -*([^ -])g}" != "$COMP_LINE" ]; then
               rpm_group_completion
          elif [ "${COMP_LINE#* -*([^ -])p}" != "$COMP_LINE" ]; then
               # uninstalled package completion
               if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '--scripts --root \
                    --rcfile --whatprovides --whatrequires \
                    --requires --triggeredby --ftpport --ftpproxy \
                    --httpproxy --httpport --provides --triggers \
                    --dump --changelog --dbpath --filesbypkg \
                    --define --eval --pipe --showrc --info --list \
                    --state --docfiles --configfiles --queryformat \
                    --conflicts --obsoletes --nodigest \
                    --nosignature' -- $cur ) )
               else
                    _filedir 'rpm'
               fi
          else
               # installed package completion
               if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '--scripts --root \
                    --rcfile --whatprovides --whatrequires \
                    --requires --triggeredby --ftpport --ftpproxy \
                    --httpproxy --httpport --provides --triggers \
                    --dump --changelog --dbpath --specfile \
                    --querybynumber --last --filesbypkg --define \
                    --eval --pipe --showrc --info --list --state \
                    --docfiles --configfiles --queryformat \
                    --conflicts --obsoletes --pkgid --hdrid \
                    --fileid --tid --nodigest --nosignature' \
                    -- $cur ) )
               elif [ "${COMP_LINE#* -*([^ -])a}" == "$COMP_LINE" ]; then
                    rpm_installed_packages
               fi
          fi
          ;;
     -@(K|-checksig))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--nopgp --nogpg --nomd5 \
                         --nodigest --nosignature' -- $cur ) )
          else
               _filedir 'rpm'
          fi
          ;;
     -@([Vy]*|-verify))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--root --rcfile --dbpath \
               --nodeps --nofiles --noscripts --nomd5 --querytags \
               --specfile --whatrequires --whatprovides --nodigest \
               --nosignature' -- $cur ) )
          # check whether we're doing file completion
          elif [ "${COMP_LINE#* -*([^ -])f}" != "$COMP_LINE" ]; then
               _filedir
          elif [ "${COMP_LINE#* -*([^ -])g}" != "$COMP_LINE" ]; then
               rpm_group_completion
          elif [ "${COMP_LINE#* -*([^ -])p}" != "$COMP_LINE" ]; then
               _filedir 'rpm'
          else
               rpm_installed_packages
          fi
          ;;
     -[bt]*)
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--short-circuit --timecheck \
               --clean --rmsource --rmspec --test --sign --buildroot \
               --targetbuildarch --buildos --nobuild --nodeps \
               --nodirtokens' -- $cur ) )
          elif [[ ${COMP_WORDS[1]} == -b* ]]; then
               _filedir 'spec'
          else
               _filedir '@(tgz|tar.@(gz|bz2))'
          fi
          ;;
     --re@(build|compile))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--nodeps --rmsource \
                    --rmspec --sign --nodirtokens' -- $cur ) )
          else
               _filedir 'src.rpm'
          fi
          ;;
     --tarbuild)
          _filedir '@(tgz|tar.@(gz|bz2))'
          ;;
     --@(re|add)sign)
          _filedir 'rpm'
          ;;
     --set@(perms|gids))
          rpm_installed_packages
          ;;
     --@(clean|rms@(ource|pec)))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--clean --rmsource \
                         --rmspec' -- $cur ) )
          else
               _filedir 'spec'
          fi
          ;;
     --@(import|dbpath|root))
          if [[ "$cur" == -* ]]; then
               COMPREPLY=( $( compgen -W '--import --dbpath --root' \
                         -- $cur ) )
          else
               _filedir
          fi
          ;;
     esac

     return 0
}
complete -F _rpm $filenames rpm rpmbuild


## end

You might check your environment (ie set), RH probably has some functions already defined.

-- 
Hal Burgiss
 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]