bash 使用自定义命令完成行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/427472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:37:13  来源:igfitidea点击:

Line completion with custom commands

bashunixcustomizationautocomplete

提问by pistacchio

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

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

$ myProgram doSomething
$ myProgram doSomethingElse
$ myProgram nowDoSomethingDifferent

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)

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

Any idea on how to implement this?

关于如何实现这一点的任何想法?

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

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

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

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

回答by richq

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

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

_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

回答by Peter Hoffmann

There is the module optcompletewhich allows you to write the completion for bash autocompletion in your python program. This is very useful in combination with optparse. You only define your arguments once, add the following to your .bashrc

有模块optcomplete允许您在 python 程序中编写 bash 自动完成的完成。这与 optparse 结合使用非常有用。您只需定义一次参数,将以下内容添加到您的 .bashrc

complete -F _optcomplete <program>

and all your options will be autocompleted.

并且您的所有选项都将自动完成。

回答by Scz

As mentioned in other answers, in bash this can be done with the bash-builtin complete. Easier than writing a function (as in richq's answer) is using complete's option -Wwhich lets you specify a list of words. In your example this would be:

正如其他答案中提到的,在 bash 中,这可以通过 bash-builtin 来完成complete。比编写函数(如richq 的回答)更容易是使用complete's 选项-W,它可以让您指定一个单词列表。在您的示例中,这将是:

complete -W "doSomething doSomethingElse nowDoSomethingDifferent" myProgram

As it is a one-liner you don't have to create a file for this, but you can just put it in your .bashrc.

由于它是单行的,因此您不必为此创建文件,但您只需将其放入.bashrc.

回答by Anders Westrup

If I understand correctly you want line completion on the command line before your python script starts. Then you shouldn't search for a python solution, but look at the shell features.

如果我理解正确,您希望在 python 脚本启动之前在命令行上完成行。那么你不应该搜索 python 解决方案,而是查看 shell 功能。

If you are using bash you can look at /etc/bash_completion, and at least on debian/ubuntu you should create a file in /etc/bash_completion.d/ that specifies the completions for your program.

如果您正在使用 bash,您可以查看 /etc/bash_completion,至少在 debian/ubuntu 上,您应该在 /etc/bash_completion.d/ 中创建一个文件,指定您程序的补全。

回答by sastanin

As well as I know, PYTHONSTARTUP is for commands to be executed when the interpreter starts up [1]. rlcompleteris for autocompletion insideyour script, if it is using readlinelibrary. Something like this:

据我所知, PYTHONSTARTUP 用于在解释器启动时执行的命令[1]rlcompleter用于脚本中自动完成,如果它使用readline库。像这样的东西:

$ ./myscript.py
My Script version 3.1415.
Enter your commands:
myscript> B<TAB>egin
myscript> E<TAB>nd

In your example you want to complete on the shellcommand line. This autocompletion is a shell feature (either bashor zsh, whatever you use). See, for example, an introduction to bash autocompletion(also part 2). For zshsee, for example this guide.

在您的示例中,您希望在shell命令行上完成。此自动完成功能是一个 shell 功能(无论您使用的是bash还是zsh)。例如,请参阅bash 自动完成简介(另见第 2 部分)。对于zsh见,例如本指南

回答by sastanin

If you want your program to select an command line option even though you only used an abbreviated form of this option you should have a look at the optparse modulein the standard library.

如果你希望你的程序选择一个命令行选项,即使你只使用了这个选项的缩写形式,你应该看看标准库中的optparse 模块