3

I met a strange behaviour with my python code today. I wrote the following small program to illustrate.

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--list1", "-l1", nargs='+',  help="liste 1",   metavar="THE_LIST")

args = parser.parse_args()

if args.list1:
    print("list1:" + str(args.list1))

I ran the code on a first server. I got the expected behaviour (list1 contains a string that includes '-V').

$ uname -a
Linux computer 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
$ python  --version
Python 2.7.3
$ cat test.py
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--list1", "-l1",  help="liste 1", metavar="THE_LIST")

args = parser.parse_args()

if args.list1:
    print("list1:" + str(args.list1))
$ python test.py -l1 "abc -V def"
list1:abc -V def

I ran it on a second server (Centos 6.6) and got an error. The hyphen seems to be considered as part of an additional argument although the value for -l1 is still enclosed with double quotes... Any idea would be really appreciated...

$ uname -a
Linux sacapus 2.6.32-504.12.2.el6.x86_64 #1 SMP Wed Mar 11 22:03:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ python --version
Python 2.7.9
$ cat test.py 
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--list1", "-l1",  help="liste 1", metavar="THE_LIST")

args = parser.parse_args()

if args.list1:
    print("list1:" + str(args.list1))
$ python test.py -l1 "abc -V def"
usage: test.py [-h] [--list1 THE_LIST]
test.py: error: unrecognized arguments: -V def
3
  • Seems to me like this is much more likely to be an issue with your shell/OS than with Python. Added some tags, please feel free to modify if that helps Commented Apr 27, 2015 at 18:40
  • Works for me on Python 2.7.6 using bash, ksh, csh, and fish. Commented Apr 27, 2015 at 19:23
  • I have to agree with @IanClark; it appears to be a shell issue. I usually run Linux, but happened to be on a Windows machine when I came across this. I can't replicate the error under Windows. Commented Apr 27, 2015 at 19:23

1 Answer 1

1

This really shouldn't happen. To figure out what's going on, I would include

print(repr(sys.argv))

at the beginning of the script, to isolate if the "abc -V def" string is really passed as a single argument by the shell.

If it arrives in your program as a single argument blame argparse, otherwise your shell is messing up. Either way, this seems like something that should become an entry in CentOS's bug database.

FWIW, I can't reproduce this on Debian Jessie with Python 2.7.9:

greek0@orest:/tmp$ python2.7 a.py -l1 "abc -V def"
list1:['abc -V def']
Sign up to request clarification or add additional context in comments.

9 Comments

Adding print(repr(sys.argv)) give me ['test.py', '-l1', 'abc', '-V', 'def'] on the centos server.... It may be related to the Python installation on Centos. I got the same error when using the Python 3.3.5 version installed on this server...
So what does print(repr(sys.argv)) print?
I got ['test.py', '-l1', 'abc', '-V', 'def'] on the second server while it gives me the expected output (['test.py', '-l1', 'abc -V def']) on the first.
That's it. The python executable was replaced with a shell script that contains: #!/bin/sh LD_LIBRARY_PATH=/Softs/users/Python-2.7.9/binaries/lib/ /Softs/users/Python-2.7.9/binaries/bin/python2.7ld (where python2.7ld is the original python executable)
@ChristianAichinger: the simplest was to find out which shell you are using is ps (no parameters).
|

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.