def build_sample_flag_data(checkpoint_path, is_gather, has_common, has_misc, missing_gather, missing_run, missing_mandatory):
    """ This function serves two purposes;
            (1) Generate the dictionary that would be read from YAML under various conditions (including erroroneous ones)
            (2) Generate the expected output under those conditions
    """
    # Dictionary sections, toggled using parameters
    misc = {
        "misc_flag_1": {
            "key": "--misc-flag",
            "value": "misc value"
        },
        "misc_flag_no_value": {
            "key": "--another-misc-flag"
        },
    }

    common = {
        "log_steps": {
            "key": "--steps-per-log",
            "value": 1
        },
        "config": {
            "key": "--config",
            "value": "configs/demo.json"
        }
    }

    gather_mandatory = {
        "checkpoint_output": {
            "set_in_script": True,
            "key": "--checkpoint-dir"
        }
    }

    run_mandatory = {
        "checkpoint_input": {
            "key": "--onnx-checkpoint",
            "set_in_script": True
        },
        "start_step": {
            "key": "--continue-training-from-step",
            "set_in_script": True
        },
        "run_for": {
            "key": "--epochs",
            "set_in_script": True
        }
    }

    def add_kv(expected, d):
        for elem in d.values():
            expected[elem["key"]] = elem["value"] if "value" in elem else None

    # Build the flags dictionary and populate the expected values
    test_flags = {}
    expected_kv = {}

    gather_all = {} if missing_mandatory else {**gather_mandatory}
    run_all = {} if missing_mandatory else {**run_mandatory}

    if is_gather:
        add_kv(expected_kv, gather_mandatory)
    else:
        add_kv(expected_kv, run_mandatory)

    if has_misc == "gather" or has_misc == "both":
        gather_all["misc"] = misc
        if is_gather:
            add_kv(expected_kv, misc)
    if has_misc == "run" or has_misc == "both":
        run_all["misc"] = misc
        if not is_gather:
            add_kv(expected_kv, misc)

    if has_common:
        test_flags["common"] = common
        add_kv(expected_kv, common)

    if not missing_gather:
        test_flags["gather"] = gather_all

    if not missing_run:
        test_flags["run"] = run_all

    return test_flags, expected_kv