1

I've upgraded to Python 3.4.2 and argparse (from optparse) but neither appears to recognise command line options. As a simple test I run this;

#test_argparse.py

def main():
    import argparse

    parser = argparse.ArgumentParser(description='Execute database query.')

    parser.add_argument("-q", "--query", dest="query",
                  help="Name of the query file to be run")

    args = parser.parse_args()

    print(args)

if __name__ == '__main__':
     main()

From the command line, when I run

test_argparse.py -q get_msre_for_book

I get:

Namespace(query=None)

But when I run from within the IDE:

*** Python 3.4.2rc1 (v3.4.2rc1:8711a0951384, Sep 21 2014, 21:16:45) [MSC v.1600 32 bit (Intel)] on win32. ***

Command Line : -q get_msre_for_book
Namespace(query='get_msre_for_book')

I'm trying to launch the script via

os.system('python.exe', '<path to script>test_argparse.py -q get_msre_for_book')

But that fails as no query is presented as shown above. I've worked round this by launching using

subprocess.call

but I'd rather not have to. Again, any suggestions or ideas gratefully received.

NickA
  • 13
  • 3
  • Sounds like a Windows command line issue. I'd import `sys`, and print `sys.argv` to check the commandline strings that the parser has to work with. – hpaulj Jul 13 '15 at 23:27
  • ok having added `print(sys.argv)` when I run `test_argparse.py -q get_msre_for_book` I now get `Namespace(query=None)` and `['Y:\\Python\\test_argparse.py']` So it does indeed appear to be something wrong with the command line. – NickA Jul 14 '15 at 11:47
  • and in the ide `Command Line : -q get_msre_for_trader_book2` `Namespace(query='get_msre_for_trader_book2')` `['Y:\\Python\\test_argparse.py', '-q', 'get_msre_for_trader_book2']` – NickA Jul 14 '15 at 11:53
  • But on another Windows 7 box, running optparse and Python 2.6.6 it works as expected `(, []) ['N:\\Python\\test_optparse.py', '-q', 'test']` – NickA Jul 14 '15 at 11:57
  • There are earlier SO questions about passing commandline arguments in Windows 7. The solutions involve getting a registry link right. http://stackoverflow.com/questions/2640971, http://stackoverflow.com/questions/9880540. e,g, `"C:\Python\Python33\python.exe" "%1" %*.` – hpaulj Jul 14 '15 at 15:47
  • That fixed it - thank you very much - but to be precise the registry entry needs to be `C:\Python\Python33\python.exe" "%1" %"` (added the final double quote) – NickA Jul 14 '15 at 20:09

1 Answers1

0

To give closure to this question, these are the 2 comments that solve the problem:

There are earlier SO questions about passing commandline arguments in Windows 7. The solutions involve getting a registry link right. stackoverflow.com/questions/2640971, stackoverflow.com/questions/9880540. e,g, "C:\Python\Python33\python.exe" "%1" %*.

That fixed it - thank you very much - but to be precise the registry entry needs to be C:\Python\Python33\python.exe" "%1" %" (added the final double quote)

hpaulj
  • 221,503
  • 14
  • 230
  • 353