使用ArgParse忽略CLI参数中的子字符串/线路断开字符
我是第一次使用Python中的ArgParse模块,一年多来没有触摸该语言。 我需要在一个待办事项列表中求解一个错误,如果在换行中输入新任务,它会创建两个条目,例如:
$ python todo.py add "Hello,
World!"
将创建条目:
0 Hello,
1 World!
有没有办法告诉我可以告诉Argparse忽略界限如果?我已经尝试查看文档,但找不到它。
I'm using the argparse module in Python for the first time, and I haven't touched the language in over a year.
I need to solve a bug in a todo list where, if a new task is inputted with a line break, it creates two entries instead of one, e.g.:
$ python todo.py add "Hello,
World!"
will create entries:
0 Hello,
1 World!
Is there a way I can tell argparse to ignore the line break? I have tried looking into the documentation, but couldn't find it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从参数中删除每个新线,然后将它们传递给
parse_args
或parse_known_args
:AS
grogng> gright> gright> gright> grognal
usesys.argv
by Default by Default (/0924B95F6E78BEAF4A059D679515956BAC608FB/LIB/LIB/ARGPARSE.PYPPARSE.PY#L1870 ),您也可以修改
sys.argv
,然后以相同的方式进行 ,因为那是完成解析的地方。Remove every newline from the arguments then pass them to
parse_args
orparse_known_args
:As
ArgumentParser
usessys.argv
by default (https://github.com/python/cpython/blob/0924b95f6e678beaf4a059d679515956bac608fb/Lib/argparse.py#L1870), you could also modifysys.argv
before the parsing, in the same way:Or, still, you could override
parse_known_args
, as that is where the parsing is done.