代码之家  ›  专栏  ›  技术社区  ›  Régis B.

自动完成命令行参数

  •  34
  • Régis B.  · 技术社区  · 14 年前

    在bash中,mplayer和imagemagick的“convert”等可执行文件在其命令行参数上具有很酷的自动完成功能。例如,如果我输入

    mplayer <tab><tab>
    

    同样,如果我输入

    convert -<tab><tab>
    

    我的问题是如何使用bash、ruby或python脚本实现类似的功能?

    3 回复  |  直到 7 年前
        1
  •  41
  •   Zoe - Save the data dump 张群峰    5 年前

    这是BASH智能完成的一个例子。基本描述如下 here ,编写自己的扩展的指南是 here here fuller featured introduction complete 命令(促进这种行为的命令)。

        2
  •  7
  •   hojusaram    7 年前

    http://web.archive.org/web/20090409201619/http://ifacethoughts.net/2009/04/06/extending-bash-auto-completion/

    Bash提供了一种指定关键字并使用它们 自动完成应用程序的命令行参数。我用vim 我索引的内容,而不是搜索它,和速度 显示它。我想添加的一个特性是访问这些标签

    这可以用一种直接的方法来实现:

    $ vim -t tagname
    

    标签的自动完成。

    我的.bashrc文件的以下代码:

    function get {
        vim -t $1
    } Now I can use get tagname command to get to the content.
    

    /etc/bash完成脚本。脚本允许我们添加 自动完成脚本/etc/bash-completion.d/目录并执行

    _get()
    {
        local cur
        COMPREPLY=()
        #Variable to hold the current word
        cur="${COMP_WORDS[COMP_CWORD]}"
    
        #Build a list of our keywords for auto-completion using
        #the tags file
        local tags=$(for t in `cat /home/anadgouda/wiki/tags | \
                          awk '{print $1}'`; do echo ${t}; done)
    
        #Generate possible matches and store them in the
        #array variable COMPREPLY
        COMPREPLY=($(compgen -W "${tags}" $cur))
    }
    
    #Assign the auto-completion function _get for our command get.
    complete -F _get get Once the /etc/bash-completion is sourced, you will get auto-completion for the tags when you use the get command.
    

    与我的wiki一起,我将它用于所有的文档工作和网站 索引系统让我记住上下文而不是文件名 和目录。

    你可以为你使用的任何工具调整这个系统。你所需要的一切 Bash可编程完成系统。

        3
  •  4
  •   wRAR    5 年前

    bash中的这个功能是由 bash-completion