1. import json
  2. import argparse
  3. def load_config(config_file, override_file=None):
  4. """Loads configuration data from a JSON file, applying overrides if provided."""
  5. try:
  6. with open(config_file, 'r') as f:
  7. config = json.load(f)
  8. except FileNotFoundError:
  9. print(f"Error: Configuration file not found: {config_file}")
  10. return {}
  11. except json.JSONDecodeError:
  12. print(f"Error: Invalid JSON in configuration file: {config_file}")
  13. return {}
  14. if override_file:
  15. try:
  16. with open(override_file, 'r') as f:
  17. override = json.load(f)
  18. config.update(override) #Apply overrides
  19. except FileNotFoundError:
  20. print(f"Warning: Override file not found: {override_file}")
  21. except json.JSONDecodeError:
  22. print(f"Warning: Invalid JSON in override file: {override_file}")
  23. return config
  24. def main():
  25. """Parses command-line arguments and loads the configuration."""
  26. parser = argparse.ArgumentParser(description="Local utility configuration.")
  27. parser.add_argument("-c", "--config", required=True, help="Path to the configuration file.")
  28. parser.add_argument("-o", "--override", help="Path to the override file (optional).")
  29. args = parser.parse_args()
  30. config = load_config(args.config, args.override)
  31. #Example usage: Print the config
  32. print(json.dumps(config, indent=4))
  33. if __name__ == "__main__":
  34. main()

Add your comment