import json
import argparse
def format_config(config_file, override_file):
"""
Formats a configuration file with manual overrides.
Args:
config_file (str): Path to the main configuration file (JSON).
override_file (str): Path to the override configuration file (JSON).
Returns:
str: Formatted configuration string.
"""
try:
with open(config_file, 'r') as f:
config = json.load(f)
except FileNotFoundError:
return f"Error: Config file '{config_file}' not found."
except json.JSONDecodeError:
return f"Error: Invalid JSON in '{config_file}'."
try:
with open(override_file, 'r') as f:
overrides = json.load(f)
except FileNotFoundError:
return f"Error: Override file '{override_file}' not found."
except json.JSONDecodeError:
return f"Error: Invalid JSON in '{override_file}'."
# Merge overrides into the base config
for key, value in overrides.items():
config[key] = value
# Format the configuration as a string
formatted_config = json.dumps(config, indent=4)
return formatted_config
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Format configuration files with overrides.")
parser.add_argument("config_file", help="Path to the main configuration file (JSON)")
parser.add_argument("override_file", help="Path to the override configuration file (JSON)")
args = parser.parse_args()
formatted_output = format_config(args.config_file, args.override_file)
print(formatted_output)
Add your comment