[英]argparse telling me a store_true arg needs an argument all of a sudden
I'm using the argparse
library and have a few boolean arguments created like this:我正在使用argparse
库并创建了一些 boolean arguments,如下所示:
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate a batch of Line Protocol points of a specified shape")
parser.add_argument('--loop', action='store_true', help="If True, script runs in infinit loop; used with Telegraf `execd` input plugin")
args = parser.parse_args()
print(args)
This has worked for weeks with no issue and now all of a sudden I'm getting the error:这已经工作了数周没有问题,现在突然间我得到了错误:
ipython3: error: argument --loop: expected one argument
If you have the following file ( example.py
):如果您有以下文件( example.py
):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--loop', action='store_true', help="<help stuff")
args = parser.parse_args()
print(args)
you can execute it either with "plain" Python:您可以使用“普通”Python 执行它:
$ python example.py
Namespace(loop=False)
$ python example.py --loop
Namespace(loop=True)
or with ipython, where you need to separate ipython arguments with script arguments by --
:或使用 ipython,您需要通过--
将 ipython arguments 与脚本 arguments 分开:
$ ipython3 example.py -- --loop
Namespace(loop=True)
Is this the full error message:这是完整的错误消息:
0814:~/mypy$ ipython3 --loop
usage: ipython3 [-h] [--debug] [--quiet] [--init] [--autoindent] [--no-autoindent] [--automagic]
[--no-automagic] [--pdb] [--no-pdb] [--pprint] [--no-pprint] [--color-info]
[--no-color-info] [--ignore-cwd] [--no-ignore-cwd] [--nosep] [--autoedit-syntax]
[--no-autoedit-syntax] [--simple-prompt] [--no-simple-prompt] [--banner] [--no-banner]
[--confirm-exit] [--no-confirm-exit] [--term-title] [--no-term-title] [--classic]
[--quick] [-i] [--profile-dir ProfileDir.location]
[--profile TerminalIPythonApp.profile] [--ipython-dir TerminalIPythonApp.ipython_dir]
[--log-level TerminalIPythonApp.log_level]
[--config TerminalIPythonApp.extra_config_file]
[--autocall TerminalInteractiveShell.autocall]
[--colors TerminalInteractiveShell.colors] [--logfile TerminalInteractiveShell.logfile]
[--logappend TerminalInteractiveShell.logappend] [-c TerminalIPythonApp.code_to_run]
[-m TerminalIPythonApp.module_to_run] [--ext TerminalIPythonApp.extra_extensions]
[--gui TerminalIPythonApp.gui] [--pylab [TerminalIPythonApp.pylab]]
[--matplotlib [TerminalIPythonApp.matplotlib]]
[--cache-size TerminalInteractiveShell.cache_size]
[extra_args [extra_args ...]]
ipython3: error: argument --loop: expected one argument
1240:~/mypy$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.