commit:     a01bd8fb3e280ad07110975fb857d980e024fb84
Author:     Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Fri Nov 28 22:19:15 2025 +0000
Commit:     Brian Harring <ferringb <AT> gmail <DOT> com>
CommitDate: Fri Nov 28 22:32:35 2025 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=a01bd8fb

refactor(Tool): slotting and tweaking main() so self.args can be eliminated.

Signed-off-by: Brian Harring <ferringb <AT> gmail.com>

 src/snakeoil/cli/input.py |  2 ++
 src/snakeoil/cli/tool.py  | 15 +++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/snakeoil/cli/input.py b/src/snakeoil/cli/input.py
index 7e63e33..0035a78 100644
--- a/src/snakeoil/cli/input.py
+++ b/src/snakeoil/cli/input.py
@@ -10,6 +10,8 @@ class NoChoice(KeyboardInterrupt):
     should do something reasonable.
     """
 
+    __slots__ = ()
+
 
 def userquery(prompt, out, err, responses=None, default_answer=None, limit=3):
     """Ask the user to choose from a set of options.

diff --git a/src/snakeoil/cli/tool.py b/src/snakeoil/cli/tool.py
index c7b9b98..8fc4562 100644
--- a/src/snakeoil/cli/tool.py
+++ b/src/snakeoil/cli/tool.py
@@ -17,6 +17,9 @@ from .exceptions import ExitException, find_user_exception
 class Tool:
     """Abstraction for commandline tools."""
 
+    # TODO: remove args once all downstreams have been fixed to pass 
*args/**kwargs down
+    __slots__ = ("out", "err", "parser", "options", "_outfile", "_errfile", 
"args")
+
     out: formatters.Formatter
     err: formatters.Formatter
 
@@ -154,9 +157,11 @@ class Tool:
             self.parser.error(exc)
         raise
 
-    def main(self):
+    def main(self, args=None):
         """Execute the main script function."""
-        exitstatus = -10
+        exitstatus: int = -10
+
+        args = self.args if args is None else args
 
         # ignore broken pipes
         signal(SIGPIPE, SIG_DFL)
@@ -175,7 +180,7 @@ class Tool:
                 exitstatus = func(self.options, self.out, self.err)
         except SystemExit as e:
             # handle argparse or other third party modules using sys.exit 
internally
-            exitstatus = e.code
+            exitstatus = e.code  # pyright: ignore[reportAssignmentType]
         except KeyboardInterrupt:
             self._errfile.write("keyboard interrupted- exiting")
             if self.parser.debug:
@@ -202,7 +207,9 @@ class Tool:
 class FormattingHandler(logging.Handler):
     """Logging handler printing through a formatter."""
 
-    def __init__(self, formatter):
+    __slots__ = ("out",)
+
+    def __init__(self, formatter: formatters.Formatter):
         logging.Handler.__init__(self)
         # "formatter" clashes with a Handler attribute.
         self.out = formatter

Reply via email to