[elbe-devel] [PATCH 3/9] elbepack: cli: add helper add_argument_to_parser_or_function()
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Thu Aug 1 12:40:24 CEST 2024
For convenience provide a helper that can handle both argument
configuration mechanism used.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbepack/cli.py | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/elbepack/cli.py b/elbepack/cli.py
index 6633305b1bff..11e6274b98b8 100644
--- a/elbepack/cli.py
+++ b/elbepack/cli.py
@@ -31,6 +31,21 @@ def add_argument(*args, **kwargs):
return decorator
+def add_argument_to_parser_or_function(parser_or_func, *args, **kwargs):
+ """
+ Add arguments either to an :py:meth:`argparse.ArgumentParser` and similar objects,
+ or to a decoracted function, the same as :py:meth:`add_argument`.
+ """
+ if hasattr(parser_or_func, 'add_argument'):
+ return parser_or_func.add_argument(*args, **kwargs)
+
+ elif callable(parser_or_func):
+ return add_argument(*args, **kwargs)(parser_or_func)
+
+ else:
+ raise ValueError(parser_or_func)
+
+
def add_arguments_from_decorated_function(parser, f):
"""
Apply calls to :py:meth:`argparse.ArgumentParser.add_argument` recorded by
--
2.45.2
More information about the elbe-devel
mailing list