docopt_command.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. # we certainly want to have "exec" command, since that's what docker client has
  33. # but in python exec is a keyword
  34. if command == "exec":
  35. command = "exec_command"
  36. if not hasattr(self, command):
  37. raise NoSuchCommand(command, self)
  38. return getattr(self, command)
  39. class NoSuchCommand(Exception):
  40. def __init__(self, command, supercommand):
  41. super(NoSuchCommand, self).__init__("No such command: %s" % command)
  42. self.command = command
  43. self.supercommand = supercommand