docopt_command.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from inspect import getdoc
  2. from docopt import docopt
  3. from docopt import DocoptExit
  4. def docopt_full_help(docstring, *args, **kwargs):
  5. try:
  6. return docopt(docstring, *args, **kwargs)
  7. except DocoptExit:
  8. raise SystemExit(docstring)
  9. class DocoptDispatcher(object):
  10. def __init__(self, command_class, options):
  11. self.command_class = command_class
  12. self.options = options
  13. def parse(self, argv):
  14. command_help = getdoc(self.command_class)
  15. options = docopt_full_help(command_help, argv, **self.options)
  16. command = options['COMMAND']
  17. if command is None:
  18. raise SystemExit(command_help)
  19. handler = get_handler(self.command_class, command)
  20. docstring = getdoc(handler)
  21. if docstring is None:
  22. raise NoSuchCommand(command, self)
  23. command_options = docopt_full_help(docstring, options['ARGS'], options_first=True)
  24. return options, handler, command_options
  25. def get_handler(command_class, command):
  26. command = command.replace('-', '_')
  27. # we certainly want to have "exec" command, since that's what docker client has
  28. # but in python exec is a keyword
  29. if command == "exec":
  30. command = "exec_command"
  31. if not hasattr(command_class, command):
  32. raise NoSuchCommand(command, command_class)
  33. return getattr(command_class, command)
  34. class NoSuchCommand(Exception):
  35. def __init__(self, command, supercommand):
  36. super(NoSuchCommand, self).__init__("No such command: %s" % command)
  37. self.command = command
  38. self.supercommand = supercommand