我有一个问题,我想执行我的python脚本,但是使用这样的特殊命令:
iceweasel 'info.py server.py path_install.py'必须在客户端上输入此命令,然后使用以下信息打开页面:
info.py (= os and ip of client)
server.py
path_install.py但我真的不知道该从哪里开始.
发布于 2014-06-19 11:28:53
需求审查
看来,你想:
概念
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似乎是Python2.7版本以来的标准部分argparse可以做很多事情,但是需要在许多行上进行相当复杂的调用。plac是很好的替代方案,在大多数情况下可以快速地提供服务。docopt是最灵活的,同时在所需的代码行中最短。
python,则会有其他#!/usr/bin/env python作为脚本的第一行,设置它为可执行文件,然后您甚至可以删除.py扩展。只在*nix上工作,而不适用于Windows。setup.py并执行任务以安装脚本。在任何地方都可以工作,但是需要更多的编码。另一方面,如果您期望更多的用户使用该脚本,它可能是非常有效的解决方案,因为它可以大大简化安装过程。
https://stackoverflow.com/questions/24305156
复制相似问题