我有以下python类工作,我想创建一个命令行界面供用户使用。我遇到了docopt,并认为使用它是一个好主意。我没有遇到过如何将docopt集成到python类的教程或文档,我得到的只是小函数。可能吗?一个有用的信息链接可以帮助我或只是一个小片段,以帮助我按照我的方式工作会非常有帮助。
class Bootcamp(object):
tasks = {1: "TDD", 2: "OOP", 3: "Programming Logic", 4:
"Version Control", 5: "Agile Methodology",
6: "Growth Mindset", 7: "Asking Questions", 8:
"Motivation and Commitment", 9: "Speaking"}
def __init__(self, name, tasks=tasks):
self.name = name
self.tasks = tasks
self.completed = []
self.incompleted = tasks.values()
def add_completed_items(self, i):
if i in self.tasks.keys():
self.completed.append(self.tasks[i])
self.incompleted.remove(self.tasks[i])
return "tasks added to complete"
else:
return "not in the tasks"
def check_progress(self):
self.progress = float(len(self.completed)) / \
float(len(self.tasks)) * 100
return int(self.progress)
答案 0 :(得分:1)
docopt
GitHub page有说明。
例如:
"""Naval Fate.
Usage:
naval_fate.py ship new <name>...
naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
naval_fate.py ship shoot <x> <y>
naval_fate.py mine (set|remove) <x> <y> [--moored | --drifting]
naval_fate.py (-h | --help)
naval_fate.py --version
Options:
-h --help Show this screen.
--version Show version.
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
"""
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='Naval Fate 2.0')
print(arguments)
我不确定docopt
是否适用于你希望做的事情。它设计用于在调用脚本时处理命令行参数,而不是整个命令行界面。