def make_argparser():
    """Argument parsing. Keep this together with main()"""

    if PY2:
        def unicode_arg(val):
            return val.decode(sys.getfilesystemencoding() or 'utf8')
    else:
        unicode_arg = str

    # Generic options
    p_main = ArgumentParser()
    generic = p_main.add_argument_group("generic arguments")
    generic.add_argument("-q", "--quiet",
                         action='store_true', dest='quiet', default=False,
                         help="silence information messages")
    generic.add_argument("-f", "--force",
                         action='store_true', dest='force', default=False,
                         help="Kill connections automatically if they prevent a command from executing. "
                              "Rename existing databases that are in the way.")
    generic.add_argument("--traceback", action='store_true', default=False,
                         help="print traceback when an error occurs")
    generic.add_argument("--host", metavar="HOST",
                         help="hostname of database server")
    generic.add_argument("-p", "--port", metavar="PORT", type=int,
                         help="port number of database server")

    sub = p_main.add_subparsers(metavar="COMMAND", dest='cmd')

    p_cp = sub.add_parser('cp', description=cmd_copy.__doc__,
                          help="Create a copy of a database within a server")
    p_mv = sub.add_parser('mv', description=cmd_move.__doc__,
                          help="Rename a database within a server")

    for p_cmd in (p_cp, p_mv):
        p_cmd.add_argument('src', metavar="SOURCE", type=unicode_arg,
                           help="source database name")
        p_cmd.add_argument('dest', metavar="DEST", type=unicode_arg,
                           help="destination database name")
        p_cmd.add_argument("--no-backup",
                           action='store_true', dest='no_backup', default=False,
                           help="drop existing DEST database if it exists")

    p_kill = sub.add_parser('kill', description=cmd_kill.__doc__,
                            help="Terminate active connections to a database")
    p_kill.add_argument('databases', metavar="DBNAME", type=unicode_arg, nargs='+',
                        help="kill connections on this database")

    p_reindex = sub.add_parser('reindex', description=cmd_reindex.__doc__,
                               help="Gracefully recreate an index")
    p_reindex.add_argument('-d', '--database', metavar="DB", type=unicode_arg,
                           help="apply reindex in this database")
    p_reindex.add_argument('indexes', metavar="IDXNAME", type=unicode_arg, nargs='+',
                           help="reindex these indexes")

    return p_main