From d1dcb1d587f71e3d922d5ead79d8df5255d2654c Mon Sep 17 00:00:00 2001 From: truePhilipp Date: Mon, 6 May 2024 17:14:43 +0000 Subject: [PATCH] added proper command line parsing --- html_to_django/converter.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/html_to_django/converter.py b/html_to_django/converter.py index 2161f89..ec3a989 100644 --- a/html_to_django/converter.py +++ b/html_to_django/converter.py @@ -1,6 +1,8 @@ import bs4 import sys import os +import sys +import argparse from glob import glob from .formatters import formatters @@ -39,9 +41,24 @@ def convert_dir(path: str, overwrite: bool = False) -> None: convert_file(filepath, overwrite) +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser("html_to_django") + parser.add_argument("path", help=""" + The path to a file (or folder of files) to be converted. + In case a folder is passed, ALL html files in it (recursivly) will be converted. + """, type=str) + parser.add_argument("-r", "--replace", help=""" + Replace the input file(s). + """, action='store_true') + return parser.parse_args() + + def command_entry() -> None: - path = os.path.abspath(sys.argv[1]) + args = parse_args() + path = os.path.abspath(args.path) if os.path.isdir(path): - convert_dir(path) + convert_dir(path, args.replace) elif os.path.isfile(path): - convert_file(path) + convert_file(path, args.replace) + else: + sys.exit(f'The path "{path}" does not exist.')