I have a parser with 3-4 arguments, which works well. I want to supply an unknown number of extra arguments to the script, which would get loaded into a template. I've read the argparse documentation, but I'm not sure it's possible. I can parse_known_arguments(), but I still have to handle the ["--placeholder1", "value1", "--placeholder2", "value2", ...] array myself. Should I go ahead with that, or is there a more pythonic way?
Just from the top of my head:
parser = argparse.ArgumentParser()
parser.add_argument("--template", required=True)
parser.add_argument("--location", required=True)
args,unknown = parser.parse_known_arguments()
tpl = LoadTemplate(args.template)
# Missing part where I transform unknown into an dict or namespace called extraarguments
raw = tpl.render(extraarguments)
# Print into args.location raw
render.py --template template1 --location /path/to/invoices --author John --customer Customer1 --title "Your invoice is ready!"
render.py --template template2 --location /path/to/newsletter --customer Customer2 --sender [email protected] --subject "Weekly newsletter"
In both cases, the template and location have to be present, but the extra arguments should be unpacked and sent into the template rendering function. It looks like a one-liner, but is there a more pythonic way of doing it?
argparseparse?docopt, it will make you smile.