From 93d95199cbcad411feef0da6e9afff06c46c7e5b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 28 Jun 2024 00:43:39 +0200 Subject: [PATCH] refactor: move CLI options validation to main() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/ocpp-server/server.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/ocpp-server/server.py b/tests/ocpp-server/server.py index a5ebed38..fdef6bed 100644 --- a/tests/ocpp-server/server.py +++ b/tests/ocpp-server/server.py @@ -130,12 +130,6 @@ class ChargePoint(ocpp.v201.ChargePoint): async def send_command( self, command_name: Action, delay: Optional[float], period: Optional[float] ): - if not delay and not period: - raise ValueError("Either delay or period must be defined") - if delay and delay <= 0: - raise ValueError("Delay must be a positive number") - if period and period <= 0: - raise ValueError("Period must be a positive number") try: if delay and not self._command_timer: self._command_timer = Timer( @@ -201,12 +195,36 @@ async def on_connect( cp.handle_connection_closed() +def check_positive_number(value: Optional[float]): + try: + value = float(value) + except ValueError: + raise argparse.ArgumentTypeError("must be a number") from None + if value <= 0: + raise argparse.ArgumentTypeError("must be a positive number") + return value + + # Main function to start the WebSocket server. async def main(): parser = argparse.ArgumentParser(description="OCPP2 Server") - parser.add_argument("-c", "--command", type=Action, help="OCPP2 Command Name") - parser.add_argument("-d", "--delay", type=float, help="Delay in seconds") - parser.add_argument("-p", "--period", type=float, help="Period in seconds") + parser.add_argument("-c", "--command", type=Action, help="OCPP2 command name") + parsed_args, _ = parser.parse_known_args() + group = parser.add_mutually_exclusive_group( + required=parsed_args.command is not None + ) + group.add_argument( + "-d", + "--delay", + type=check_positive_number, + help="delay in seconds", + ) + group.add_argument( + "-p", + "--period", + type=check_positive_number, + help="period in seconds", + ) args = parser.parse_args() -- 2.34.1