Go through argv and find any special command-line flags
    that set environment variables that affect multiple modules.

    If any of them are given, then set them in this process's
    environment and re-exec the process without the CLI flags.

    argv should be like sys.argv:  argv[0] is the binary

    Does not return on re-exec.
    Returns {args} on success
    Returns False on error.

def check_and_set_envars( argv ):
    """
    Go through argv and find any special command-line flags
    that set environment variables that affect multiple modules.

    If any of them are given, then set them in this process's
    environment and re-exec the process without the CLI flags.

    argv should be like sys.argv:  argv[0] is the binary

    Does not return on re-exec.
    Returns {args} on success
    Returns False on error.
    """
    special_flags = {
        '--debug': {
            'arg': False,
            'envar': 'BLOCKSTACK_DEBUG',
            'exec': True,
        },
        '--verbose': {
            'arg': False,
            'envar': 'BLOCKSTACK_DEBUG',
            'exec': True,
        },
        '--testnet-id': {
            'arg': True,
            'envar': 'BLOCKSTACK_TESTNET_ID',
            'exec': True,
        },
        '--testnet-start-block': {
            'arg': True,
            'envar': 'BLOCKSTACK_TESTNET_START_BLOCK',
            'exec': True,
        },
        '--working_dir': {
            'arg': True,
            'argname': 'working_dir',
            'exec': False,
        },
        '--working-dir': {
            'arg': True,
            'argname': 'working_dir',
            'exec': False,
        },
    }

    cli_envs = {}
    cli_args = {}
    new_argv = []
    stripped_argv = []

    do_exec = False
    i = 0
    while i < len(argv):

        arg = argv[i]
        value = None

        for special_flag in special_flags.keys():

            if not arg.startswith(special_flag):
                continue

            if special_flags[special_flag]['arg']:
                if '=' in arg:
                    argparts = arg.split("=")
                    value_parts = argparts[1:]
                    arg = argparts[0]
                    value = '='.join(value_parts)

                elif i + 1 < len(argv):
                    value = argv[i+1]
                    i += 1

                else:
                    print >> sys.stderr, "%s requires an argument" % special_flag
                    return False

            else:
                # just set
                value = "1"

            break

        i += 1

        if value is not None:
            if 'envar' in special_flags[special_flag]:
                # recognized
                cli_envs[ special_flags[special_flag]['envar'] ] = value
            
            if 'argname' in special_flags[special_flag]:
                # recognized as special argument
                cli_args[ special_flags[special_flag]['argname'] ] = value
                new_argv.append(arg)
                new_argv.append(value)

            if special_flags[special_flag]['exec']:
                do_exec = True

        else:
            # not recognized
            new_argv.append(arg)
            stripped_argv.append(arg)

    if do_exec:
        # re-exec
        for cli_env, cli_env_value in cli_envs.items():
            os.environ[cli_env] = cli_env_value

        if os.environ.get("BLOCKSTACK_DEBUG") is not None:
            print "Re-exec as {}".format(" ".join(new_argv))

        os.execv(new_argv[0], new_argv)

    log.debug("Stripped argv: {}".format(' '.join(stripped_argv)))
    return cli_args, stripped_argv