optparse: Why does this syntax work and not another?
anthonybaxter at gmail.com
Mon Jul 5 08:49:02 CEST 2004
On Mon, 05 Jul 2004 06:36:46 GMT, washu <me at privacy.net> wrote:
> And everything works. It will print foo.txt. Now, instead of doing the
> parser = optparse.OptionParser() substitution, I spell everything out:
> optparse.OptionParser().add_option( . . .)
> (options, args) = optparse.OptionParser().parse_args( . . . )
> And the script stops with an error message of: "no such option: -f"
Because your code is incorrect. The first of these two lines
creates and OptionParser() instance, and calls 'add_option()'
on it. It then throws away the newly created instance, as you
didn't assign it to anything.
More information about the Python-list