I don’t think I’m even using funky characters – just trying to pass in a “-d” but my dash seems to get munged. If I just print sys.argv[1] it looks okay, but if I print the entire list sys.argv, I can see funky characters instead of my dash.
Mac OSX 10.6.8
Python 2.6.1
#!/usr/bin/env python
import sys
if __name__ == "__main__":
try:
print "SVH FLAG sys.argv ",sys.argv
num_args = len(sys.argv)
for i in range(0,num_args):
print "SVH FLAG sys.argv[",i,"] ",sys.argv[i]
except:
print "problem with sys.argv"
Which gives me this when I call it with -d:
./deleteme.py –d /Library/Python/2.6/site-packages
SVH FLAG sys.argv ['./deleteme.py', '\xe2\x80\x93d', '/Library/Python/2.6/site-packages']
SVH FLAG sys.argv[ 0 ] ./deleteme.py
SVH FLAG sys.argv[ 1 ] –d
SVH FLAG sys.argv[ 2 ] /Library/Python/2.6/site-packages
That funky string on the first line of output seems to really mess up something like optparse, which doesn’t see my dash.
Is there something I need to tell sys to give me a normal looking argv?
Thanks in advance!
Somehow, you are not typing a hyphen, but an actual dash character (option -), Unicode 8211, whose UTF-8 representation is the three-byte string
\xe2\x80\x93. We always refer to the character that precedes command-line options as a “dash”, but it’s really a hyphen!