Python program with arguments on Windows
<p>How to run this program saved in the file test.py on Windows XP with python 2.7 installed.</p>

<pre><code>import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',const=sum, default=max,help='sum the integers (default: find the max)')
args = parser.parse_args()
print args.accumulate(args.integers)
</code></pre>

<p>I tried to run it with command line. For example</p>

<pre><code>$ python test.py 1 2 3 4
</code></pre>

<p>or</p>

<pre><code>$ python test.py 1 2 3 4 --sum
</code></pre>

<p>gives error "invalid syntax".</p>

<p>Your <code>test</code> script is the first example on the Python argparse documentation.   <a href="https://docs.python.org/3/library/argparse.html" rel="nofollow">https://docs.python.org/3/library/argparse.html</a></p>

<p>Your comment with new lines added is</p>

<pre><code>Python 2.7.8 (default, Jun 30 2014, 16:03:49) 
[MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
&gt;&gt;&gt; ================================ RESTART ================================ 
&gt;&gt;&gt; usage: test [-h] [--sum] N [N ...] test: error: too few arguments 
&gt;&gt;&gt; $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
&gt;&gt;&gt; python test.py 1 2 3 4 
SyntaxError: invalid syntax 
&gt;&gt;&gt; $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
&gt;&gt;&gt; python test.py 1 2 3 4 --sum 
SyntaxError: invalid syntax 
&gt;&gt;&gt; python test.py 1 2 3 4 --sum
</code></pre>

<p>From this I deduce that you saved the script as <code>test</code> ('test.py` would have been better), and ran it, from a Windows command line, as</p>

<pre><code>python -i test
</code></pre>

<p>which produces</p>

<pre><code>usage: test [-h] [--sum] N [N ...] test: error: too few arguments
</code></pre>

<p>That usage message from the <code>parser</code>; <code>test</code> is the name of the script.</p>

<p>I'm not sure about the <code>RESTART</code> line.  My tests (at the end) suggest your Python call (or some default environment feature) includes the <code>-i</code> option, which leaves you in the interactive Python session, even after the <code>argparse</code> step fails.</p>

<p>The next command is straight out of the Python example:</p>

<pre><code>&gt;&gt;&gt; $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
</code></pre>

<p>But the context is all wrong.  The docs include <code>$</code> to indicate that this is being typed in a commandline (Linux shell or Windows commmand).  And the meaning, in the correct context is:</p>

<ul>
<li>run Python</li>
<li>tell it to run the test.py script</li>
<li>and pass it the arguments '1','2', etc</li>
</ul>

<p>But if you are already inside a Python interpreter (indicated by the <code>&gt;&gt;&gt;</code> prompt string), this does not make sense.  <code>python</code> and <code>test.py</code> are strings that don't have a default meaning inside Python.  So the interpreter gives you a syntax error.  And none of the variations fix that.</p>

<p>A little further along, the <code>argparse</code> documentation gives an example of calling a <code>parser</code> from within a Python interactive session:</p>

<pre><code>&gt;&gt;&gt; parser.parse_args(['--sum', '7', '-1', '42'])
</code></pre>

<p>That has a very different syntax.  In this <code>python -i</code> context it should run.</p>

<p>Going back to the Windows command window and typing</p>

<pre><code>python test 1 2 3 4
</code></pre>

<p>has a better chance of working.  If that doesn't work, then you/we need to focus on running an even more basic Python script.</p>

<p>=========</p>

<p>Here's an example of running another simple script from a Linux shell.  The <code>...$</code> is the shell prompt; the <code>&gt;&gt;&gt;</code> is the python prompt.  Adding the <code>-i</code> to the initial python call ensures it stays in python after parsing.</p>

<pre><code>0957:~/mypy$ python -i simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
Traceback (most recent call last):
  File "simple.py", line 4, in &lt;module&gt;
    print(parser.parse_args())
   ...
SystemExit: 2
&gt;&gt;&gt; python simple.py 1 2
  File "&lt;stdin&gt;", line 1
    python simple.py 1 2
                ^
SyntaxError: invalid syntax
</code></pre>

<p>The main difference between my test and yours is that I don't get the <code>RESTART</code> and I get a traceback.  Without the <code>-i</code> I simply get the usage message and a return the command line.</p>

<pre><code>1000:~/mypy$ python simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
1000:~/mypy$
</code></pre>

python argparse