argparse: nargs with multiple flags in one dash?

孤者浪人 提交于 2020-07-10 10:25:43

问题


If I try running python test.py -bf with the below code, I get bar=f and foo=foo1 instead of the desired result (bar=bar1 and foo=foo1). How would I achieve the desired result?

import argparse

ap = argparse.ArgumentParser(description='test')
ap.add_argument('--bar', '-b', nargs='?', const='bar1')
ap.add_argument('--foo', '-f', nargs='?', const='foo1')
args = ap.parse_args()

来源:https://stackoverflow.com/questions/62747802/argparse-nargs-with-multiple-flags-in-one-dash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!