1
0

docopt_command.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import sys
  4. from inspect import getdoc
  5. from docopt import docopt
  6. from docopt import DocoptExit
  7. def docopt_full_help(docstring, *args, **kwargs):
  8. try:
  9. return docopt(docstring, *args, **kwargs)
  10. except DocoptExit:
  11. raise SystemExit(docstring)
  12. class DocoptCommand(object):
  13. def docopt_options(self):
  14. return {'options_first': True}
  15. def sys_dispatch(self):
  16. self.dispatch(sys.argv[1:], None)
  17. def dispatch(self, argv, global_options):
  18. self.perform_command(*self.parse(argv, global_options))
  19. def parse(self, argv, global_options):
  20. options = docopt_full_help(getdoc(self), argv, **self.docopt_options())
  21. command = options['COMMAND']
  22. if command is None:
  23. raise SystemExit(getdoc(self))
  24. handler = self.get_handler(command)
  25. docstring = getdoc(handler)
  26. if docstring is None:
  27. raise NoSuchCommand(command, self)
  28. command_options = docopt_full_help(docstring, options['ARGS'], options_first=True)
  29. return options, handler, command_options
  30. def get_handler(self, command):
  31. command = command.replace('-', '_')
  32. if not hasattr(self, command):
  33. raise NoSuchCommand(command, self)
  34. return getattr(self, command)
  35. class NoSuchCommand(Exception):
  36. def __init__(self, command, supercommand):
  37. super(NoSuchCommand, self).__init__("No such command: %s" % command)
  38. self.command = command
  39. self.supercommand = supercommand