From 0b13a0979fcbe0594d47d82e7f885d36100a7587 Mon Sep 17 00:00:00 2001 From: kingstead Date: Mon, 20 Jan 2025 15:08:03 -0600 Subject: [PATCH 1/2] update cli with argparse --- main.py | 123 +++++++++++++++++++++++++------------------------------- 1 file changed, 54 insertions(+), 69 deletions(-) diff --git a/main.py b/main.py index 26c5ee2..2a26efd 100644 --- a/main.py +++ b/main.py @@ -1,84 +1,69 @@ from parsers.sig import * -import sys +import sys, argparse -class bcolors: - HEADER = "\033[95m" - OKBLUE = "\033[94m" - OKCYAN = "\033[96m" - OKGREEN = "\033[92m" - WARNING = "\033[93m" - FAIL = "\033[91m" - ENDC = "\033[0m" - BOLD = "\033[1m" - UNDERLINE = "\033[4m" - WHITE = "\033[97m" +def main(): + parser = setup_parser() + args = parser.parse_args() + if len(sys.argv) <= 1: + parser.print_usage() + sys.exit("Error: No arguments provided") + elif args.m: + generate_single() + elif args.b: + input_file, output_file = args.b + check_csv_files(args.b) + generate_bulk() + else: + print("Something bad happened.") -def main(): - value = get_input() - generate_output(value) +def setup_parser(): + parser = argparse.ArgumentParser( + prog="ParseRx", + description="A modern, lightweight medication sig parser.", + formatter_class=argparse.RawTextHelpFormatter, + ) + parser.add_argument( + "-m", nargs=argparse.REMAINDER, help="""Enter a single sig to parse.""" + ) + parser.add_argument( + "-b", + nargs=2, + metavar=("input.csv", "output.csv"), + help="""Bulk sig instructions: +> Place your input file in the /csv directory. +> Input files are read from the /csv directory. +> Output files are written to the /csv/output directory. +> Enter the input file name (input.csv as default) and output file name (output.csv as default), separated by a space.""", + ) + return parser -def get_input(): - while True: - try: - if len(sys.argv) == 1: - print_usage_instructions() - break - elif sys.argv[1] == "--b": - input_file, output_file = sys.argv[2], sys.argv[3] - return 2 - else: - return 1 - except IndexError: - print("Usage: main.py --b input.csv output.csv") - break +def check_csv_files(filenames): + for filename in filenames: + if not filename.endswith(".csv"): + raise argparse.ArgumentTypeError("All files must have a .csv extension") + return filenames -def print_usage_instructions(): - instructions = [ - ( - bcolors.BOLD - + bcolors.WHITE - + "\n Individual sig usage: " - + bcolors.ENDC - + "main.py your sig goes here" - ), - ( - bcolors.BOLD - + bcolors.WHITE - + "\n Bulk sig usage: " - + bcolors.ENDC - + " main.py --b input.csv output.csv\n" - ), - ( - " Bulk sig instructions: \n > Place your input file in the /csv directory.\n" - " > Input files are read from the /csv directory.\n" - " > Output files are written to the /csv/output directory.\n" - " > Enter the input file name (input.csv as default) and output file name (output.csv as default), separated by a space.\n" - ), - ] - for instruction in instructions: - print(instruction) +def generate_single(): + print(SigParser().parse(" ".join(sys.argv[1:]))) -def generate_output(n): - if n == 1: - print(SigParser().parse(" ".join(sys.argv[1:]))) - elif n == 2: - try: - input_file, output_file = sys.argv[2], sys.argv[3] - if input_file.endswith(".csv") and output_file.endswith(".csv"): - SigParser().parse_sig_csv(input_file, output_file) - print(f"Output written to {output_file}.") - else: - print("Both files must end with .csv. Please try again.") - except ValueError: - print("Invalid. Enter input and output file names separated by a space.") - except FileNotFoundError: - print("Input file not found. Please try again.") +def generate_bulk(): + try: + input_file, output_file = sys.argv[2], sys.argv[3] + if input_file.endswith(".csv") and output_file.endswith(".csv"): + SigParser().parse_sig_csv(input_file, output_file) + print(f"Output written to {output_file}.") + else: + print("Both files must end with .csv. Please try again.") + except ValueError: + print("Invalid. Enter input and output file names separated by a space.") + except FileNotFoundError: + print("Input file not found. Please try again.") if __name__ == "__main__": From 5996acea4dc1aa8ee450c455463957473c9d9f19 Mon Sep 17 00:00:00 2001 From: kingstead Date: Mon, 20 Jan 2025 18:15:20 -0600 Subject: [PATCH 2/2] update cli for argparse --- main.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 2a26efd..a5ea3f4 100644 --- a/main.py +++ b/main.py @@ -14,9 +14,9 @@ def main(): elif args.b: input_file, output_file = args.b check_csv_files(args.b) - generate_bulk() + generate_bulk(input_file, output_file) else: - print("Something bad happened.") + sys.exit("An unknown error occured.") def setup_parser(): @@ -52,18 +52,8 @@ def generate_single(): print(SigParser().parse(" ".join(sys.argv[1:]))) -def generate_bulk(): - try: - input_file, output_file = sys.argv[2], sys.argv[3] - if input_file.endswith(".csv") and output_file.endswith(".csv"): - SigParser().parse_sig_csv(input_file, output_file) - print(f"Output written to {output_file}.") - else: - print("Both files must end with .csv. Please try again.") - except ValueError: - print("Invalid. Enter input and output file names separated by a space.") - except FileNotFoundError: - print("Input file not found. Please try again.") +def generate_bulk(input, output): + SigParser().parse_sig_csv(input, output) if __name__ == "__main__":