def main():
    """Call do() and if it raises an exception then sys.exit() appropriately.

    This makes sure that any usage and error messages are printed correctly,
    and that the exit code is right.

    do() itself doesn't call sys.exit() because we want it to be callable from
    tests that check that it raises the right exception classes for different
    invalid inputs.

    """
    parser = make_parser()
    try:
        output = do(parser=parser)
    except CommandLineExit as err:
        sys.exit(err.code)
    except CommandLineError as err:
        if err.message:
            parser.error(err.message)
    sys.stdout.write(output)