我有一个问题,我想执行我的python脚本但是使用这样的特殊命令:
iceweasel 'info.py server.py path_install.py'
必须在客户端上键入此命令,我们打开包含信息的页面:
info.py (= os and ip of client)
server.py
path_install.py
但我真的不明白从哪里开始......
答案 0 :(得分:1)
看来,你想:
docopt
进行命令行参数解析(argparse
,plac
,其他也是替代方案)iceweasel.py
"""
Usage:
iceweasel.py <pythonfile>...
iceweasel.py -h
Prints internal details for arbirtary set of <pythonfile> files.
"""
import os
def srcdetails(fname):
with open(fname) as f:
content = f.read()
shortname = os.path.split(fname)[-1]
size = len(content)
words = len(content.split())
templ = """
---- {fname} -----
short name: {shortname}
size: {size}
words: {words}
"""
print templ.format(**locals())
def main(pythonfiles):
for fname in pythonfiles:
srcdetails(fname)
if __name__ == "__main__":
from docopt import docopt
args = docopt(__doc__)
pythonfiles = args["<pythonfile>"]
main(pythonfiles)
首先安装docopt
$ pip install docopt
不带参数调用命令:
$ python iceweasel.py
Usage:
iceweasel.py <pythonfile>...
iceweasel.py -h
尝试帮助
$ python iceweasel.py -h
Usage:
iceweasel.py <pythonfile>...
iceweasel.py -h
Prints internal details for arbirtary set of <pythonfile> files.
将它用于一个文件:
$ python iceweasel.py iceweasel.py
---- iceweasel.py -----
short name: iceweasel.py
size: 692
words: 74
使用通配符将其用于多个文件:
$ python iceweasel.py ../*.py
---- ../camera2xml.py -----
short name: camera2xml.py
size: 567
words: 47
---- ../cgi.py -----
short name: cgi.py
size: 612
words: 63
---- ../classs.py -----
short name: classs.py
size: 485
words: 44
argparse
似乎是自2.7版本以来Python的标准部分。argparse
可以做很多事,但需要在很多行上进行相当复杂的调用plac
是不错的选择,在大多数情况下可以快速服务docopt
在我看来是最灵活的,同时也是所需代码行中最短的python
,则可以选择其他方法
#!/usr/bin/env python
作为脚本的第一行,将其设置为可执行文件,然后您甚至可以删除.py
扩展名。仅适用于* nix,不适用于Windows setup.py
并对其进行任务以安装脚本。无处不在,但需要更多编码。另一方面,如果您希望更多用户使用该脚本,它可以是非常有效的解决方案,因为它可以显着简化安装过程。