Bring a large pot of salted water to a rolling boil and cook the spaghetti according to package directions until just before your preferred doneness, reserving about 1/2 cup (125 ml) of the pasta cooking water before draining.
While the pasta cooks, heat the olive oil in a non-stick frying pan over medium heat.
Add the crushed or finely chopped garlic and chopped anchovy fillets to the pan and cook briefly, stirring, until the garlic just starts to turn golden and the anchovies have broken down into the oil, about 1–2 minutes.
Pour in the tinned chopped tomatoes, then add the capers, black olives, half the chopped parsley, and the chopped basil or basil pesto. Stir in the dried chili flakes or the fresh chopped chilli.
Simmer the sauce gently for a few minutes until it slightly thickens; if it becomes too thick, loosen it with up to 1/4 cup of the reserved pasta water.
Add the drained spaghetti to the pan with the sauce and toss well over medium heat, adding more pasta water as needed, until the pasta is cooked to your liking and well coated, about 2–3 minutes.
Scatter the remaining chopped parsley over the pasta and transfer to bowls.
Serve immediately with freshly grated Parmesan and optional finely chopped fresh tomato.