Gracefully handle missing argument in pymongo.uri_parser __main__ block

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Unresolved
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: greenerbuild
    • None
    • None
    • Python Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Context

      The main block in pymongo/uri_parser.py reads sys.argv[1] without checking that an argument was supplied. Running python -m pymongo.uri_parser with no URI raises an unhandled IndexError and dumps a traceback instead of printing usage.

      Definition of done

      Change to:

      if _name_ == "_main_":
          import pprint

          if len(sys.argv) < 2:
              print("usage: python -m pymongo.uri_parser <uri>")  # noqa: T201
              sys.exit(1)
          try:
              pprint.pprint(parse_uri(sys.argv[1]))  # noqa: F405, T203
          except InvalidURI as exc:
              print(exc)  # noqa: T201
          sys.exit(0)

      Then add a regression test (test_missing_arg_prints_usage) asserting the usage message and a non-zero exit code.

      Pitfalls

      NA

            Assignee:
            Unassigned
            Reporter:
            Alex Clark
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: