| |
| |
| """ |
| Collection of custom argparse actions. |
| """ |
|
|
| from argparse import Action, _CountAction |
|
|
| from ..common.constants import NULL |
| from ..deprecations import deprecated |
|
|
|
|
| class NullCountAction(_CountAction): |
| @staticmethod |
| @deprecated("26.9", "27.3") |
| def _ensure_value(namespace, name, value): |
| if getattr(namespace, name, NULL) in (NULL, None): |
| setattr(namespace, name, value) |
| return getattr(namespace, name) |
|
|
| def __call__(self, parser, namespace, values, option_string=None): |
| count = getattr(namespace, self.dest, NULL) |
| if not isinstance(count, int): |
| count = 0 |
| setattr(namespace, self.dest, count + 1) |
|
|
|
|
| class ExtendConstAction(Action): |
| """ |
| A derivative of _AppendConstAction and Python 3.8's _ExtendAction |
| """ |
|
|
| def __init__( |
| self, |
| option_strings, |
| dest, |
| const, |
| default=None, |
| type=None, |
| choices=None, |
| required=False, |
| help=None, |
| metavar=None, |
| ): |
| super().__init__( |
| option_strings=option_strings, |
| dest=dest, |
| nargs="*", |
| const=const, |
| default=default, |
| type=type, |
| choices=choices, |
| required=required, |
| help=help, |
| metavar=metavar, |
| ) |
|
|
| def __call__(self, parser, namespace, values, option_string=None): |
| items = getattr(namespace, self.dest, None) |
| items = [] if items is None else items[:] |
| items.extend(values or [self.const]) |
| setattr(namespace, self.dest, items) |
|
|