arg.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. Cement core argument module.
  3. """
  4. from ..core import interface
  5. from ..core.handler import CementBaseHandler
  6. from ..utils.misc import minimal_logger
  7. LOG = minimal_logger(__name__)
  8. # pylint: disable=w0613
  9. def argument_validator(klass, obj):
  10. """Validates a handler implementation against the IArgument interface."""
  11. members = [
  12. '_setup',
  13. 'parse',
  14. 'add_argument',
  15. ]
  16. interface.validate(IArgument, obj, members)
  17. # pylint: disable=W0105,W0232,W0232,R0903,E0213,R0923
  18. class IArgument(interface.Interface):
  19. """
  20. This class defines the Argument Handler Interface. Classes that
  21. implement this handler must provide the methods and attributes defined
  22. below. Implementations do *not* subclass from interfaces.
  23. Example:
  24. .. code-block:: python
  25. from cement.core import interface, arg
  26. class MyArgumentHandler(arg.CementArgumentHandler):
  27. class Meta:
  28. interface = arg.IArgument
  29. label = 'my_argument_handler'
  30. """
  31. class IMeta:
  32. """Interface meta-data options."""
  33. label = 'argument'
  34. """The string identifier of the interface."""
  35. validator = argument_validator
  36. """Interface validator function."""
  37. # Must be provided by the implementation
  38. Meta = interface.Attribute('Handler Meta-data')
  39. def _setup(app_obj):
  40. """
  41. The _setup function is called during application initialization and
  42. must 'setup' the handler object making it ready for the framework
  43. or the application to make further calls to it.
  44. :param app_obj: The application object
  45. :returns: ``None``
  46. """
  47. # pylint: disable=E0211
  48. def add_argument(*args, **kw):
  49. """
  50. Add arguments for parsing. This should be -o/--option or positional.
  51. Note that the interface defines the following parameters so that at
  52. the very least, external extensions can guarantee that they can
  53. properly add command line arguments when necessary. The
  54. implementation itself should, and will provide and support many more
  55. options than those listed here. That said, the implementation must
  56. support the following:
  57. :arg args: List of option arguments. Generally something like
  58. ['-h', '--help'].
  59. :keyword dest: The destination name (var). Default: arg[0]'s string.
  60. :keyword help: The help text for --help output (for that argument).
  61. :keyword action: Must support: ['store', 'store_true', 'store_false',
  62. 'store_const']
  63. :keyword choices: A list of valid values that can be passed to an
  64. option whose action is ``store``.
  65. :keyword const: The value stored if action == 'store_const'.
  66. :keyword default: The default value.
  67. :returns: ``None``
  68. """
  69. def parse(arg_list):
  70. """
  71. Parse the argument list (i.e. sys.argv). Can return any object as
  72. long as it's members contain those of the added arguments. For
  73. example, if adding a '-v/--version' option that stores to the dest of
  74. 'version', then the member must be callable as 'Object().version'.
  75. :param arg_list: A list of command line arguments.
  76. :returns: Callable object
  77. """
  78. # pylint: disable=W0105
  79. class CementArgumentHandler(CementBaseHandler):
  80. """Base class that all Argument Handlers should sub-class from."""
  81. class Meta:
  82. """
  83. Handler meta-data (can be passed as keyword arguments to the parent
  84. class).
  85. """
  86. label = None
  87. """The string identifier of the handler implementation."""
  88. interface = IArgument
  89. """The interface that this class implements."""
  90. def __init__(self, *args, **kw):
  91. super(CementArgumentHandler, self).__init__(*args, **kw)