Skip to content

Commit 3d9d01f

Browse files
author
ArcHound
committed
feat: better file handling
1 parent 48e9d96 commit 3d9d01f

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎{{ cookiecutter.project_slug }}/{{ cookiecutter.project_slug }}.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ def new_func(ctx, *args, **kwargs):
7373
@click.command()
7474
{%- if cookiecutter.file_input == "Yes" %}
7575
@click.option(
76-
"--input-file", help="Input file [default: STDIN]", type=click.File("rt"), default=sys.stdin
76+
"--input-file",
77+
help="Input file [default: STDIN]",
78+
type=click.Path(readable=True, file_okay=True, dir_okay=False),
79+
default="-",
7780
)
7881
{%- endif %}
7982
{%- if cookiecutter.file_output == "Yes" %}
8083
@click.option(
81-
"--output-file", help="Output file [default: STDOUT]", type=click.File("at"), default=sys.stdout
84+
"--output-file",
85+
help="Output file [default: STDOUT]",
86+
type=click.Path(readable=True, file_okay=True, dir_okay=False),
87+
default="-",
8288
)
8389
{%- endif %}
8490
{%- if cookiecutter.use_requests == "Yes" %}
@@ -138,11 +144,11 @@ def main(
138144
# Your script starts here!
139145
# ======================================================================
140146
{%- if cookiecutter.file_input == "Yes" %}
141-
if input_file.isatty():
147+
if input_file == "-" and sys.stdin.isatty():
142148
logging.critical("Input from stdin which is a tty - aborting")
143149
return 128
144-
with input_file:
145-
in_data = input_file.read()
150+
with click.open_file(input_file, "r") as f:
151+
in_data = f.read()
146152

147153
{%- endif %}
148154
{%- if cookiecutter.use_requests == "Yes" %}
@@ -156,6 +162,10 @@ def main(
156162
{{ cookiecutter.service_name }}_session.proxies.update(proxies)
157163
{{ cookiecutter.service_name }}_session.verify = False
158164

165+
{%- endif %}
166+
{%- if cookiecutter.file_output == "Yes" %}
167+
with click.open_file(output_file, "w") as f:
168+
f.write("Hello, world!")
159169
{%- endif %}
160170
return 0
161171

0 commit comments

Comments
 (0)