ned14 / pcpp

A C99 preprocessor written in pure Python
Other
215 stars 39 forks source link

Just wondering about main() in pcmd.py #73

Closed assarbad closed 1 year ago

assarbad commented 1 year ago

Hi folks,

wouldn't it make a bit more sense to implement it like this:

diff --git a/pcpp/pcmd.py b/pcpp/pcmd.py
index 5688e9a..e3bd93d 100644
--- a/pcpp/pcmd.py
+++ b/pcpp/pcmd.py
@@ -247,10 +247,9 @@ class CmdPreprocessor(Preprocessor):
             return True  # Pass through
         return super(CmdPreprocessor, self).on_comment(tok)

-def main():
-    p = CmdPreprocessor(sys.argv)
-    sys.exit(p.return_code)
+def main(*argv):
+    p = CmdPreprocessor(argv)
+    return p.return_code

 if __name__ == "__main__":
-    p = CmdPreprocessor(sys.argv)
-    sys.exit(p.return_code)
+    sys.exit(main(sys.argv))

If I understand it correctly the idea behind having a main() function is so it can be imported, correct? In this case it makes little sense for the part inside the if __name__ == "__main__": condition to parrot what the main() does. Instead it should call it.

And I think it would make sense to pass it the argument list, because it allows anyone reimplementing code based on pcmd to prepend or append arguments to whatever comes from the command line. Likewise it's sensible then not to call sys.exit() outright from main().

What do you think?