import argparse
import os
from .lazy import LazyConfig


def parse_args_and_configs(program_description: str = ""):
    parser = argparse.ArgumentParser(description=program_description)
    parser.add_argument(
        "config_file",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "opts",
        help="Modify config options using the command-line 'KEY VALUE' pairs",
        default=None,
        nargs=argparse.REMAINDER,
    )
    args = parser.parse_args()

    if not os.path.exists(args.config_file):
        raise FileNotFoundError(f"Config file not found: {args.config_file}")
    cfg = LazyConfig.load(args.config_file)

    if args.opts:
        cfg.merge_with_dotlist(args.opts)

    return cfg