2

I'm obviously in need of help with argparse. From the 3.4 manual, if I type

import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('-f', '--foo')
parser.add_argument('bar')
parser.parse_args()

on the command line, having saved this code into test.py and then run

test.py bar

I as expecting my prompt back, but I get

usage: PROG [-h] [-f FOO] bar
PROG: error: the following arguments are required: bar

What have I got to do to make test.py accept a non-optional argument? Incidentally, I'm running this on Windows 8.1, and when I run the same code on my Mac, I get my prompt back with no script output as expected. That means it's something to do with windows.

EDIT: I can try this in an interactive session and it also fails to operate correctly.

EDIT: Yes - sys.argv is not having the arguments appended to it. I think it's something to do with the file association. On install, Parallels uses Python's Python Launcher which mean it tried to run the script on my Mac host. I edited the association to the local python binary, but now this. I've tried hacking the registry to help, but it was already set correctly to "c:\windows\py.exe" "%1" %*

volvox
  • 3,014
  • 16
  • 51
  • 80

1 Answers1

2

OK I found out what's happened. I set file associations to .py (and .pyc, .pyo, .pyw) to the explicit python binary path. This does not work, even if you hack the registry. Going back into file associations ('PC Settings'>Search & Apps>Defaults (scroll to bottom)>Files by extension>Scroll to .py) and clicking the .py extension, Select 'Python Launcher for Windows (Console) then my script behaved as expected.

It boils down to selecting the right launcher.

volvox
  • 3,014
  • 16
  • 51
  • 80