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:
 | 
					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
 | 
					    @staticmethod
 | 
				
			||||||
    def main() -> None:
 | 
					    def main() -> None:  # pragma: no cover
 | 
				
			||||||
        print("Hello world!")
 | 
					        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)
 | 
				
			||||||
							
								
								
									
										2
									
								
								tox.ini
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								tox.ini
									
									
									
									
									
								
							@@ -39,7 +39,7 @@ exclude =
 | 
				
			|||||||
    .cache,
 | 
					    .cache,
 | 
				
			||||||
    .eggs
 | 
					    .eggs
 | 
				
			||||||
max-complexity = 15
 | 
					max-complexity = 15
 | 
				
			||||||
max-line-length = 80
 | 
					max-line-length = 120
 | 
				
			||||||
import-order-style = google
 | 
					import-order-style = google
 | 
				
			||||||
application-import-names = flake8
 | 
					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
 | 
					format = ${cyan}%(path)s${reset}:${yellow_bold}%(row)d${reset}:${green_bold}%(col)d${reset}: ${red_bold}%(code)s${reset} %(text)s
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user