python and getopt and spaces in option

Jorgen Grahn grahn+nntp at snipabacken.se
Wed Jun 10 18:32:45 EDT 2009


On Tue, 9 Jun 2009 12:22:20 -0400, David Shapiro <David.Shapiro at sas.com> wrote:
> 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

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.se>          R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list