getopt with negative numbers?

Casey Caseyweb at gmail.com
Thu Sep 27 17:55:34 EDT 2007


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>




More information about the Python-list mailing list