简体   繁体   English

使用argparse完成Bash选项卡不会显示目录中的所有文件

[英]Bash tab completion with argparse does not show all the files in the directory

I noticed that bash tab completion returns less files if I use an argparse parameter. 我注意到,如果使用argparse参数,bash选项卡完成返回的文件较少。 How can I change/control that? 我该如何更改/控制?

minimal example code 最小的示例代码

me@here:~/test$ cat argparsetest.py 
import argparse
parser.add_argument('-i', help='input', required=True)

bash completion examples: bash完成示例:

# shows all the files 
me@here:~/test$ python argparsetest.py 
argparsetest.py  result.png       u1.py  

# does not show the image result.png I am actually interested in
me@here:~/test$ python argparsetest.py -i
argparsetest.py  u1.py            

There are already two similar questions, but I did not find them helpfull. 已经有两个类似的问题,但我没有发现它们有用。

This is not related to argparse or even Python itself. 这与argparse甚至Python本身无关。 You probably have " programmable bash completion " enabled, and the completion rules are being confused because your command line starts with "python". 您可能已启用“ 可编程bash完成 ”,并且完成规则正在混淆,因为您的命令行以“python”开头。

The easiest way to work around that is to add to the top of your python file: 解决这个问题的最简单方法是添加到python文件的顶部:

#!/usr/bin/env python

, then make the Python script executable: ,然后使Python脚本可执行:

me@here:~/test$ chmod u+x argparsetest.py 

and then call it directly, without explicitly calling "python": 然后直接调用它,而不显式调用“python”:

me@here:~/test$ ./argparsetest.py<TAB>
argparsetest.py  result.png       u1.py  

me@here:~/test$ ./argparsetest.py -i<TAB>
argparsetest.py  result.png       u1.py  

Alternatively, you can turn off bash completion completely with 或者,您可以完全关闭bash完成

complete -r

And, if you want to disable it for future session, comment out or remove the lines on either your ~/.bashrc or /etc/bashrc that probably look like this: 并且,如果要在将来的会话中禁用它,请注释掉或删除〜/ .bashrc或/ etc / bashrc中可能如下所示的行:

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM