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