Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用argparse完成Bash选项卡不会显示目录中的所有文件_Python_Bash_Shell_Argparse - Fatal编程技术网

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

Python 使用argparse完成Bash选项卡不会显示目录中的所有文件,python,bash,shell,argparse,Python,Bash,Shell,Argparse,我注意到,如果使用argparse参数,bash tab completion返回的文件会更少。我怎样才能改变/控制它 最小示例代码 me@here:~/test$ cat argparsetest.py import argparse parser.add_argument('-i', help='input', required=True) bash完成示例: # shows all the files me@here:~/test$ python argparsetest.py a

我注意到,如果使用argparse参数,bash tab completion返回的文件会更少。我怎样才能改变/控制它

最小示例代码

me@here:~/test$ cat argparsetest.py 
import argparse
parser.add_argument('-i', help='input', required=True)
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            
已经有两个类似的问题,但我觉得它们没有帮助


这与argparse甚至Python本身无关。您可能启用了“”,并且由于命令行以“python”开头,所以完成规则很混乱

解决此问题的最简单方法是将以下内容添加到python文件的顶部:

#!/usr/bin/env python
,然后使Python脚本可执行:

me@here:~/test$ chmod u+x argparsetest.py 
然后直接调用它,而不显式调用“python”:

而且,如果您想在以后的会话中禁用它,请注释掉或删除~/.bashrc或/etc/bashrc上可能如下所示的行:

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

我无法重现这种行为。python 2.7.2,bash完成版本
1:1.3-1ubuntu6
。带和不带
-i
的选项卡都包含png文件。此外,在最小代码中缺少一行,如
parser=argparse.ArgumentParser(description='do things')
complete -r
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi