/argh/helpers.py

https://bitbucket.org/neithere/argh/ · Python · 53 lines · 20 code · 9 blank · 24 comment · 0 complexity · b4fb09773862ccd6638f7a87f6f71058 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (c) 2010—2013 Andrey Mikhaylenko and contributors
  4. #
  5. # This file is part of Argh.
  6. #
  7. # Argh is free software under terms of the GNU Lesser
  8. # General Public License version 3 (LGPLv3) as published by the Free
  9. # Software Foundation. See the file README for copying conditions.
  10. #
  11. """
  12. Helpers
  13. ~~~~~~~
  14. """
  15. import argparse
  16. from argh.completion import autocomplete
  17. from argh.assembling import add_commands, set_default_command
  18. from argh.dispatching import PARSER_FORMATTER, dispatch
  19. __all__ = ['ArghParser']
  20. class ArghParser(argparse.ArgumentParser):
  21. """ A subclass of :class:`ArgumentParser` with a couple of convenience
  22. methods.
  23. There is actually no need to subclass the parser. The methods are but
  24. wrappers for stand-alone functions :func:`~argh.assembling.add_commands`,
  25. :func:`~argh.completion.autocomplete` and
  26. :func:`~argh.dispatching.dispatch`.
  27. Uses :attr:`~argh.dispatching.PARSER_FORMATTER`.
  28. """
  29. def __init__(self, *args, **kwargs):
  30. kwargs.setdefault('formatter_class', PARSER_FORMATTER)
  31. super(ArghParser, self).__init__(*args, **kwargs)
  32. def set_default_command(self, *args, **kwargs):
  33. "Wrapper for :func:`set_default_command`."
  34. return set_default_command(self, *args, **kwargs)
  35. def add_commands(self, *args, **kwargs):
  36. "Wrapper for :func:`add_commands`."
  37. return add_commands(self, *args, **kwargs)
  38. def autocomplete(self):
  39. return autocomplete(self)
  40. def dispatch(self, *args, **kwargs):
  41. "Wrapper for :func:`dispatch`."
  42. return dispatch(self, *args, **kwargs)