argparse in python3.2.3 on windows 7 does not seem to parse

拜拜、爱过 提交于 2019-12-25 12:52:13

问题


since I got python on windows running, here is the next problem I encountered with argparse, and for which I did not see a solution. I uses optparse before. Here is my code:

import argparse
parser = argparse.ArgumentParser(
        description = 'Test description')       # main description for help

parser.add_argument('-d', '--dir',                 # -u or --user option           
        dest = "dir",
        help = 'directory to start with')           
args = parser.parse_args()
print(args.dir)

but when I run this code with either

code.py -d test
code.py --dir test

I always get a None as output. I feel this is something trivial, and something obvious I overlooked, but I cannot see it.

Tanks

Alex


回答1:


The problem seem to be caused by Windows, and how the code is tried to be executed on the command line. In the given example the test script was called directly on the command line, without python before the code, as suggested in this answer.

If the code is executed like

python code.py

the expected behavior is seen, and the arguments are correctly parsed in the code.

So either the setup of the Windows system is stil incomplete, or the suggestion in the above link is incomplete.



来源:https://stackoverflow.com/questions/12420472/argparse-in-python3-2-3-on-windows-7-does-not-seem-to-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!