You can pass your own code, however one more option hope you are interested to explore
mysqld --validate-config
If no errors are found, the server terminates with an exit code of 0. If an error is found, the server displays a diagnostic message and terminates with an exit code of 1.
shell> mysqld --validate-config --no-such-option
2030-03-31T11:50:12.738919Z 0 [ERROR] [MY-000068] [Server] unknown option '--no-such-option'.
2030-03-31T11:50:12.738962Z 0 [ERROR] [MY-010119] [Server] Aborting
One more example
shell> mysqld --validate-config
2030-03-31T12:40:02.712141Z 0 [ERROR] [MY-000067] [Server] unknown variable 'tx_read_only=ON'.
2030-03-31T12:40:02.712178Z 0 [ERROR] [MY-010119] [Server] Aborting
If you want to test my.cnf which is on different path before applying, use like below.
shell> mysqld --defaults-file=./my.cnf-test --validate-config