python and getopt and spaces in option

D

David Shapiro

Hello,

I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows:

try:
(opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v",
["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"])
except getopt.GetoptError:
usage()

for opt in opts:
(key, value) = opt
if (key in ("-v", "--version")):
print "Version: " + version
sys.exit(1)
if (key in ("-h", "--help")):
usage()
if (key in ("-s", "--server")):
server = value
if (key in ("-p", "--port")):
port = value
if (key in ("-n", "--dsName")):
dsName = value
if (key in ("-j", "--jndiName")):
jndiName = value
if (key in ("-d", "--driverName")):
driverName = value
if (key in ("-l", "--driverURL")):
driverURL = value
if (key in ("-u", "--user")):
user = value
if (key in ("-c", "--passWD")):
passWD = value
if (key in ("-t", "--targetServer")):
targetServer = value
if (key in ("-q", "--testTableName")):
testTableName = value
if (key in ("-w", "--whereProp")):
whereProp = value


print "server: " + server
print "port: " + port
print "dsName: " + dsName
print "jndiName: " + jndiName
print "driverName: " + driverName
print "driverURL: " + driverURL
print "user: " + user
print "passWD: " + passWD
print "testtable: " + testTableName
print "targetServer: " + targetServer
print "whereProp: " + whereProp

The one that gives me trouble is with the -q option, because it can look like: -q "SQL 1 TABLE". It returns back just SQL. How do I get it to return the whole thing that is in double quotes?

David
 
J

Jorgen Grahn

I have been trying to find an example of how to deal with options
that have spaces in them. I am using jython, which is the same I
think as python 2.2.3. I feebly tried to use optparse and argparse
with no success (got gettext, locale, and optparse). The code is as
follows:
try:
(opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v",
["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"])
except getopt.GetoptError:
usage()

for opt in opts:
(key, value) = opt ....
if (key in ("-q", "--testTableName")): ....

The one that gives me trouble is with the -q option, because it can
look like: -q "SQL 1 TABLE". It returns back just SQL. How do I get
it to return the whole thing that is in double quotes?

You are probably calling the program incorrectly. A non-broken getopt has no
trouble with such things. When executing a program from a normal Unix
shell, single or double quotes (like you do above) is enough. I'd expect
other environments to behave in the same way.

If -q only eats the string "SQL", where does "1 TABLE" go? It cannot
just disappear; does it end up in 'args'?

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,297
Latest member
EngineerD

Latest Threads

Top