Bring a large pot of water to a boil and add a generous amount of salt once boiling.
Break the spaghetti in half and add to the boiling water, then reduce heat to a low simmer and cook until al dente, following package time as a guide.
While the pasta cooks, remove the thick ribs from the chard, slice the leaves crosswise into strips, then wash and dry (use a salad spinner or pat dry with towels).
Heat the olive oil in a large heavy frying pan over medium heat, add the minced garlic and red pepper flakes if using, and cook about 1 minute without letting the garlic brown.
Add the sliced chard all at once and cook 1–2 minutes, stirring occasionally, until the leaves are wilted to about half their volume.
Immediately remove about 1/4 cup of the hot pasta cooking water from the pot and add it to the pan with the chard, then reduce heat to low.
When the pasta is done and still slightly chewy, drain it in a colander.
Toss the hot spaghetti with the wilted chard in the frying pan (or combine in a bowl), using tongs or forks to mix thoroughly so the pasta is coated.
Add the coarsely grated Parmesan or Pecorino-Romano and toss again to combine, then season with fresh ground black pepper to taste and serve hot.