我是 docopt 的新手,在让一个小例子工作时遇到了一些困难。我刚才遇到了两个小问题,欢迎就这些问题提供帮助,并欢迎对改进代码提出更一般的意见。第一个问题是让程序要求该--required
选项。它应该在没有所需命令行选项的情况下在运行时打印文档字符串。第二个问题是让程序接受COMPUTER
选项(例如 )的参数(例如--computer
)。这将如何在终端中指定以及如何编码?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
my example program
Usage:
docopt_example_1.py [--ipaddress=IPADDRESS] [--computer=COMPUTER]
docopt_example_1.py --network <network>
docopt_example_1.py (--required)
docopt_example_1.py --notrequired
docopt_example_1.py --version
Arguments:
IPADDRESS I.P. address
COMPUTER computer identification
<network> network identification
Options:
-h, --help Show this help message.
--version Show the version and exit.
--required option required for running
--notrequired option not required for running
--ipaddress=IPADDRESS I.P. address
--computer=COMPUTER computer identification
--network=NETWORK network identification
"""
from docopt import docopt
def main(options):
print("----------")
print("a printout of the command line options as parsed by docopt:")
print(options)
print("----------")
if options["--notrequired"]:
print("required option selected")
if options["--computer"]:
print("computer name: {computer}".format(computer=options["COMPUTER"]))
if options["--network"]:
print("computer name: {network}".format(network=options["<network>"]))
else:
print("no options")
if __name__ == "__main__":
options = docopt(__doc__, version='1')
main(options)