Make main() return error code and use it, since easy-install CLI wrapper script expec...
authorNick Downing <nick.downing@lifx.co>
Sun, 26 Jan 2020 22:30:59 +0000 (09:30 +1100)
committerNick Downing <nick.downing@lifx.co>
Sun, 26 Jan 2020 23:14:07 +0000 (10:14 +1100)
ndcode/pitree/cli.py

index 7b3c990..aca10b3 100755 (executable)
 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
 import getopt
+import os
+import sys
 from ndcode.pitree import element
 from ndcode.pitree import generate_c
 from ndcode.pitree import generate_py
 from ndcode.pitree import lex_yy
 from ndcode.pitree import t_def
 from ndcode.pitree import y_tab
-import os
-import sys
+
+EXIT_SUCCESS = 0
+EXIT_FAILURE = 1
 
 def main():
   home_dir = os.path.dirname(__file__)
   try:
     opts, args = getopt.getopt(
       sys.argv[1:],
-      'o:pS:',
-      ['outfile=', 'python', 'skel=']
+      'eo:pS:',
+      ['install-element', 'outfile=', 'python', 'skel=']
     )
   except getopt.GetoptError as err:
     sys.stderr.write('{0:s}\n'.format(str(err)))
-    sys.exit(1)
+    return EXIT_FAILURE
 
+  install_element = False
   out_file = None
   python = False
   skel_file = None
   for opt, arg in opts:
-    if opt == '-e' or opt == '--element':
-      _element = True
+    if opt == '-e' or opt == '--install-element':
+      install_element = True
     elif opt == '-o' or opt == '--outfile':
       out_file = arg
     elif opt == '-p' or opt == '--python':
@@ -52,13 +56,25 @@ def main():
       skel_file = arg
     else:
       assert False
+
+  if install_element:
+    if skel_file is None:
+      skel_file = os.path.join(home_dir, 'skel/element.py')
+    if out_file is None:
+      out_file = 'element.py'
+    with open(skel_file) as fin:
+      with open(out_file, 'w+') as fout:
+       for line in fin:
+         fout.write(line)
+    return EXIT_SUCCESS
+
   if len(args) < 1:
     sys.stdout.write(
       'usage: {0:s} [options] defs.t\n'.format(
         sys.argv[0]
       )
     )
-    sys.exit(1)
+    return EXIT_FAILURE
   in_file = args[0]
 
   with open(in_file) as fin:
@@ -75,6 +91,7 @@ def main():
     skel_file,
     out_file
   )
+  return EXIT_SUCCESS
 
 if __name__ == '__main__':
-  main()
+  sys.exit(main())