【发布时间】:2015-10-20 05:38:36
【问题描述】:
我想使用带有多个命令行的 argparser 模块,其中一些应该没有参数。
例子:
parser.add_argument('-website', type=str, nargs='*')
parser.add_argument('-auth', type=str, nargs='*')
parser.add_argument('-dothis', action='store_true')
所以我想在命令行中调用以下命令:
- python script.py -website www.website.com www.website2.com -dothis
在这种情况下,-dothis 命令应该只用于第二个网站而不是第一个,但它用于两个网站。
另一个例子:
-python script.py -website www.website1.com www.website2.com -auth username/password
在这种情况下,第二个网站的身份验证不是第一个。
所以我想要的是:
- python script.py www.webstie1.com -dothis www.website2.com -auth u:p -dothis www.website3.com -auth u:p www.website4.com
或:
- python script.py -site www.website1.com -auth u:p -site www.website2.com -site www.website3.com -auth u2:p2
那么我的脚本如何知道哪个网站的身份验证是什么?
我该如何解决?
【问题讨论】:
-
为什么不使用不同的参数集调用脚本两次?这个脚本有什么意义?
-
@jonrsharpe 因为我只想每天运行一次。
-
那么,我不确定标准的命令行界面是否可以帮助您。
-
您的示例混合使用了
-site、“-website”和“website”(具有不同名称和/或位置的选项)来输入名称。 -
你想看什么样的
usage消息?
标签: python arguments command-line-arguments argparse