copy and organize code from google flag package