Created
June 6, 2011 19:32
-
-
Save tebeka/1010916 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from itertools import imap, chain | |
| from sys import stdin, stdout | |
| def iterfile(filename): | |
| fo = stdin if filename == "-" else open(filename) | |
| return iter(fo) | |
| def main(argv=None): | |
| import sys | |
| from argparse import ArgumentParser | |
| argv = argv or sys.argv | |
| parser = ArgumentParser( | |
| description="Concatenate FILE(s),to standard output.") | |
| parser.add_argument("files", nargs="*", help="FILE(s) to print", | |
| metavar="FILE") | |
| parser.add_argument("-n", "--number", help="number all output lines", | |
| action="store_true", default=False) | |
| args = parser.parse_args(argv[1:]) | |
| template = "{0:3d} {1}" if args.number else "{1}" | |
| for lnum, line in enumerate(chain(*imap(iterfile, args.files or ["-"])), 1): | |
| stdout.write(template.format(lnum, line)) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment