getopt with negative numbers?

Neal Becker ndbecker2 at gmail.com
Fri Sep 28 01:57:03 CEST 2007


Casey wrote:

> On Sep 27, 2:21 pm, "J. Clifford Dyer" <j... at sdf.lonestar.org> wrote:
>> If you can access the argument list manually, you could scan it for a
>> negative integer, and then insert a '--' argument before that,
>> if needed, before passing it to getopt/optparse.  Then you wouldn't have
>> to worry about it on the command line.
>>
>> Cheers,
>> Cliff
> 
> Brilliant!
> 
> <code>
>     # Look for the first negative number (if any)
>     for i,arg in enumerate(sys.argv[1:]):
>         # stop if
>         if arg[0] != "-": break
>         # if a valid number is found insert a "--" string before it
> which
>         # explicitly flags to getopt the end of options
>         try:
>             f = float(arg)
>             sys.argv.insert(i+1,"--")
>             break;
>         except ValueError:
>             pass
> </code>
> 

One person's "brilliant" is another's "kludge".




More information about the Python-list mailing list