Skip to content

Commit 944676e

Browse files
committed
update
1 parent 19bd301 commit 944676e

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

‎Python Common/让python脚本支持bash补全.org‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@
77

88
Bash有一项很有用的功能,就是当你在终端输入一个命令后,按两下 =<TAB>= 就会列出该命令支持的所有可能参数.
99

10-
在Liquid Galaxy软件中心,我们有一个脚本,可以让我们以ssh的方式通过一个特定隧道连接到我们的其中一个装置上. 这个脚本本身很复杂,但是大多数时候使用方法很简单. 下面这行命令可以让我通过一个特定的代理服务器连接到Liquid Galaxy上的机器.
10+
接下来,我会展示如何为一段python脚本也赋予这项能力. 而且, 该解决方案要足够的通用. 使得他能自动适应对python脚本选项或配置文件所做出的改动.
11+
12+
该脚本接受两种类型的参数. 一类是以'--'开头的标志,一类是从chef脚本中取出来的主机名称.
13+
14+
假设该脚本名为 =show.py= - 它的作用是显示某主机的信息. 它的使用方法为:
15+
1116
#+BEGIN_SRC sh
12-
lg-ssh szymon
17+
show.py szymon
1318
#+END_SRC
1419

1520
其中 =szymon= 为我的LG的名字, 是从我的chef节点定义文件中抽取出来的.
1621

1722
该脚本还支持许多的其他参数,例如:
1823
#+BEGIN_SRC sh
19-
lg-ssh --chef-directory --ssh-identity --ssh-tunnel-port
24+
show.py --cpu --memory --format=json
2025
#+END_SRC
2126

2227
脚本支持的参数有两种格式: 一种是简单的字符串,还有一种是以--开头的参数
@@ -57,7 +62,7 @@ Bash有一项很有用的功能,就是当你在终端输入一个命令后,按
5762

5863
然后还需要一种方法来获取python脚本支持的所有选项. 我们可以通过下面这行代码实现:
5964
#+BEGIN_SRC sh
60-
$LG_CHEF_DIR/repo_scripts/lg-ssh.py --help | grep ' --' | awk {'print $1'}
65+
$LG_CHEF_DIR/repo_scripts/show.py --help | grep ' --' | awk {'print $1'}
6166
#+END_SRC
6267

6368
最后通过一个简单的bash脚本来整合上面两个脚本.
@@ -68,7 +73,7 @@ Bash有一项很有用的功能,就是当你在终端输入一个命令后,按
6873
COMPREPLY=()
6974
cur="${COMP_WORDS[COMP_CWORD]}"
7075
prev="${COMP_WORDS[COMP_CWORD-1]}"
71-
opts=`$LG_CHEF_DIR/repo_scripts/lg-ssh.py --help | grep ' --' | awk {'print $1'}`
76+
opts=`$LG_CHEF_DIR/repo_scripts/show.py --help | grep ' --' | awk {'print $1'}`
7277
node_names=`python $LG_CHEF_DIR/repo_scripts/node_names.py`
7378

7479
if [[ ${cur} == -* ]] ; then
@@ -79,27 +84,28 @@ Bash有一项很有用的功能,就是当你在终端输入一个命令后,按
7984
COMPREPLY=( $(compgen -W "${node_names}" -- ${cur}) )
8085
}
8186

82-
complete -F _lg_ssh lg-ssh
83-
complete -F _lg_ssh lg-scp
84-
complete -F _lg_ssh lg-ssh.py
87+
complete -F _lg_ssh show.py
8588
#+END_SRC
8689

8790
最后,我们只需要在当前bash session中使用source命令加载该文件即可. 因此,我把下面这行添加到 =~/.bashrc= 中.
8891
#+BEGIN_SRC sh
89-
source $LG_CHEF_DIR/repo_scripts/lg-ssh.bash-completion
92+
source $LG_CHEF_DIR/repo_scripts/show.bash-completion
9093
#+END_SRC
9194

9295
至此,只要在终端中按两下 =<TAB>= 就会列出所有的补全选项了.
9396
#+BEGIN_EXAMPLE
94-
$ lg-ssh
95-
Display all 129 possibilities? (y or n)
96-
... and here go all 129 node names ...
97+
$ show.py <tab><tab>
98+
Display all 42 possibilities? (y or n)
99+
... and here go all 42 node names ...
100+
</tab></tab>
97101

98-
$ lg-ssh h
102+
$ show.py h<tab><tab>
99103
... and here go all node names beginning with 'h' ...
104+
</tab></tab>
100105

101-
$ lg-ssh --
106+
$ show.py --<tab><tab>
102107
.. and here go all the options beginning with -- ...
108+
</tab></tab>
103109
#+END_EXAMPLE
104110

105111
该实现最大的好处在于,任何对python script的修改或chef node名称的修改,都会自动反映出来.

0 commit comments

Comments
 (0)