import json


def parse_args(parser, config_flag="config_file"):
    """
    Parse arguments.
    Args:
        parser (argparse.ArgumentParser): argument parser
        config_flag (str): name of the arg key
            that gives the name of the config file.
    Returns:
        args (argparse.Namespace): arguments
    """

    # parse the arguments
    args = parser.parse_args()

    # if the config path is specified, then load
    # arguments from that file and apply the results
    # to `args`

    config_path = getattr(args, config_flag, None)
    if config_path is not None:
        with open(config_path, "r") as f:
            config_args = json.load(f)
        for key, val in config_args.items():
            if hasattr(args, key):
                setattr(args, key, val)
    return args