See comment below. Would love the main logic to be wrapped up into a function (maybe using argparse since I'm sure it will end up having options) so that it can be tested and generally invoked from Python as well. I can merge and do the adaptation but if you would be willing to, that would be great too.

An example of the CLI style I like is https://github.com/dimagi/commcare-export/blob/master/commcare_export/cli.py (I wrote it).