简体   繁体   English

Windows 7上的python3.2.3中的argparse似乎没有解析

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

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. 因为我在windows上运行python,这是我遇到的argparse的下一个问题,我没有看到解决方案。 I uses optparse before. 我以前用过optparse。 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. 我总是得到一个None输出。 I feel this is something trivial, and something obvious I overlooked, but I cannot see it. 我觉得这是微不足道的,我忽略了一些明显的东西,但我看不到它。

Tanks 坦克

Alex 亚历克斯

The problem seem to be caused by Windows, and how the code is tried to be executed on the command line. 问题似乎是由Windows引起的,以及如何尝试在命令行上执行代码。 In the given example the test script was called directly on the command line, without python before the code, as suggested in this answer . 在给定的示例中,测试脚本直接在命令行上调用,在代码之前没有python ,如本答案中所示

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. 因此,要么Windows系统的设置不完整,要么上述链接中的建议不完整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM