【发布时间】:2015-05-18 16:29:47
【问题描述】:
我正在尝试使用docopt,以便用户可以进行如下输入:
python3 -p argum1 -d argum2 [-u arg_facul]
参数argum1 和argum 必须不是位置的;前两个参数是必需的,第三个是可选的。
我已经有了这个:
"""
Usage:
pyprogram.py (-p PASS | --pass=PASS) (-d DICT | --dict=DICT) [-u USER --user=USER]
Arguments:
Options:
-p demand argument 1
-d demand argument 2
-u may have this agrument or not
"""
输出是:
...$ python3 pyprogram.py -d dict.txt -p passwd.txt -u root
{'--dict': None, '--pass': None, '-d': True, '-p': True, '-u': True, 'DICT': 'passwd.txt', 'PASS': 'dict.txt', 'USER': 'root'}
我希望输出是:
... $ python3 pyprogram.py -d dict.txt -p passwd.txt -u root
{'--dict': None, '--pass': None, '-d': True, '-p': True, '-u': True, 'DICT': 'dict.txt', 'PASS': 'passwd.txt', 'USER': 'root'}
【问题讨论】:
-
您是否考虑过改用
argparse? -
我必须使用 docopt...人们告诉我 docopt 非常干净,比 argparse 更少的行和更容易...
-
问题是你也在这里暗示和订购。这样
dict必须在pass之后。这就是为什么你没有得到你想要的订单。 -
所以我可以这样做,这样我就不用你把 args 整理好了吗?
标签: python python-3.x docopt