Your test
script is the first example on the Python argparse documentation. https://docs.python.org/3/library/argparse.html
Your comment with new lines added is
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.
>>> ================================ RESTART ================================
>>> usage: test [-h] [--sum] N [N ...] test: error: too few arguments
>>> $ python test.py 1 2 3 4
SyntaxError: invalid syntax
>>> python test.py 1 2 3 4
SyntaxError: invalid syntax
>>> $ python test.py 1 2 3 4
SyntaxError: invalid syntax
>>> python test.py 1 2 3 4 --sum
SyntaxError: invalid syntax
>>> python test.py 1 2 3 4 --sum
From this I deduce that you saved the script as test
('test.py` would have been better), and ran it, from a Windows command line, as
python -i test
which produces
usage: test [-h] [--sum] N [N ...] test: error: too few arguments
That usage message from the parser
; test
is the name of the script.
I'm not sure about the RESTART
line. My tests (at the end) suggest your Python call (or some default environment feature) includes the -i
option, which leaves you in the interactive Python session, even after the argparse
step fails.
The next command is straight out of the Python example:
>>> $ python test.py 1 2 3 4
SyntaxError: invalid syntax
But the context is all wrong. The docs include $
to indicate that this is being typed in a commandline (Linux shell or Windows commmand). And the meaning, in the correct context is:
- run Python
- tell it to run the test.py script
- and pass it the arguments '1','2', etc
But if you are already inside a Python interpreter (indicated by the >>>
prompt string), this does not make sense. python
and test.py
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.
A little further along, the argparse
documentation gives an example of calling a parser
from within a Python interactive session:
>>> parser.parse_args(['--sum', '7', '-1', '42'])
That has a very different syntax. In this python -i
context it should run.
Going back to the Windows command window and typing
python test 1 2 3 4
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.
=========
Here's an example of running another simple script from a Linux shell. The ...$
is the shell prompt; the >>>
is the python prompt. Adding the -i
to the initial python call ensures it stays in python after parsing.
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 <module>
print(parser.parse_args())
...
SystemExit: 2
>>> python simple.py 1 2
File "<stdin>", line 1
python simple.py 1 2
^
SyntaxError: invalid syntax
The main difference between my test and yours is that I don't get the RESTART
and I get a traceback. Without the -i
I simply get the usage message and a return the command line.
1000:~/mypy$ python simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
1000:~/mypy$