簡體   English   中英

argparse 告訴我一個 store_true arg 突然需要一個參數

[英]argparse telling me a store_true arg needs an argument all of a sudden

我正在使用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)

這已經工作了數周沒有問題,現在突然間我得到了錯誤:

ipython3: error: argument --loop: expected one argument

如果您有以下文件( example.py ):

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--loop', action='store_true', help="<help stuff")
args = parser.parse_args()
print(args)

您可以使用“普通”Python 執行它:

$ python example.py 
Namespace(loop=False)

$ python example.py --loop
Namespace(loop=True)

或使用 ipython,您需要通過--將 ipython arguments 與腳本 arguments 分開:

$ ipython3 example.py -- --loop
Namespace(loop=True)

這是完整的錯誤消息:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM