| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
""" |
| 5 |
This module extends Python's built-in argparse library by adding several |
| 6 |
custom methods. |
| 7 |
""" |
| 8 |
|
| 9 |
import argparse |
| 10 |
|
| 11 |
from .cli import INHERIT |
| 12 |
|
| 13 |
# We have to use implementation details of the argparse module... |
| 14 |
# pylint: disable=protected-access |
| 15 |
|
| 16 |
|
| 17 |
def dummy(): |
| 18 |
'''Since this module only modifies the classes by argparse we provide this |
| 19 |
function so it can be called to silence warnings.''' |
| 20 |
|
| 21 |
|
| 22 |
def _action_complete(self, command, *args): |
| 23 |
setattr(self, '_complete', (command, *args)) |
| 24 |
return self |
| 25 |
|
| 26 |
|
| 27 |
def _action_get_complete(self): |
| 28 |
return getattr(self, '_complete', None) |
| 29 |
|
| 30 |
|
| 31 |
def _action_when(self, when): |
| 32 |
setattr(self, '_when', when) |
| 33 |
return self |
| 34 |
|
| 35 |
|
| 36 |
def _action_get_when(self): |
| 37 |
return getattr(self, '_when', None) |
| 38 |
|
| 39 |
|
| 40 |
def _action_set_repeatable(self, enable=True): |
| 41 |
setattr(self, '_repeatable', enable) |
| 42 |
return self |
| 43 |
|
| 44 |
|
| 45 |
def _action_get_repeatable(self): |
| 46 |
return getattr(self, '_repeatable', INHERIT) |
| 47 |
|
| 48 |
|
| 49 |
def _action_set_final(self, enable=True): |
| 50 |
setattr(self, '_final', enable) |
| 51 |
return self |
| 52 |
|
| 53 |
|
| 54 |
def _action_get_final(self): |
| 55 |
return getattr(self, '_final', False) |
| 56 |
|
| 57 |
|
| 58 |
def _parser_alias(self, alias): |
| 59 |
setattr(self, '_aliases', [alias]) |
| 60 |
return self |
| 61 |
|
| 62 |
|
| 63 |
def _parser_aliases(self, aliases): |
| 64 |
setattr(self, '_aliases', list(aliases)) |
| 65 |
return self |
| 66 |
|
| 67 |
|
| 68 |
def _parser_get_aliases(self): |
| 69 |
return getattr(self, '_aliases', []) |
| 70 |
|
| 71 |
|
| 72 |
def _parser_remove_help(self): |
| 73 |
self._actions.pop(0) |
| 74 |
self._option_string_actions.pop('-h') |
| 75 |
self._option_string_actions.pop('--help') |
| 76 |
|
| 77 |
|
| 78 |
argparse.Action.complete = _action_complete |
| 79 |
argparse.Action.get_complete = _action_get_complete |
| 80 |
argparse.Action.when = _action_when |
| 81 |
argparse.Action.get_when = _action_get_when |
| 82 |
argparse.Action.set_repeatable = _action_set_repeatable |
| 83 |
argparse.Action.get_repeatable = _action_get_repeatable |
| 84 |
argparse.Action.set_final = _action_set_final |
| 85 |
argparse.Action.get_final = _action_get_final |
| 86 |
argparse.ArgumentParser.alias = _parser_alias |
| 87 |
argparse.ArgumentParser.aliases = _parser_aliases |
| 88 |
argparse.ArgumentParser.get_aliases = _parser_get_aliases |
| 89 |
argparse.ArgumentParser.remove_help = _parser_remove_help |