简体   繁体   English

argparse 告诉我一个 store_true arg 突然需要一个参数

[英]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.

相关问题 argparse:store_true和int同时出现 - argparse: store_true and int at same time Argparse:具有store_true标志的可选子解析器 - Argparse: optional subparsers with store_true flags 带有动作=&#39;store_true&#39;的Argparse无法正常工作 - Argparse with action='store_true' not working as expected 在 python 中使用带有 argparse 和 store_true 的 JSON - Using JSON with argparse & store_true in python Python argparse-具有&#39;store_true&#39;行为的用户定义操作 - Python argparse - user defined Action with 'store_true' behavoiur 如何在argparse的互斥组中存储_true和存储值? - How to store_true and store value in a mutual exclusive group in argparse? Python argparse:metavar和action = store_true在一起 - Python argparse: metavar and action=store_true together 在 parser.add_argument 中同时包含 action='store_true' 和 default=False 有什么意义? - What's the point of having both action='store_true' and default=False in parser.add_argument? Python:optparse是否可以具有ACTION属性以像STORE和STORE_TRUE一样起作用? - Python: Can optparse have the ACTION attribute to act both like STORE and STORE_TRUE? 在不告诉我的情况下返回true或false……python 3 - Returning true or false without me telling it to… python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM