def parse_arguments(argv):
    """Consume known command line arguments."""

    parser = ExceptionRaisingArgumentParser(
        prog="canbus_explorer",
        description=canbus_explorer.__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument(
        "-v",
        "--version",
        action="version",
        version=canbus_explorer.__version__,
        help="Print the Canbus Explorer version and exit.",
    )

    parsed_args, remaining_argv = parser.parse_known_args(argv)
    return parsed_args, remaining_argv
class ValidateExceptionRaising(unittest.TestCase):
    """Verify that the ArgumentParser raises a checked exception
    instead of default behaviour of an unchecked exception (which
    terminates the runtime)."""

    def setUp(self):
        self._stderr = sys.stderr
        sys.stderr = DevNull()
        self.argparser = ExceptionRaisingArgumentParser()

    def tearDown(self):
        sys.stderr = self._stderr

    def test_can_create_arg_parser(self):
        self.assertIsInstance(self.argparser, argparse.ArgumentParser)

    def test_raises_checked_exceptions(self):
        self.argparser.add_argument("-r", "--required", required=True)
        self.assertRaises(ArgumentParsingError, self.argparser.parse_args)
 def setUp(self):
     self._stderr = sys.stderr
     sys.stderr = DevNull()
     self.argparser = ExceptionRaisingArgumentParser()