I'm assuming you have the controller somehow mapped to "Config Menu" (normally mapped to tab).
Start mame with controller plugged in, press what you use to enter the menu, select the UI inputs, scroll down to "Config Menu", press enter, press what you use from the controller, wait until mame registers your input, press enter again, press tab on you keyboard, wait, then exit mame.
More details at
mameDev FAQ. Look at the OR and AND part near the end of this FAQ.