Skip to content

Commit 19bd301

Browse files
committed
增加翻译adding-bash-completion-to-python-script
1 parent 65abb13 commit 19bd301

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#+TITLE: 让python脚本支持bash补全
2+
#+URL: http://blog.endpoint.com/2016/04/adding-bash-completion-to-python-script.html
3+
#+AUTHOR: lujun9972
4+
#+CATEGORY: Python Common
5+
#+DATE: [2016-06-09 四 20:18]
6+
#+OPTIONS: ^:{}
7+
8+
Bash有一项很有用的功能,就是当你在终端输入一个命令后,按两下 =<TAB>= 就会列出该命令支持的所有可能参数.
9+
10+
在Liquid Galaxy软件中心,我们有一个脚本,可以让我们以ssh的方式通过一个特定隧道连接到我们的其中一个装置上. 这个脚本本身很复杂,但是大多数时候使用方法很简单. 下面这行命令可以让我通过一个特定的代理服务器连接到Liquid Galaxy上的机器.
11+
#+BEGIN_SRC sh
12+
lg-ssh szymon
13+
#+END_SRC
14+
15+
其中 =szymon= 为我的LG的名字, 是从我的chef节点定义文件中抽取出来的.
16+
17+
该脚本还支持许多的其他参数,例如:
18+
#+BEGIN_SRC sh
19+
lg-ssh --chef-directory --ssh-identity --ssh-tunnel-port
20+
#+END_SRC
21+
22+
脚本支持的参数有两种格式: 一种是简单的字符串,还有一种是以--开头的参数
23+
24+
为了让该python脚本支持bash补全,首先需要先写一个简单的python脚本来列出所有的chef node名称.
25+
#+BEGIN_SRC python
26+
#!/usr/bin/env python
27+
28+
from sys import argv
29+
import os
30+
import json
31+
32+
if __name__ == "__main__":
33+
pattern = ""
34+
if len(argv) == 2:
35+
pattern = argv[1]
36+
37+
chef_dir = os.environ.get('LG_CHEF_DIR', None)
38+
if not chef_dir:
39+
exit(0)
40+
node_dirs = [os.path.join(chef_dir, "nodes"),
41+
os.path.join(chef_dir, "dev_nodes")]
42+
node_names = []
43+
44+
for nodes_dir in node_dirs:
45+
for root, dirs, files in os.walk(nodes_dir):
46+
for f in files:
47+
try:
48+
with open(os.path.join(root, f), 'r') as nf:
49+
data = json.load(nf)
50+
node_names.append(data['normal']['liquid_galaxy']['support_name'])
51+
except:
52+
pass
53+
54+
for name in node_names:
55+
print name
56+
#+END_SRC
57+
58+
然后还需要一种方法来获取python脚本支持的所有选项. 我们可以通过下面这行代码实现:
59+
#+BEGIN_SRC sh
60+
$LG_CHEF_DIR/repo_scripts/lg-ssh.py --help | grep ' --' | awk {'print $1'}
61+
#+END_SRC
62+
63+
最后通过一个简单的bash脚本来整合上面两个脚本.
64+
#+BEGIN_SRC sh
65+
_lg_ssh()
66+
{
67+
local cur prev opts node_names
68+
COMPREPLY=()
69+
cur="${COMP_WORDS[COMP_CWORD]}"
70+
prev="${COMP_WORDS[COMP_CWORD-1]}"
71+
opts=`$LG_CHEF_DIR/repo_scripts/lg-ssh.py --help | grep ' --' | awk {'print $1'}`
72+
node_names=`python $LG_CHEF_DIR/repo_scripts/node_names.py`
73+
74+
if [[ ${cur} == -* ]] ; then
75+
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
76+
return 0
77+
fi
78+
79+
COMPREPLY=( $(compgen -W "${node_names}" -- ${cur}) )
80+
}
81+
82+
complete -F _lg_ssh lg-ssh
83+
complete -F _lg_ssh lg-scp
84+
complete -F _lg_ssh lg-ssh.py
85+
#+END_SRC
86+
87+
最后,我们只需要在当前bash session中使用source命令加载该文件即可. 因此,我把下面这行添加到 =~/.bashrc= 中.
88+
#+BEGIN_SRC sh
89+
source $LG_CHEF_DIR/repo_scripts/lg-ssh.bash-completion
90+
#+END_SRC
91+
92+
至此,只要在终端中按两下 =<TAB>= 就会列出所有的补全选项了.
93+
#+BEGIN_EXAMPLE
94+
$ lg-ssh
95+
Display all 129 possibilities? (y or n)
96+
... and here go all 129 node names ...
97+
98+
$ lg-ssh h
99+
... and here go all node names beginning with 'h' ...
100+
101+
$ lg-ssh --
102+
.. and here go all the options beginning with -- ...
103+
#+END_EXAMPLE
104+
105+
该实现最大的好处在于,任何对python script的修改或chef node名称的修改,都会自动反映出来.

0 commit comments

Comments
 (0)