def parse_config_args()

in projects/imagen-object-changer/imagen_object_changer.py [0:0]


def parse_config_args(config_file):
    """Parses config.ini and command line args.

    Parses first the .ini -style config file, and then command line args.
    CLI args overwrite default values from the config file.

    Args:
      config_file: Path to config file

    Returns:
      Configparser config

    Raises:
      None
    """
    # Create a config parser object
    config = configparser.ConfigParser()
    config["DEFAULT"] = {
        "mask": "mask.png",
        "invert_mask": "False",
        "output_json": "output.json",
    }
    config["parameters"] = {}

    # Create an argument parser
    parser = argparse.ArgumentParser()

    # Read the configuration file
    read_config = config.read(config_file)

    if not read_config:
        print("{} not found. Using command line args only".format(config_file))
        # Add arguments for each configuration value using hardcoded defaults
        parser.add_argument(
            "--mask",
            default=config["DEFAULT"]["mask"],
            type=str,
            help="Output mask file",
        )
        parser.add_argument(
            "--invert_mask",
            default=config["DEFAULT"]["invert_mask"],
            type=str,
            help="Invert mask; replace the background",
        )
        parser.add_argument(
            "--output_json",
            default=config["DEFAULT"]["output_json"],
            type=str,
            help="Output JSON file name for GenAI response",
        )
        parser.add_argument(
            "--input", required=True, type=str, help="Original image file"
        )
        parser.add_argument(
            "--label",
            required=True,
            type=str,
            help="Object to detect e.g car | cat | tree",
        )
        parser.add_argument(
            "--prompt",
            required=True,
            type=str,
            help="Imagen prompt for image generation",
        )
        parser.add_argument(
            "--project_id",
            required=True,
            type=str,
            help="Google Cloud Project ID string",
        )
    else:
        # Add arguments for each cfg value using read file for fallback defaults
        parser.add_argument(
            "--input",
            default=config["parameters"]["input"],
            type=str,
            help="Original image file",
        )
        parser.add_argument(
            "--label",
            default=config["parameters"]["label"],
            type=str,
            help="Object to detect e.g car | cat",
        )
        parser.add_argument(
            "--prompt",
            default=config["parameters"]["prompt"],
            type=str,
            help="Imagen prompt for image generation",
        )
        parser.add_argument(
            "--project_id",
            default=config["parameters"]["project_id"],
            type=str,
            help="Google Cloud Project ID string",
        )
        parser.add_argument(
            "--mask",
            default=config["parameters"]["mask"],
            type=str,
            help="Output mask file",
        )
        parser.add_argument(
            "--invert_mask",
            default=config["parameters"]["invert_mask"],
            type=str,
            help="Invert mask; replace the background",
        )
        parser.add_argument(
            "--output_json",
            default=config["parameters"]["output_json"],
            type=str,
            help="Output JSON file name for GenAI response",
        )

    # Parse the arguments
    args = parser.parse_args()

    # Update the configuration values with the command line arguments
    for arg in vars(args):
        config["parameters"][arg] = getattr(args, arg)

    print(dict(config["parameters"]))

    # Check for required values
    if not config["parameters"]["project_id"]:
        print("error: the following arguments are required: --project_id")
        exit(1)
    return config