[elbe-devel] [PATCH 03/18] elbepack: cli: unify decorator argument handling

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Aug 13 13:15:33 CEST 2024


Move the functionality to work with both decorated functions and
ArgumentParser objects directory into add_argument().
In the future all arguments should be applicable to both kinds of
objects.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/cli.py    | 18 ++++++++----------
 elbepack/config.py |  6 +++---
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/elbepack/cli.py b/elbepack/cli.py
index 8e8e5476ec78..bace38acbdfd 100644
--- a/elbepack/cli.py
+++ b/elbepack/cli.py
@@ -13,11 +13,7 @@ import typing
 _decorator_argparse_attr = '__' + __name__ + '.decorator_argparse'
 
 
-def add_argument(*args, **kwargs):
-    """
-    Record calls to :py:meth:`argparse.ArgumentParser.add_argument` to later be
-    applied by :py:func:`add_arguments_from_decorated_function`.
-    """
+def _add_argument_decorator(*args, **kwargs):
     def decorator(f):
         if not hasattr(f, _decorator_argparse_attr):
             setattr(f, _decorator_argparse_attr, [])
@@ -31,19 +27,21 @@ def add_argument(*args, **kwargs):
     return decorator
 
 
-def add_argument_to_parser_or_function(parser_or_func, *args, **kwargs):
+def add_argument(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`.
+    or to a decoracted function, to later be
+    applied by :py:func:`add_arguments_from_decorated_function`.
     """
     if hasattr(parser_or_func, 'add_argument'):
-        return parser_or_func.add_argument(*args, **kwargs)
+        parser_or_func.add_argument(*args, **kwargs)
+        return parser_or_func
 
     elif callable(parser_or_func):
-        return add_argument(*args, **kwargs)(parser_or_func)
+        return _add_argument_decorator(*args, **kwargs)(parser_or_func)
 
     else:
-        raise ValueError(parser_or_func)
+        return _add_argument_decorator(parser_or_func, *args, **kwargs)
 
 
 def add_arguments_from_decorated_function(parser, f):
diff --git a/elbepack/config.py b/elbepack/config.py
index 6503a0552300..552953aaeba2 100644
--- a/elbepack/config.py
+++ b/elbepack/config.py
@@ -4,7 +4,7 @@
 
 import os
 
-from elbepack.cli import add_argument_to_parser_or_function
+from elbepack.cli import add_argument
 
 
 def add_argument_soaptimeout(parser):
@@ -16,7 +16,7 @@ def add_argument_soaptimeout(parser):
 
 
 def add_argument_soapport(parser_or_func, arg='--port'):
-    return add_argument_to_parser_or_function(
+    return add_argument(
         parser_or_func,
         arg,
         dest='soapport',
@@ -56,7 +56,7 @@ def add_arguments_soapclient(parser):
 
 
 def add_argument_sshport(parser_or_func):
-    return add_argument_to_parser_or_function(
+    return add_argument(
         parser_or_func,
         '--sshport',
         type=int,

-- 
2.46.0



More information about the elbe-devel mailing list