getop or optparse with option with spaces?

David Shapiro David.Shapiro at sas.com
Wed Jun 10 09:47:54 EDT 2009


Unfortunately, I had no luck installing argparse, which is really confusing because I would need to use some old version pre-optik to use I think. The Jython I use is like python 2.2.3.  I spent all of yesterday trying to get either getopt, argparse, or optparse to work.  Even with optparse I had to modify a module.  For example, in textwrap.py, they have 

@ line 124 in the module:

if self.replace_whitespace:
    #if isinstance(text,str):
    #     text = text.translate(self.whitespace_trans)
    #elif isinstances(text,Unicode):
         text = text.translate(self.unicode_whitespace_trans)
     return text

I had to comment out the if isinstance(text,str) and elif.  Is the double quotes supposed to work?
 
-----Original Message-----
From: Javier Collado [mailto:javier.collado at gmail.com] 
Sent: Wednesday, June 10, 2009 9:38 AM
To: David Shapiro
Cc: python-list at python.org
Subject: Re: getop or optparse with option with spaces?

Hello,

It's strange behaviour. Have you tried argparse
(http://code.google.com/p/argparse/)? I've been using it for long time
without any problem like that?

Best regards,
    Javier

2009/6/10 David Shapiro <David.Shapiro at sas.com>:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>




More information about the Python-list mailing list