some refactoring of flag parsing