Docopt喜欢写很多文档,因为我似乎无法在他们的许多页面上找到如何编写注释部分的单个实际命令行调用。我有一个非常简单的文件:
"""Main.py
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
Options:
-h --help Show this screen.
--version Show version.
--outfile Output file.
"""
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='Main.py 1.0')
print(arguments)
我得到了第一部分:
adams-mbp:Aut adam$ python main.py model
{'--help': 0,
'--outfile': None,
'--version': 0,
'Options:': False,
'Show': 0,
'controller': False,
'file.': False,
'form': False,
'model': True,
'screen.': False,
'this': False,
'version.': False}
但是我似乎无法弄清楚如何传递一个--outfile
参数。这是我尝试过的:
adams-mbp:Aut adam$ python main.py main.py --outfile thing
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
Options:
-h --help Show this screen.
--version Show version.
--outfile Output file.
adams-mbp:Aut adam$ python main.py main.py --outfile=thing
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
Options:
-h --help Show this screen.
--version Show version.
--outfile Output file.
adams-mbp:Aut adam$ python --outfile thing main.py main.py
Unknown option: --
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
adams-mbp:Aut adam$ python --outfile=thing main.py main.py
Unknown option: --
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
adams-mbp:Aut adam$ python main.py main.py -outfile thing
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
Options:
-h --help Show this screen.
--version Show version.
--outfile Output file.
adams-mbp:Aut adam$ python main.py main.py -outfile=thing
Naval Fate.
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
Options:
-h --help Show this screen.
--version Show version.
--outfile Output file.
adams-mbp:Aut adam$ python main.py main.py outfile=thing
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
Options:
-h --help Show this screen.
--version Show version.
--outfile Output file.
认真吗?
答案 0 :(得分:0)
首先,为什么要两次调用脚本?
Usage:
main.py controller
main.py model
main.py form
main.py -h | --help
main.py --version
第二,docopt的用法部分向您展示了不同的脚本调用方式。
main.py model
--outfile
的工作方式,因为此处已进行了描述。但是Usage:
main.py controller
main.py model [--outfile]
在“用法”部分没有出现,因此docopt认为它是错误的输入。这就是脚本始终返回帮助的原因。
如果要使用此选项,则必须在“用法”中对其进行描述。
您可以这样做:
options
括号表明该选项不是必需的,请使用()作为必需选项。
另一点,即使您第一次调用显示错误,screen
也不应出现在词典中,也许您应该在文档之前留空。 this.
或"""Main.py
Usage:
main.py controller [--outfile]
main.py model [--outfile]
main.py form [--outfile]
main.py (-h | --help)
main.py --version
Options:
-h, --help Show this screen.
--version Show version.
--outfile Output file.
"""
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='Main.py 1.0')
print(arguments)
等都没有...
尝试类似的事情:
import GHC.TypeLits
import GHC.TypeLits.Extra
data TC (n::Nat) (d::Nat) = TC Int Int deriving Show
type family Norm (n::Nat) (d::Nat) ::(Nat, Nat) where
Norm n d = '(Div n (GCD n d), Div d (GCD n d))
norm :: Norm n d ~ '(np dp) => TC n d -> TC np dp
norm (a,b) = TC (div a (gcd a b)) (div b (gcd a b))