"""Parsing keyword arguments from a command line option."""

import argparse
import ast


class ParseKwargs(argparse.Action):
    """Custom action to parse keyword arguments from command line.

    Same as the pytorch-image-models function, but auto-generated by
    github copilot!
    """

    def __call__(self, parser, namespace, values, option_string=None):
        kw = {}
        for value in values:
            key, value = value.split("=")
            try:
                kw[key] = ast.literal_eval(value)
            except ValueError:
                kw[key] = str(
                    value
                )  # fallback to string (avoid need to escape on command line)
        setattr(namespace, self.dest, kw)
