线完成自定义命令 [英] Line completion with custom commands

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

问题描述

我的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的管理员(从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天全站免登陆