Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ArgumentParser-h(帮助)将不起作用_Python_Command Line Arguments - Fatal编程技术网

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

Python ArgumentParser-h(帮助)将不起作用,python,command-line-arguments,Python,Command Line Arguments,我不能去上班。以下内容有什么问题: import argparse parser=argparse.ArgumentParser(description='''I wish this description would output''', epilog='''Please out the epilog''') parser.add_argument('-l', type=str, default='inf

我不能去上班。以下内容有什么问题:

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
时,我看不到任何帮助

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

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

如果从命令行运行此脚本,则只需打印“再见”,将
argparse
代码放在
If\uuu name\uuuu==“\uuu main\uuuuuu”之后:

如果从命令行运行此脚本,则只需打印“再见”,将
argparse
代码放在
If\uu name\uuuuu==“\uu main\uuuuuuu”之后:

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

声称你的代码在Ubuntu上运行得很好——我觉得这很奇怪。

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

声称你的代码在Ubuntu上运行得很好——我觉得这很奇怪。

好吧,奇怪的答案。通过调用程序解决了该问题:

python myscript.py -h
如果将python添加到路径中,请设置文件关联,然后执行以下操作:

myscript.py -h 

对于这个问题,它不会选择-h

OK奇怪的答案。通过调用程序解决了该问题:

python myscript.py -h
如果将python添加到路径中,请设置文件关联,然后执行以下操作:

myscript.py -h 

它不会拾取-h

,只是关于样式的注释,
parser.parse_args()
将在您导入模块时运行。这是你想要的行为吗?通常,您会将命令行参数处理放在
main
中。值得一提的是,这些代码在Ubuntu 11.10中工作。@Chris在其他函数中,我引用了args.l。这就是我在顶部定义args的原因。它就像一个全局变量。这是一个坏主意吗?只是一个关于样式的注释,
parser.parse_args()
将在您导入模块时运行。这是你想要的行为吗?通常,您会将命令行参数处理放在
main
中。值得一提的是,这些代码在Ubuntu 11.10中工作。@Chris在其他函数中,我引用了args.l。这就是我在顶部定义args的原因。它就像一个全局变量。这是个坏主意吗?如果其他函数需要引用args.l,会发生什么?会发生什么?编程发生了。例如:
args=vars(parser.parse_args());打印参数[“l”]#打印“信息”
。你可以随意将这些值传递给其他函数。我希望将它们作为全局变量,这样就不必传递了。它使事情变得更干净了。我必须传递它吗?或者每个函数都有它吗?叹气,如果你必须使用globals:……但是我认为你错误地认为代码“更干净”——globals使代码更难理解和调试(如果使用不当,我认为你正在尝试这样做)。感谢链接。我用词不当。专用于模块。你仍然认为这是个坏主意吗?如果其他函数需要引用args.l,会发生什么?会发生什么?编程发生了。例如:
args=vars(parser.parse_args());打印参数[“l”]#打印“信息”
。你可以随意将这些值传递给其他函数。我希望将它们作为全局变量,这样就不必传递了。它使事情变得更干净了。我必须传递它吗?或者每个函数都有它吗?叹气,如果你必须使用globals:……但是我认为你错误地认为代码“更干净”——globals使代码更难理解和调试(如果使用不当,我认为你正在尝试这样做)。感谢链接。我用词不当。专用于模块。你还认为这是个坏主意吗?