summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmkheader.py55
1 files changed, 44 insertions, 11 deletions
diff --git a/mkheader.py b/mkheader.py
index b28e1de..89c3620 100755
--- a/mkheader.py
+++ b/mkheader.py
@@ -1,25 +1,43 @@
#!/usr/bin/env python
-import os
-import sys
+import os, sys
+import getopt
import re
def usage():
- print """usage: %s header...""" % (os.path.basename(sys.argv[0]))
+ print """usage: %s [OPTION] FILE...
+
+ -e launch editor after creating files. Uses editor from $EDITOR envvar.
+ -f overwrite existing files
+ -h show this help and exit""" % (os.path.basename(sys.argv[0]))
def main():
- if len(sys.argv) < 2:
+ try:
+ opts, args = getopt.getopt(sys.argv[1:], "efh")
+ except getopt.GetoptError, err:
+ print str(err)
+ usage()
+ sys.exit(2)
+
+ if len(args) < 1:
usage()
- sys.exit()
+ sys.exit(2)
overwrite = False
- first = 1
- if len(sys.argv) >= 2 and sys.argv[1] == '-f':
- overwrite = True
- first = 2
+ launch_editor = False
+ for o, a in opts:
+ if o == "-e":
+ launch_editor = True
+ elif o == "-f":
+ overwrite = True
+ elif o == "-h":
+ usage()
+ sys.exit()
+ else:
+ assert False, "unhandled option"
p = re.compile('[a-zA-Z0-9._-]+\.h')
- for f in sys.argv[first:]:
+ for f in args:
if not p.match(f):
print "Invalid header filename '%s'. Header not created" % f
continue
@@ -38,7 +56,22 @@ def main():
f.write('#endif /* _%s_ */' % define)
f.close()
- print "Finished"
+ if launch_editor:
+ editor = os.getenv("EDITOR")
+ if editor is None:
+ editor = "vi"
+
+ # Fork an editor for each file as not all editors support
+ # editing multiple files at once (e.g. splitted view in vim)
+ for f in args:
+ print "Editing header '%s' with '%s'" % (f, editor)
+
+ pid = os.fork()
+ if pid == 0: # child process
+ os.execvp(editor, [editor, f])
+ sys.exit(0)
+ else: # parent process
+ os.waitpid(pid, 0)
if __name__ == '__main__':
main()