ArgumentParser -h(帮助)将不起作用

发布于 2025-01-04 05:06:10 字数 701 浏览 1 评论 0原文

我无法让 ArgumentParser 工作。以下有什么问题:

import argparse
parser=argparse.ArgumentParser(description='''I wish this description would output''',
                                            epilog='''Please out the epilog''')

parser.add_argument('-l', type=str, default='info', help='logging level. Default is info. Use debug if you have problems.')
args=parser.parse_args()

def main():
    print("goodbye")

if __name__ == "__main__":
    #main
    main()

当我运行 myscript -h 时,我看不到任何帮助。

我在 Windows 7 上运行 Python 2.7。我的路径上有 Python,并且 pathext 设置为:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py

I cannot get ArgumentParser to work. What's wrong with the following:

import argparse
parser=argparse.ArgumentParser(description='''I wish this description would output''',
                                            epilog='''Please out the epilog''')

parser.add_argument('-l', type=str, default='info', help='logging level. Default is info. Use debug if you have problems.')
args=parser.parse_args()

def main():
    print("goodbye")

if __name__ == "__main__":
    #main
    main()

When I run myscript -h I see no help.

I am running Python 2.7 on Windows 7. I have Python on my path and also pathext set as:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琉璃繁缕 2025-01-11 05:06:10

argsparse 代码实际上从未被执行。通过从命令行执行脚本,您将调用 main(),它只会打印并退出。您必须在 main() 函数中调用 parse_args() 才能正常工作。

import argparse

# Personally, I think these belong in the main()
# function as well, but they don't need to be.
parser = argparse.ArgumentParser(
    description="I wish this description would output",
    epilog="Please out the epilog"
)
parser.add_argument(
    "-l",
    type=str,
    default="info",
    help="logging level. Default is info. Use debug if you have problems."
)

def main():
    args = parser.parse_args() # Parses arguments
    print("goodbye")

if __name__ == "__main__":
    main() # Calls main

产生:

~/Desktop $ python untitled.py --help
usage: untitled.py [-h] [-l L]

I wish this description would output

optional arguments:
  -h, --help  show this help message and exit
  -l L        logging level. Default is info. Use debug if you have problems.

Please out the epilog

jcollado 声称你的代码在 Ubuntu 上运行良好 - 我觉得这很好奇。

The argsparse code never actually gets executed. By executing the script from the command line, you're calling main(), which simply prints and exits. You have to call parse_args() in the main() function for this to work.

import argparse

# Personally, I think these belong in the main()
# function as well, but they don't need to be.
parser = argparse.ArgumentParser(
    description="I wish this description would output",
    epilog="Please out the epilog"
)
parser.add_argument(
    "-l",
    type=str,
    default="info",
    help="logging level. Default is info. Use debug if you have problems."
)

def main():
    args = parser.parse_args() # Parses arguments
    print("goodbye")

if __name__ == "__main__":
    main() # Calls main

Produces:

~/Desktop $ python untitled.py --help
usage: untitled.py [-h] [-l L]

I wish this description would output

optional arguments:
  -h, --help  show this help message and exit
  -l L        logging level. Default is info. Use debug if you have problems.

Please out the epilog

jcollado claims your code worked fine on Ubuntu--I find this very curious.

つ低調成傷 2025-01-11 05:06:10

如果您从命令行运行此脚本,您将只打印“再见”,请将 argparse 代码放在 if __name__ == "__main__": 之后。

If you run this script from the command line you're going to just print 'goodbye', put the argparse code after if __name__ == "__main__":.

╰ゝ天使的微笑 2025-01-11 05:06:10

好吧,这个问题的答案很奇怪。通过将程序调用为以下方式解决了问题:

python myscript.py -h

如果将 python 添加到路径,设置文件关联,然后执行以下操作:

myscript.py -h 

它不会拾取 -h

OK weird answer for this. The problem was resolved by calling the program as:

python myscript.py -h

If you add python to your path, set file associations and then just do:

myscript.py -h 

it will not pick up the -h

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文