This is a little bit more boiler plate, but the UX is much nicer.
Asserts produce race tracebacks while parser.error produces a clean
error message. This in turn makes it much more obvious that the
user made an error vs the tool crashing.
If gerrit_client was written using argparse, we could just set
required=True on the option, but it's still using optparse, and
that has no built-in support for required options.
Before:
$ ./gerrit_client.py addmessage
Traceback (most recent call last):
File ".../depot_tools/./gerrit_client.py", line 563, in <module>
sys.exit(main(sys.argv[1:]))
^^^^^^^^^^^^^^^^^^
File ".../depot_tools/./gerrit_client.py", line 555, in main
return dispatcher.execute(OptionParser(), argv)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../depot_tools/subcommand.py", line 254, in execute
return command(parser, args[1:])
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../depot_tools/./gerrit_client.py", line 409, in CMDaddMessage
(opt, args) = parser.parse_args(args)
^^^^^^^^^^^^^^^^^^^^^^^
File ".../depot_tools/./gerrit_client.py", line 547, in parse_args
assert options.host, "--host not defined."
^^^^^^^^^^^^
AssertionError: --host not defined.
After:
$ ./gerrit_client.py addmessage
Usage: gerrit_client.py addMessage [options] [args ...]
gerrit_client.py: error: --host is required.
Change-Id: I2f807628439e6399daaedc00cd42d160505ee4ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6172484
Auto-Submit: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>