Mercurial > hg > TextShaper
changeset 10:466386702968
more stuff
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 18 Jan 2014 09:28:55 -0800 |
parents | 71fb16088d54 |
children | 0d0db0d79bfd |
files | textshaper/indent.py |
diffstat | 1 files changed, 25 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/textshaper/indent.py Fri Jan 17 18:17:59 2014 -0800 +++ b/textshaper/indent.py Sat Jan 18 09:28:55 2014 -0800 @@ -39,12 +39,35 @@ index = indentation retval.append(line[index:]) + return retval + + def main(args=sys.argv[1:]): - usage = '%prog [options]' + # parse command line + usage = '%prog [options] [file] [file2] [...]' + description = """indent files or stdin if no files given""" parser = optparse.OptionParser(usage=usage, description=__doc__) - options, args = parser.parse_args(args) + parser.add_option('-o', '--output', dest='output', + help="output file or stdout if not given") + options, files = parser.parse_args(args) + # input from files or stdin + if files: + missing = [not os.path.exists(filename) for filename in files] + if missing: + parser.error("File(s) not found: %s" % ', '.join(missing)) + def _files(): + for filename in files: + with open(f, 'r') as f: + yield f + else: + def _files(): + yield sys.stdin + + # process input + for f in _files(): + print f if __name__ == '__main__': main()