0

OK, I found three similar threads but all of them dealt with command-line arguments in C#. My question is, how can I read the command-line arguments from the cmd window, into IronPython?

The way I read Section 2.1.1 in these docs, it should work the same as CPython -- just import the sys module and get the values from the sys.argv array. It doesn't. Here's the test code I tried (file z.py):

import sys
print sys.argv[0]
print sys.argv[1]

When I call this as z.py testing using the CPython (2.7.4) interpreter, I get what you'd expect:

E:\>c:\python27\python.exe z.py testing
z.py
testing

E:\>

But when I call it using the IronPython (2.7.3) interpreter, I get:

Traceback (most recent call last):
  File "E:\z.py", line 3, in <module>
IndexError: index out of range: 1

If I comment out line 3 and try again with IronPython, it runs fine:

E:\>z.py testing
E:\z.py

E:\>

So sys.argv[0] is being assigned but that's it -- the command-line arguments never appear as additional elements in sys.argv.

So either there's some extra step I'm missing, or sys.argv just doesn't work for this purpose in IronPython. Either way, how do I get the module to recognize the arguments?

1 Answer 1

1

The problem is that IronPython installation hasn't registered the python interpreter correctly. It just passes the first argument (which is the script name) to the IronPython interpreter, missing the rest of the arguments. That is: it registered the *.py files with ...\ipy.exe %1 instead of ...\ipy.exe %*.

The solution is to run the scripts with the interpreter directly - as you do with the python.exe:

E:\>c:\path\to\ipy64.exe z.py testing

And not as:

E:\>z.py testing

I think this should be reported as a bug on the IronPython installation.

Sign up to request clarification or add additional context in comments.

1 Comment

That took care of it. Thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.