Last active
August 29, 2015 14:01
-
-
Save mitsuhiko/f315e39c9e9e1ccf483f to your computer and use it in GitHub Desktop.
This file contains 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
import click | |
@click.command() | |
@click.argument('input', type=click.File('rb'), nargs=-1) | |
@click.argument('output', type=click.File('wb')) | |
def cli(input, output): | |
for f in input: | |
while True: | |
chunk = f.read(1024) | |
if not chunk: | |
break | |
output.write(chunk) | |
output.flush() | |
if __name__ == '__main__': | |
cli() |
This file contains 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
"""Inout | |
Usage: | |
inout.py <output> <input>... | |
Options: | |
--help Show this message and exit. | |
""" | |
import sys | |
import docopt | |
def cli(): | |
x = docopt.docopt(__doc__) | |
if x['<output>'] == '-': | |
out = sys.stdout | |
close_out = False | |
else: | |
try: | |
out = open(x['<output>']) | |
except OSError as e: | |
print 'error: %s' % e | |
sys.exit(1) | |
close_out = True | |
try: | |
for filename in x['<input>']: | |
if filename == '-': | |
f = sys.stdin | |
close_f = False | |
else: | |
try: | |
f = open(x['<input>']) | |
except OSError as e: | |
print 'error: %s' % e | |
sys.exit(1) | |
close_f = True | |
try: | |
while True: | |
chunk = f.read(1024) | |
if not chunk: | |
break | |
out.write(chunk) | |
out.flush() | |
finally: | |
if close_f: | |
f.close() | |
finally: | |
if close_out: | |
out.close() | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment