<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def format(args):
 """ %prog format oldagpfile newagpfile Reformat AGP file. --switch will replace the ids in the AGP file. """

    from jcvi.formats.base import DictFile

    p = OptionParser(format.__doc__)
    p.add_option("--switchcomponent",
                 help="Switch component id based on")
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    oldagpfile, newagpfile = args
    switchcomponent = opts.switchcomponent
    if switchcomponent:
        switchcomponent = DictFile(switchcomponent)

    agp = AGP(oldagpfile)
    fw = open(newagpfile, "w")
    nconverts = 0
    for i, a in enumerate(agp):
        if not a.is_gap and a.component_id in switchcomponent:
            oldid = a.component_id
            newid = switchcomponent[a.component_id]
            a.component_id = newid
            logging.debug("Covert {0} to {1} on line {2}".\
                        format(oldid, newid, i+1))
            nconverts += 1
        print(a, file=fw)

    logging.debug("Total converted records: {0}".format(nconverts))