def main(argv=None):

    if not argv:
        argv = sys.argv

    # setup command line parser
    parser = E.OptionParser(version="%prog version: $Id$",
                            usage=globals()["__doc__"])

    parser.add_option("--scripts", dest="rename_scripts", type="string",
                      help="rename scripts")

    parser.add_option("--options", dest="rename_options", type="string",
                      help="rename command line options")

    parser.add_option("--split-prefix", dest="split_prefix",
                      type="string",
                      help="move scripts with prefix to subdirectory")

    parser.add_option("--suffix", dest="suffixes", action="append",
                      type="string",
                      help="file suffixes to use.")

    parser.add_option("-n", "--dry-run", dest="dry_run",
                      action="store_true",
                      help="dry run, do not implement any changes")

    parser.add_option("--restrict-regex", dest="regex_restrict", type="string",
                      help="regular expression to restrict refactoring to")

    parser.add_option("-d", "--directories", dest="dirs", action="append",
                      type="string",
                      help="directories to change files in [%defaul]")

    parser.set_defaults(
        rename_scripts=None,
        rename_options=None,
        split_prefix=None,
        scriptsdir="scripts",
        dirs=[],
        suffixes=[],
        dry_run=False,
        regex_restrict=None,
    )

    # add common options (-h/--help, ...) and parse command line
    (options, args) = E.Start(parser, argv=argv)

    if len(options.suffixes) == 0:
        raise ValueError("please supply --suffixes")

    if len(options.dirs) == 0:
        raise ValueError("please supply --directories")

    scriptsdir = options.scriptsdir

    counter = E.Counter()

    map_old2new = {}

    if options.rename_scripts or options.split_prefix:

        if options.rename:
            with IOTools.openFile(options.rename_scripts, "r") as inf:
                for line in inf:
                    if line.startswith("#"):
                        continue
                    if line.startswith("old"):
                        continue
                    try:
                        old, new = line[:-1].split("\t")
                    except ValueError:
                        continue
                    if not os.path.exists(os.path.join(scriptsdir, old)):
                        E.warn("%s does not exist - no renaming" % old)
                        continue
                    map_old2new[old] = new

        elif options.split_prefix:
            if not os.path.exists(os.path.join(scriptsdir,
                                               options.split_prefix)):
                E.warn("destination %s does not exist - no renaming" %
                       options.split_prefix)
                return

            scripts = glob.glob("%s/%s_*.py" % (scriptsdir,
                                                options.split_prefix))
            if len(scripts) == 0:
                E.info("nothing to change")
                return

            for script in scripts:
                scriptname = os.path.basename(script)
                newname = scriptname[len(options.split_prefix) + 1:]
                map_old2new[scriptname] = "%s/%s" % (options.split_prefix,
                                                     newname)

        if len(map_old2new) == 0:
            E.info("nothing to change")
            return

        for old, new in map_old2new.items():
            statement = "git mv %(scriptsdir)s/%(old)s %(scriptsdir)s/%(new)s" % locals()
            counter.renamed += 1

            if options.dry_run:
                E.info(statement)
            else:
                E.run(statement)

        updateFiles(options.dirs,
                    map_old2new, counter,
                    suffixes=options.suffixes,
                    dry_run=options.dry_run)

    elif options.rename_options:
        # read refactoring guides
        table = pandas.read_csv(
            IOTools.openFile(options.rename_options),
            sep="\t")

        # select all options that need to renamed
        selected = table[table.action == "rename"]

        # check if all are unique
        checkUnique(selected["option"])
        checkOverlap(selected["option"],
                     selected["alternative"])

        # build map adding "--" prefix
        map_old2new = dict(zip(
            ["--%s" % x for x in selected["option"]],
            ["--%s" % x for x in selected["alternative"]]))

        updateFiles(options.dirs, map_old2new, counter,
                    suffixes=options.suffixes,
                    regex_restrict=options.regex_restrict,
                    dry_run=options.dry_run)

    E.info(str(counter))

    # write footer and output benchmark information.
    E.Stop()
script main.
dataset/ETHPy150Open CGATOxford/cgat/refactor/cgat_refactor.py/main