0

I have the following python class working and i want to create a command line interface for the user to use. I came across docopt and thought it was a good idea to use. I haven't come across a tutorial or documentation showing how to integrate docopt to your python classes all i get is just small functions . Is it possible? A link with helpful infromation would help me out or just a small snippet to help me work out my way would be very helpful.

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) 
Nix
  • 203
  • 1
  • 3
  • 14
  • What do you mean by "integrate docopt to your python classes" ? Maybe you are looking for docstrings? – greole Apr 09 '17 at 07:12
  • I want to create a cli interface for my class using docopt – Nix Apr 09 '17 at 10:04
  • Can you give an example of how you want to interact with your class via the cli? – greole Apr 09 '17 at 10:17
  • Its a class that was designed for a user to check tasks he or she has to do. The user can add the tasks that have been done and even check the progress in percentage using the check_progress method. Maybe the user could give his name, then check on the tasks and can later check the tasks that he has completed. He can view the remaining tasks. – Nix Apr 09 '17 at 14:48
  • `nick = Bootcamp("nick")` `print nick.tasks` `print nick.add_completed_items(1)` `print nick.add_completed_items(2)` `print nick.add_completed_items(3)` `print nick.incompleted` `print nick.completed` – Nix Apr 09 '17 at 14:50

1 Answers1

1

The docopt GitHub page has a description.


For instance:

"""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)

I'm not certain that docopt is designed for what you hope to do, though. It's designed for handling command-line parameters when a script is called, not an entire command-line interface.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
  • That's what i was asking whether it can make a CLI for an app. I guess i will just use it as a user guide to help in running my class – Nix Apr 09 '17 at 10:08
  • You might get better use from [Click](http://click.pocoo.org/5/prompts/). – Arya McCarthy Apr 09 '17 at 16:45