Add --config option to specify explicit configuration file path
Context
Context
Provide background to help others understand this issue.
The current configuration loading logic relies on recursive file tree traversal to find a file named .mdformat.toml or loading from Path.cwd() for stdin. This auto-discovery model is simple but inflexible for complex projects.
Describe the problem or need you'd like to address.
The reliance on a fixed filename and recursive search creates friction for users who manage configuration:
-
Pre-commit Integration: When using
mdformatin a.pre-commit-config.yamlhook alongside other tools (likemarkdownlintorhadolint), it is standard practice to store all configuration files in a shared directory (e.g.,.config/). Most tools support explicitly loading a config via a--configflag, butmdformatdoes not, forcing an exception to the directory structure. -
Centralized Configuration: Users cannot explicitly point
mdformatto a configuration file located outside the file's directory path (e.g., at the root level in a path like.config/mdformat.toml), complicating centralized maintenance.
The core problem is the lack of a mechanism to override the default config discovery path.
Proposal
Proposal
A simple and clear description of what you're proposing.
Introduce a new optional command-line flag, --config <path>, that allows a user to specify the absolute or relative path to a single configuration file.
This explicit path must override the default auto-discovery mechanism. If a path is provided via --config, mdformat should only attempt to load settings from that single file and ignore any .mdformat.toml files in the directory tree.
Ideas or constraints for how to implement this proposal
- The flag should be defined with
type=Pathinargparse. - The implementation should include a new internal function to load configuration directly from the provided path, separate from the cached recursive lookup function.
- If the file specified by
--configdoes not exist,mdformatmust print an error and exit with a non-zero code (e.g., 1), similar to how it handles formatting errors.
Important considerations to think about or discuss
-
Precedence: The
--configargument must take the highest precedence, overriding both recursive file discovery and any other implicit config loading (likepyproject.tomlif extensions are used). -
Compatibility: Since the config file format remains TOML (
.mdformat.tomlsyntax), compatibility with all existing options (wrap,number,plugin.*, etc.) is maintained.