0

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

Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

2

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.

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469