我如何开始对我的 python class 实施 docopt

How do i start to implement docopt to my python class

我有以下 python class 工作,我想创建一个命令行界面供用户使用。我遇到了 docopt 并认为使用它是个好主意。我还没有找到说明如何将 docopt 集成到您的 python classes 的教程或文档,我得到的只是一些小函数。可能吗? link 提供有用的信息会帮助我解决问题,或者只是一小段帮助我解决问题的方法会非常有帮助。

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) 

docoptGitHub 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 是为您希望做的事情而设计的。它设计用于在调用脚本时处理命令行参数,而不是整个命令行界面。