0

我有以下 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) 
4

1 回答 1

1

docopt GitHub 页面有描述。


例如:

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

于 2017-04-09T07:12:41.310 回答