Parse arguments to get address and port

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:03:54 +01:00
parent 272d0c25d6
commit d994ac9d67
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 51 additions and 3 deletions

0
main.py Normal file → Executable file
View File

View File

@ -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()

View File

@ -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)

View File

@ -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