🎨 Custom Code Formatters¶
Create your own custom code formatters for specialized formatting needs.
🚀 Usage¶
Pass the module path containing your formatter class:
datamodel-codegen --input {your_input_file} --output {your_output_file} --custom-formatters "{path_to_your_module}.your_module"
📝 Example¶
1️⃣ Create your formatter¶
your_module.py¶
from datamodel_code_generator.format import CustomCodeFormatter
class CodeFormatter(CustomCodeFormatter):
def apply(self, code: str) -> str:
# Apply your custom formatting here
# For example, add a custom header comment:
header = "# This code was formatted by custom formatter\n"
return header + code
2️⃣ Use your formatter¶
🔧 Passing Arguments¶
You can pass keyword arguments to your custom formatter using --custom-formatters-kwargs:
datamodel-codegen --input schema.json --output model.py \
--custom-formatters "mypackage.your_module" \
--custom-formatters-kwargs '{"line_length": 100}'
📖 See Also¶
- 🖥️ CLI Reference:
--custom-formatters- Detailed CLI option documentation - 🔧 CLI Reference:
--custom-formatters-kwargs- Pass arguments to custom formatters - 🖌️ Formatting - Built-in code formatting with black and isort