import copy
import dataclasses


def add_cfg_file_arg(cfg, cls):
    """
    If `cls` has a `cfg_file` field, we add this field to `cfg`.
    """
    cfg = copy.deepcopy(cfg)
    if "cfg_file" in cfg or cls is None:
        return cfg

    fields = dataclasses.fields(cls)
    params = {field.name: field.default for field in fields}
    if "cfg_file" in params:
        val = params["cfg_file"]
        cfg["cfg_file"] = val if val != dataclasses.MISSING else ""
    return cfg