Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsuhiko/f315e39c9e9e1ccf483f to your computer and use it in GitHub Desktop.
Save mitsuhiko/f315e39c9e9e1ccf483f to your computer and use it in GitHub Desktop.
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()
"""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