<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def bed(args):
 """ %prog bed contigfile Prints out the contigs and their associated reads. """

    p = OptionParser(main.__doc__)
    opts, args = p.parse_args(args)

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

    contigfile, = args
    bedfile = contigfile.rsplit(".", 1)[0] + ".bed"
    fw = open(bedfile, "w")
    c = ContigFile(contigfile)

    for rec in c.iter_records():
        for r in rec.reads:
            print(r.bedline, file=fw)

    logging.debug("File written to `{0}`.".format(bedfile))

    return bedfile