diff options
-rwxr-xr-x | mkheader.py | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/mkheader.py b/mkheader.py index 89c3620..dcd374e 100755 --- a/mkheader.py +++ b/mkheader.py @@ -9,11 +9,12 @@ def usage(): -e launch editor after creating files. Uses editor from $EDITOR envvar. -f overwrite existing files + -s print generated to stdout instead of writing it to file -h show this help and exit""" % (os.path.basename(sys.argv[0])) def main(): try: - opts, args = getopt.getopt(sys.argv[1:], "efh") + opts, args = getopt.getopt(sys.argv[1:], "efsh") except getopt.GetoptError, err: print str(err) usage() @@ -25,36 +26,47 @@ def main(): overwrite = False launch_editor = False + to_stdout = False for o, a in opts: if o == "-e": launch_editor = True elif o == "-f": overwrite = True + elif o == "-s": + to_stdout = True elif o == "-h": usage() sys.exit() else: assert False, "unhandled option" + if to_stdout and launch_editor: + print "Options -e and -s cannot be used together" + sys.exit(2) + p = re.compile('[a-zA-Z0-9._-]+\.h') for f in args: if not p.match(f): print "Invalid header filename '%s'. Header not created" % f continue - if not overwrite and os.path.exists(f): + if not to_stdout and not overwrite and os.path.exists(f): print "Header file '%s' already exists. Use -f to override." % f continue - print "Creating %s..." % f q = re.compile('[.-]') define = q.sub('_', f.upper()) - f = open(f, 'w') - f.write('#ifndef _%s_\n' % define) - f.write('#define _%s_\n\n' % define) - f.write('/* TODO: Your code here */\n\n') - f.write('#endif /* _%s_ */' % define) - f.close() + if to_stdout: + fd = sys.stdout + else: + print "Creating %s..." % f + fd = open(f, 'w') + fd.write('#ifndef _%s_\n' % define) + fd.write('#define _%s_\n\n' % define) + fd.write('/* TODO: Your code here */\n\n') + fd.write('#endif /* _%s_ */' % define) + if not to_stdout: + fd.close() if launch_editor: editor = os.getenv("EDITOR") |