Skip to content

Commit

Permalink
Lazy-open output files, and ensure they are always closed
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Jul 2, 2024
1 parent 172fa9f commit 0f58147
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions readme_renderer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main(cli_args: Optional[List[str]] = None) -> None:
help="README format (inferred from input file name or package)")
parser.add_argument('input', help="Input README file or package name")
parser.add_argument('-o', '--output', help="Output file (default: stdout)",
type=argparse.FileType('w'), default='-')
default='-')
args = parser.parse_args(cli_args)

content_format = args.format
Expand Down Expand Up @@ -55,7 +55,11 @@ def main(cli_args: Optional[List[str]] = None) -> None:
"`rst`, or `txt`)")
if rendered is None:
sys.exit(1)
print(rendered, file=args.output)
if args.output == "-":
print(rendered, file=sys.stdout)
else:
with open(args.output, "w") as fp:
print(rendered, file=fp)


if __name__ == '__main__':
Expand Down

0 comments on commit 0f58147

Please sign in to comment.