use*_*203 2 python python-2.7 argparse
我有一个3-4个参数的解析器,效果很好.我想为脚本提供未知数量的额外参数,这些参数将被加载到模板中.我已经阅读了argparse文档,但我不确定它是否可行.我可以parse_known_arguments(),但我仍然需要自己处理["--placeholder1", "value1", "--placeholder2", "value2", ...]数组.我应该继续这样做,还是有更多的pythonic方式?
就在我的头顶:
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 john@store.com --subject "Weekly newsletter"
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,template并且location必须存在,但额外的参数应该是解压缩并送入模板渲染功能.它看起来像一个单行,但是有更多的pythonic方式吗?
假设unknown列表是键和值的交替列表,它可以变成一个字典:
adict = dict(zip(unknown[:-1:2],unknown[1::2]))
Run Code Online (Sandbox Code Playgroud)
zip部分将列表中的列表转换为对,然后将其转换为字典.您可能需要更多地处理值以从键中删除" - "前缀.如果需要检查错误,可能需要更明确的迭代,例如不匹配的"键值"序列.
这是一个保留' - '的版本:
templates = {'template1': "from: {--author} to: {--customer} re: {--title}",
'template2': "from: {--sender} to: {--customer} re: {--subject}"}
def parser():
parser = argparse.ArgumentParser()
parser.add_argument("--template", required=True, choices=templates)
parser.add_argument("--location", required=True)
args,unknown = parser.parse_known_args()
extraarguments = dict(zip(unknown[::2], unknown[1::2]))
tpl = templates[args.template]
raw = tpl.format(**extraarguments)
return raw
print parser()
Run Code Online (Sandbox Code Playgroud)
使用您的2个样本,这会产生:
In [25]: run stack30139426.py --template template1 --location /path/to/invoices --author John --customer Customer1 --title "Your invoice is ready!"
from: John to: Customer1 re: Your invoice is ready!
In [26]: run stack30139426.py --template template2 --location /path/to/newsletter --customer Customer2 --sender john@store.com --subject "Weekly newsletter"
from: john@store.com to: Customer2 re: Weekly newsletter
Run Code Online (Sandbox Code Playgroud)
关于输入字典或其他未知键/值对,还有其他SO问题.
一个建议是使用key:value语法,然后简单[kv.split(':') for kv in unknowns]地生成一对对列表:
run stack30139426.py --template template2 --location /path/to/newsletter customer:Customer2 sender:john@store.com subject:"Weekly newsletter"
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用JSON语法
run stack30139426.py --template template2 --location /path/to/newsletter '{"customer":"Customer2", "sender":"john@store.com", "subject":"Weekly newsletter"}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |