我已经开始使用argparse
,因为我必须为mailer脚本编写Unix风格的参数解析。在
有了'auto'选项,它提供了filename mailer,它将执行作为filename接收的脚本,抓取准备好的邮件并发送它。在第二种模式--manual
,它应该能够从头开始构建任何邮件—一个文件作为内容,很少有附件,提供邮件列表的选项很少。我真的很想在那里使用--long_opt
约定(也许-short_opt
),但是如果我理解的话,我应该使用subparser。在
是否可以将子parser命名为'--name'
甚至'-n'
?现在,我想没有-我有个错误。在
示例用例:
(1) python script.py --auto 'anotherScriptName.py'
(2) python script.py --manual --content 'htmlcontent.txt' --attach 'file1.jpg' 'file2.xlsx' --mail-All
(3) python script.py --manual --content 'htmlcontent.txt' --mail-to 'mail1@gmail.com' 'mail2@yahoo.com'
(4) python script.py --manual --content 'htmlcontent.txt' --attach 'file1.txt' --mail-g ALL_DEVELOPERS
ad.4)所有的开发者可以是string或int(数据库中组的键)
代码如下:
^{pr2}$那太好了,但我想在这里有很多参数要解析(所有这些参数都是必需的),在第二个选项中,我只希望有一个路径来使用脚本来完成所有工作:
parser.add_argument('--manual', action='store_true', dest = 'mode', default = False)
我所拥有的:
p.add_argument('--content', action = 'store', dest = 'content', require=True, help = "no help for you, it's just path to html content")
p.add_argument('--attach', action = 'store', dest = 'attachments', nargs='*', help = "attachments")
p.add_argument('--mail', '-m', choices=['-All', '-fromFile', '-group', '-to'], dest = 'mailOpt', require=True)
p.add_argument( dest = 'mails', nargs='*') # 0 args if -All selected, raise error in other case
p2 = subparsers.add_parser('--auto') # Like '--manual'
p2.add_argument('--filename', action='store')
如果不可能,我应该使用_parse_known_args_u函数还是坏主意,只留下名称“manual”和“auto”,因为它们看起来足够清楚?在
目前没有回答
相关问题 更多 >
编程相关推荐