Refactor flag parsing code.