ArgumentParser -h(帮助)将不起作用
我无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
argsparse 代码实际上从未被执行。通过从命令行执行脚本,您将调用
main()
,它只会打印并退出。您必须在main()
函数中调用parse_args()
才能正常工作。产生:
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 callparse_args()
in themain()
function for this to work.Produces:
jcollado claims your code worked fine on Ubuntu--I find this very curious.
如果您从命令行运行此脚本,您将只打印“再见”,请将
argparse
代码放在if __name__ == "__main__":
之后。If you run this script from the command line you're going to just print 'goodbye', put the
argparse
code afterif __name__ == "__main__":
.好吧,这个问题的答案很奇怪。通过将程序调用为以下方式解决了问题:
如果将 python 添加到路径,设置文件关联,然后执行以下操作:
它不会拾取 -h
OK weird answer for this. The problem was resolved by calling the program as:
If you add python to your path, set file associations and then just do:
it will not pick up the -h