1. import json
  2. import argparse
  3. def format_config(config_file, override_file):
  4. """
  5. Formats a configuration file with manual overrides.
  6. Args:
  7. config_file (str): Path to the main configuration file (JSON).
  8. override_file (str): Path to the override configuration file (JSON).
  9. Returns:
  10. str: Formatted configuration string.
  11. """
  12. try:
  13. with open(config_file, 'r') as f:
  14. config = json.load(f)
  15. except FileNotFoundError:
  16. return f"Error: Config file '{config_file}' not found."
  17. except json.JSONDecodeError:
  18. return f"Error: Invalid JSON in '{config_file}'."
  19. try:
  20. with open(override_file, 'r') as f:
  21. overrides = json.load(f)
  22. except FileNotFoundError:
  23. return f"Error: Override file '{override_file}' not found."
  24. except json.JSONDecodeError:
  25. return f"Error: Invalid JSON in '{override_file}'."
  26. # Merge overrides into the base config
  27. for key, value in overrides.items():
  28. config[key] = value
  29. # Format the configuration as a string
  30. formatted_config = json.dumps(config, indent=4)
  31. return formatted_config
  32. if __name__ == "__main__":
  33. parser = argparse.ArgumentParser(description="Format configuration files with overrides.")
  34. parser.add_argument("config_file", help="Path to the main configuration file (JSON)")
  35. parser.add_argument("override_file", help="Path to the override configuration file (JSON)")
  36. args = parser.parse_args()
  37. formatted_output = format_config(args.config_file, args.override_file)
  38. print(formatted_output)

Add your comment