Heat a drizzle of oil and small lump of butter in your soup pot set over medium heat, and saute the onions for about 15 minutes, until golden. Add the salt and sugar and continue to cook until they are a fairly deep golden. Add the sherry if you're using it, and cook for another minute, until it more or less evaporates. Otherwise add the wine, then the stock, thyme and bay leaf. Bring to a simmer and cook for 20-30 minutes. Pull out the bay leaf and thyme stems if there are any, and season the soup with salt and pepper.
Meanwhile, toast the bread slices, and rub them with a cut clove of garlic if you like. Turn on the broiler. Divide the soup among 2-3 ovenproof bowls set on a baking sheet and top each with a toast, then sprinkle with the grated cheeses. Broil for a minute or two, just until the cheese melts.