使用自定义命令完成行 [英] Line completion with custom commands

查看:17
本文介绍了使用自定义命令完成行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Python 程序可以使用一系列不同的选项(或子命令)启动,例如:

my Python program can be launched with a range of different options (or subcommands) like:

$ myProgram doSomething
$ myProgram doSomethingElse
$ myProgram nowDoSomethingDifferent

我希望它使用自动完成功能,这样如果我输入myProgram d",它就会返回myProgram doSomething",如果我输入myProgram n",它会呈现myProgram nowDoSomethingDifferent".这类似于模块 rlcompleter 的平均使用,但它不会从文件系统(或从历史记录)中选择可能的完成选项,而是从一组自定义字符串(对应于可用选项)我的程序)

I want it to use auto-completion with so that if i type "myProgram d" it returns "myProgram doSomething" and if i type "myProgram n" it renders "myProgram nowDoSomethingDifferent". This is similar to the average use of the module rlcompleter, but it does not pick possible completion options from the filesystem (or from history) but from a custom set of strings (that correspond to the available options for my program)

对如何实现这一点有任何想法吗?

Any idea on how to implement this?

我知道变量 PYTHONSTARTUP(它应该指向一个我不知道如何编写的文件).

I'm aware of the variable PYTHONSTARTUP (that should point to a file I don't know how to write).

作为一个工作示例,django-admin(来自 django 包)具有我正在寻找的完全相同的功能

As a working example, django-admin (from the django package) has the same exact feature i'm looking for

推荐答案

创建一个文件myprog-completion.bash"并在您的 .bashrc 文件中获取它.像这样的东西让你开始......

Create a file "myprog-completion.bash" and source it in your .bashrc file. Something like this to get you started...

_myProgram()
{
  cur=${COMP_WORDS[COMP_CWORD]}
  case "${cur}" in
    d*) use="doSomething" ;;
    n*) use="nowDoSomethingElse" ;;
  esac
  COMPREPLY=( $( compgen -W "$use" -- $cur ) )
}
complete -o default -o nospace -F _myProgram  myProgram

这篇关于使用自定义命令完成行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆