在与argparse等于符号之后,如何将列表作为命令行参数传递?

发布于 2025-02-04 21:36:47 字数 769 浏览 3 评论 0原文

使用argparse通过参数列表的记录方法使用以下语法

parser = argparse.ArgumentParser()
parser.add_argument("--list", nargs="+", default=["a", "b"])
value = parser.parse_args()

在shell

$ python3 example.py-list ab c

$ ['a'a','b','c ']

IM与之合作的参数具有领先的破折号,要求使用=不要被ArgParse误解为另一个选项(请参阅此问题

$ python3 example.py-list = -list = -a

$ [' - - A']

我想使用=语法中的参数列表。但是,我不确定如何实现这一目标。我正在寻找类似以下操作的内容:

$ python3 example.py -list = -a,-b

$ [-a',',' - b']

The documented way to pass a list of arguments using argparse uses the following syntax

parser = argparse.ArgumentParser()
parser.add_argument("--list", nargs="+", default=["a", "b"])
value = parser.parse_args()

In the SHELL

$ python3 example.py --list a b c

$ ['a', 'b', 'c']

The arguments Im working with have leading dashes and require the use of = to not be misinterpreted by argparse as another option (See this question)

$ python3 example.py --list=-a

$ ['-a']

I would like to pass in a list of arguments using the = syntax. However, Im not sure how to accomplish this. I'm looking for something like the following to work:

$ python3 example.py --list=-a,-b

$ ['-a','-b']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文