getop or optparse with option with spaces?

David Shapiro David.Shapiro at sas.com
Wed Jun 10 09:08:56 EDT 2009


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?  Another problem is that whereProp value is just not seen. Is there a limit to the size for argv?  

If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way.  A temporary workaround is to use " ".join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces.  Also, it just seem weird I have to do a work around like that.  I could have swore using double quotes should have fixed this issue, but they do not seem to work.

David



More information about the Python-list mailing list