
import json
import sys
import argparse

parser = argparse.ArgumentParser(description='Fetches a field from a single API in the catalog')
parser.add_argument('file', help='File to load')
parser.add_argument('id', help='ID of API to fetch')
parser.add_argument('field', help='Field to find and output')
args = parser.parse_args()

filename = sys.argv[1]
file = open(filename, "r") 
catalog = json.load(file)

query = [api.get(args.field) for api in catalog["apis"] if api["id"] == args.id]

if len(query) != 1:
  raise Exception(f"API {args.id} not found (or has duplicate definitions)")
elif not query[0]:
  raise Exception(f"API {args.id} has no field {args.field}")
else:
  print(query[0])


import json
import sys
import argparse

parser = argparse.ArgumentParser(description='Fetches a field from a single API in the catalog')
parser.add_argument('file', help='File to load')
parser.add_argument('id', help='ID of API to fetch')
parser.add_argument('field', help='Field to find and output')
parser.add_argument('--default', help='Default value to output if field is not present')
args = parser.parse_args()

filename = sys.argv[1]
file = open(filename, "r") 
catalog = json.load(file)

query = [api.get(args.field) for api in catalog["apis"] if api["id"] == args.id]

if len(query) != 1:
  raise Exception(f"API {args.id} not found (or has duplicate definitions)")
elif not query[0] and args.default:
  print(args.default)
elif not query[0]:
  raise Exception(f"API {args.id} has no field {args.field}")
else:
  print(query[0])

Allow a default value to be specified when fetching a field value
Allow a default value to be specified when fetching a field value

  
  import json
  import sys
  import argparse
  
  parser = argparse.ArgumentParser(description='Fetches a field from a single API in the catalog')
  parser.add_argument('file', help='File to load')
  parser.add_argument('id', help='ID of API to fetch')
  parser.add_argument('field', help='Field to find and output')
+ parser.add_argument('--default', help='Default value to output if field is not present')
  args = parser.parse_args()
  
  filename = sys.argv[1]
  file = open(filename, "r") 
  catalog = json.load(file)
  
  query = [api.get(args.field) for api in catalog["apis"] if api["id"] == args.id]
  
  if len(query) != 1:
    raise Exception(f"API {args.id} not found (or has duplicate definitions)")
+ elif not query[0] and args.default:
+   print(args.default)
  elif not query[0]:
    raise Exception(f"API {args.id} has no field {args.field}")
  else:
    print(query[0])
  
Allow a default value to be specified when fetching a field value
## Code Before:

import json
import sys
import argparse

parser = argparse.ArgumentParser(description='Fetches a field from a single API in the catalog')
parser.add_argument('file', help='File to load')
parser.add_argument('id', help='ID of API to fetch')
parser.add_argument('field', help='Field to find and output')
args = parser.parse_args()

filename = sys.argv[1]
file = open(filename, "r") 
catalog = json.load(file)

query = [api.get(args.field) for api in catalog["apis"] if api["id"] == args.id]

if len(query) != 1:
  raise Exception(f"API {args.id} not found (or has duplicate definitions)")
elif not query[0]:
  raise Exception(f"API {args.id} has no field {args.field}")
else:
  print(query[0])

## Instruction:
Allow a default value to be specified when fetching a field value
## Code After:

import json
import sys
import argparse

parser = argparse.ArgumentParser(description='Fetches a field from a single API in the catalog')
parser.add_argument('file', help='File to load')
parser.add_argument('id', help='ID of API to fetch')
parser.add_argument('field', help='Field to find and output')
parser.add_argument('--default', help='Default value to output if field is not present')
args = parser.parse_args()

filename = sys.argv[1]
file = open(filename, "r") 
catalog = json.load(file)

query = [api.get(args.field) for api in catalog["apis"] if api["id"] == args.id]

if len(query) != 1:
  raise Exception(f"API {args.id} not found (or has duplicate definitions)")
elif not query[0] and args.default:
  print(args.default)
elif not query[0]:
  raise Exception(f"API {args.id} has no field {args.field}")
else:
  print(query[0])
