From fc40664ddff59397cec1e67c2dfc48bcb808d055 Mon Sep 17 00:00:00 2001 From: victor Date: Sat, 6 Apr 2024 20:07:21 +0200 Subject: [PATCH] Added Try/except --- status.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/status.py b/status.py index e3c4a09..594475c 100644 --- a/status.py +++ b/status.py @@ -1,22 +1,29 @@ import argparse +import sys def parseArguments(): # Crear un objeto ArgumentParser parser = argparse.ArgumentParser(description='Ejemplo de script con flags') # Agregar los argumentos con sus respectivos flags - parser.add_argument('-w', '--web', action='store', dest='web', help='Indicar una web') - parser.add_argument('-h', '--host', action='store', dest='host', help='Indicar un host') + parser.add_argument('-W', '--web', action='store', dest='web', help='Indicar un archivo con lista de Webs') + parser.add_argument('-H', '--host', action='store', dest='host', help='Indicar un archivo con lista de hosts') - # Parsear los argumentos de la línea de comandos - args = parser.parse_args() - - return args + try: + # Parsear los argumentos de la línea de comandos + args = parser.parse_args() + if args.web: + return args.web + elif args.host: + return args.host + except argparse.ArgumentError as e: + # Si ocurre un error al analizar los argumentos, mostrar un mensaje de error + print("Error:", e) + # Mostrar el mensaje de ayuda + parser.print_help() + sys.exit(1) # Salir del programa con un código de error if __name__ == '__main__': args = parseArguments() - if args.web: - print()# Parte web, comprobar que Código devuelve - else: - print()# Parte host, hará ping \ No newline at end of file + print(args) \ No newline at end of file