From d994ac9d6731e0e8090b748f329a25b0a4fd9bc7 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:03:54 +0100 Subject: [PATCH] Parse arguments to get address and port Signed-off-by: Yohann D'ANELLO --- main.py | 0 squinnondation/squinnondation.py | 27 ++++++++++++++++++++-- squinnondation/test/squinnondation_test.py | 25 ++++++++++++++++++++ tox.ini | 2 +- 4 files changed, 51 insertions(+), 3 deletions(-) mode change 100644 => 100755 main.py create mode 100644 squinnondation/test/squinnondation_test.py diff --git a/main.py b/main.py old mode 100644 new mode 100755 diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 7808aaa..a2e74b0 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -1,4 +1,27 @@ +# Copyright (C) 2020 by eichhornchen, ÿnérant +# SPDX-License-Identifier: GPL-3.0-or-later + +from argparse import ArgumentParser, Namespace + + class Squinnondation: + args: Namespace + client_address: str + client_port: int + + def parse_arguments(self) -> None: + parser = ArgumentParser(description="MIRC client.") + parser.add_argument('address', type=str, default="localhost", help="Address of the client.") + parser.add_argument('port', type=int, default=2500, help="Port of the client. Must be between 1024 and 65535.") + self.args = parser.parse_args() + + if not (1024 <= self.args.port <= 65535): + raise ValueError("The port must be between 1024 and 65535.") + + self.client_address = self.args.address + self.client_port = self.args.port + @staticmethod - def main() -> None: - print("Hello world!") + def main() -> None: # pragma: no cover + instance = Squinnondation() + instance.parse_arguments() diff --git a/squinnondation/test/squinnondation_test.py b/squinnondation/test/squinnondation_test.py new file mode 100644 index 0000000..1364565 --- /dev/null +++ b/squinnondation/test/squinnondation_test.py @@ -0,0 +1,25 @@ +import sys +import unittest + +from squinnondation.squinnondation import Squinnondation + + +class TestSquinnondation(unittest.TestCase): + def test_arguments(self): + """ + Parse arguments and check errors. + """ + squinnondation = Squinnondation() + + # No parameter was given + self.assertRaises(SystemExit, squinnondation.parse_arguments) + + # Wrong port + sys.argv = ["squinnondation", "localhost", "42"] + self.assertRaises(ValueError, squinnondation.parse_arguments) + + # Esnure that the parsing is correct + sys.argv = ["squinnondation", "localhost", "4242"] + squinnondation.parse_arguments() + self.assertEqual(squinnondation.client_address, "localhost") + self.assertEqual(squinnondation.client_port, 4242) diff --git a/tox.ini b/tox.ini index fd45328..a0ac22e 100644 --- a/tox.ini +++ b/tox.ini @@ -39,7 +39,7 @@ exclude = .cache, .eggs max-complexity = 15 -max-line-length = 80 +max-line-length = 120 import-order-style = google application-import-names = flake8 format = ${cyan}%(path)s${reset}:${yellow_bold}%(row)d${reset}:${green_bold}%(col)d${reset}: ${red_bold}%(code)s${reset} %(text)s