main.cpp: simplify by moving flag parsing logic into a function