Typo? if it is not, then I am lost.

#!/usr/bin/env python3.6

import sys
import json
import requests
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('url', help='URL to store the contents of')
parser.add_argument('filename', help='the filename to store the content under')
parser.add_argument('--content-type', '-c', # <-- Is the sam
default='html',
choices=['html', 'json'],
help='the content-type of the URL being requested')

args = parser.parse_args()

res = requests.get(args.url)

if res.status_code >= 400:
print(f"Error code received: {res.status_code}")
sys.exit(1)

if args.content_type == 'json': # <-- Is the same args
try:
content = json.dumps(res.json())
except ValueError:
print("Error: Content is not JSON")
sys.exit(1)
else:
content = res.text

with open(args.filename, 'w', encoding='UTF-8') as f:
f.write(content)
print(f"Content written to '{args.filename}'")

The two lines "# <- Is the same args" arguments are the same or args.content_type is a different argument. The code is from exercise: Utilizing Third-Party Packages Solution.
Thanks for helping.  
  • post-author-pic
    Keith T
    12-31-2018

    Hi Jose,


    You can't have a dash (`-`) in a variable or method name in Python, but they are commonly used for flags in CLIs so argparse converts all of the dashes into underscores.

    Hopefully, that helps explain what's going on.

Looking For Team Training?

Learn More