簡體   English   中英

ipython不顯示argparse幫助消息

[英]ipython not showing argparse help message

在我的python腳本myscript.py我使用argparse傳遞命令行參數。 當我想顯示有關輸入參數的幫助信息時,我只需執行以下操作:

$ python myscript.py --help

相反,如果我想使用ipython運行我的腳本,則不會顯示幫助消息。 Ipython將顯示其自己的幫助信息:

$ ipython -- myscript.py -h
=========
 IPython
=========

Tools for Interactive Computing in Python
=========================================

    A Python shell with automatic history (input and output), dynamic object
    introspection, easier configuration, command completion, access to the
    system shell and more.  IPython can also be embedded in running programs.

Usage

    ipython [subcommand] [options] [files]

並不是很煩人,但是有辦法解決嗎?

您需要在ipython中運行.py腳本。 像這樣:

%run script.py -h

這是一個IPython錯誤,已在https://github.com/ipython/ipython/pull/2663中更正。

我的0.13出現此錯誤; 0.13.2已更正。 該修復程序位於IPthyon/config/application.py Application.parse_command_line 此函數在將內容傳遞給parse_known_args (因此自定義幫助格式)之前,在sys.argv查找幫助和版本標志( -h-V )。 在更正的發行版中,它僅對sys.argv進行檢查,直到第一個--為止。 在查看整個數組之前。

早期:

較早版本的一個解決方法是在腳本中定義備用幫助標志:

simple.py腳本:

import argparse, sys
print(sys.argv)
p = argparse.ArgumentParser(add_help=False) # turn off the regular -h
p.add_argument('-t')
p.add_argument('-a','--ayuda',action=argparse._HelpAction,help='alternate help')
print(p.parse_args())

調用:

$ ./ipython3 -- simple.py -a
['/home/paul/mypy/argdev/simple.py', '-a']
usage: simple.py [-t T] [-a]

optional arguments:
  -t T
  -a, --ayuda  alternate help

$ ./ipython3 -- simple.py -t test
['/home/paul/mypy/argdev/simple.py', '-t', 'test']
Namespace(t='test')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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