Added basic functionality to make the module executable
This commit is contained in:
parent
a45cc806d8
commit
8004fcf6bc
@ -7,8 +7,7 @@ from argparse import ArgumentError
|
|||||||
def make_arg_parser() -> argparse.ArgumentParser:
|
def make_arg_parser() -> argparse.ArgumentParser:
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
usage='docxtpl template_path json_path output_filename',
|
usage='docxtpl template_path json_path output_filename',
|
||||||
description='Make docx file from existing template docx and json data.',
|
description='Make docx file from existing template docx and json data.')
|
||||||
add_help=True)
|
|
||||||
parser.add_argument('Template',
|
parser.add_argument('Template',
|
||||||
metavar='template_path',
|
metavar='template_path',
|
||||||
type=str,
|
type=str,
|
||||||
@ -28,11 +27,8 @@ def get_args(parser: argparse.ArgumentParser) -> dict:
|
|||||||
try:
|
try:
|
||||||
parsed_args = vars(parser.parse_args())
|
parsed_args = vars(parser.parse_args())
|
||||||
return parsed_args
|
return parsed_args
|
||||||
# There is a bug that prevents argparser from catching ArgumentError
|
# Argument errors cause SystemExit error, not ArgumentError
|
||||||
# manually. For more info: https://bugs.python.org/issue41255
|
except SystemExit:
|
||||||
# I know bare exceptions are wrong, could not find another way to catch
|
|
||||||
# wrong arguments.
|
|
||||||
except:
|
|
||||||
raise RuntimeError(f'Correct usage is:\n{parser.usage}')
|
raise RuntimeError(f'Correct usage is:\n{parser.usage}')
|
||||||
|
|
||||||
|
|
||||||
@ -63,7 +59,7 @@ def validate_all_args(parsed_args) -> None:
|
|||||||
try:
|
try:
|
||||||
for arg_name, arg_value in parsed_args.items():
|
for arg_name, arg_value in parsed_args.items():
|
||||||
if not is_argument_valid(arg_name, arg_value):
|
if not is_argument_valid(arg_name, arg_value):
|
||||||
raise AttributeError
|
raise ArgumentError
|
||||||
except ArgumentError as e:
|
except ArgumentError as e:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f'The specified {arg_name} "{arg_value}" is not valid.')
|
f'The specified {arg_name} "{arg_value}" is not valid.')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user