From 12334dcd3f6461e51caec5aaf2e296bded8af4f6 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 00:02:29 -0800 Subject: [PATCH] Adding checkUserInput() per #30 to start validating user input * Added checkUserInput() * Checked callsign * Checked ID * Checked IP Address --- faradayio_cli/faradayio_cli.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 5b25131..11fb2dd 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -7,6 +7,7 @@ import threading import time import pytun +import ipaddress from faradayio.faraday import Monitor from faradayio.faraday import SerialTestClass @@ -47,6 +48,36 @@ def setupArgparse(): # Parse and return arguments return parser.parse_args() +def checkUserInput(args): + """Checks user input for validity + + Args: + args: argparse arguments + + """ + # Check callsign + # Expect a string + if not isinstance(args.callsign, str): + raise TypeError + # Callsigns are at most seven characters long + if len(args.callsign) > 7: + raise ValueError + + # Check ID + # Expect and integer + if not isinstance(args.id, int): + raise TypeError + # Expect a value between 0-255 + if args.id < 0 or args.id > 255: + raise ValueError + + # Check IP Address + # Expect a string + if not isinstance(args.addr, str): + raise TypeError + # Expect an IP address that is valid + ipaddress.IPv4Address(args.addr) + def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port. @@ -95,6 +126,8 @@ def main(): except argparse.ArgumentError as error: raise SystemExit(error) + checkUserInput(args) + # Setup serial port try: serialPort = setupSerialPort(loopback=args.loopback,