Rewrite CLI options handling and output

I needed more stable and extensible option handling so argparse replaces the custom option handling code. And I also replaced the various `print` and `sys.stderr.write` calls with the logging module.