Parse arguments to get address and port
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
@ -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()
|
||||
|
25
squinnondation/test/squinnondation_test.py
Normal file
25
squinnondation/test/squinnondation_test.py
Normal 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)
|
Reference in New Issue
Block a user