from __future__ import absolute_import, print_function
from readme_renderer.rst import render
import sys


if len(sys.argv) == 2:
    with open(sys.argv[1]) as fp:
        out = render(fp.read(), stream=sys.stderr)
        if out is not None:
            print(out)
        else:
            sys.exit(1)
else:
    print("Syntax: python -m readme_renderer <file.rst>", file=sys.stderr)

from __future__ import absolute_import, print_function
import argparse
from readme_renderer.rst import render
import sys


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="Renders a .rst README to HTML",
    )
    parser.add_argument('input', help="Input README file")
    parser.add_argument('-o', '--output', help="Output file (default: stdout)")
    args = parser.parse_args()
    if args.output:
        output_file = open(args.output, 'w')
    else:
        output_file = sys.stdout
    input_file = open(args.input)

    rendered = render(input_file.read(), stream=sys.stderr)
    if rendered is None:
        sys.exit(1)
    print(rendered, file=output_file)

Use `if __name__` and argparse
Use `if __name__` and argparse

  from __future__ import absolute_import, print_function
+ import argparse
  from readme_renderer.rst import render
  import sys
  
  
- if len(sys.argv) == 2:
-     with open(sys.argv[1]) as fp:
-         out = render(fp.read(), stream=sys.stderr)
-         if out is not None:
-             print(out)
+ if __name__ == '__main__':
+     parser = argparse.ArgumentParser(
+         description="Renders a .rst README to HTML",
+     )
+     parser.add_argument('input', help="Input README file")
+     parser.add_argument('-o', '--output', help="Output file (default: stdout)")
+     args = parser.parse_args()
+     if args.output:
+         output_file = open(args.output, 'w')
-         else:
+     else:
+         output_file = sys.stdout
+     input_file = open(args.input)
-             sys.exit(1)
- else:
-     print("Syntax: python -m readme_renderer <file.rst>", file=sys.stderr)
  
+     rendered = render(input_file.read(), stream=sys.stderr)
+     if rendered is None:
+         sys.exit(1)
+     print(rendered, file=output_file)
+ 
Use `if __name__` and argparse
## Code Before:
from __future__ import absolute_import, print_function
from readme_renderer.rst import render
import sys


if len(sys.argv) == 2:
    with open(sys.argv[1]) as fp:
        out = render(fp.read(), stream=sys.stderr)
        if out is not None:
            print(out)
        else:
            sys.exit(1)
else:
    print("Syntax: python -m readme_renderer <file.rst>", file=sys.stderr)

## Instruction:
Use `if __name__` and argparse
## Code After:
from __future__ import absolute_import, print_function
import argparse
from readme_renderer.rst import render
import sys


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="Renders a .rst README to HTML",
    )
    parser.add_argument('input', help="Input README file")
    parser.add_argument('-o', '--output', help="Output file (default: stdout)")
    args = parser.parse_args()
    if args.output:
        output_file = open(args.output, 'w')
    else:
        output_file = sys.stdout
    input_file = open(args.input)

    rendered = render(input_file.read(), stream=sys.stderr)
    if rendered is None:
        sys.exit(1)
    print(rendered, file=output_file)
