Clean up options parsing and logging code

* Refactor some of the code out into easily-documentable functions
* Simplify debug logging
* Add more command-line option checking
* Add better command-line option error messages