Set a wide skillet over low heat and add the olive oil. When the oil is hot, add the onions, salt, and black pepper. Cook, stirring occasionally, for 35 to 40 minutes until deeply golden. If the onions start to stick, add a small splash of water and use your spoon to scrape up any browned bits from the bottom of the pan. Do not rush this step.
Add the garlic, capers, and optional red pepper flakes to the caramelized onions. Cook for 1 minute, just until fragrant. Add the butter and stir to combine.
Pour in the white wine and stir, scraping up any browned bits from the bottom of the pan. Simmer for about 1 minute, until the sauce tightens slightly.
Cook the Pasta. While the onions caramelize, bring a large pot of generously salted water to a boil and cook the spaghetti until just al dente. Before draining, reserve 1 cup of pasta cooking water.
Finish in the Skillet. Transfer the pasta directly into the skillet over medium-low heat. Add ¼ cup of reserved pasta water and toss vigorously. Add half the Parmesan and toss again, adding more pasta water a splash at a time until the sauce is glossy and coats every strand.
Finish and Serve. Adjust seasoning to taste, then top with the remaining Parmesan and fresh parsley. Serve immediately.