[New-bugs-announce] [issue5088] optparse: inconsistent default value for append actions

Fons Dijkstra report at bugs.python.org
Wed Jan 28 11:58:27 CET 2009

New submission from Fons Dijkstra <fdij at xs4all.nl>:

Although it is possible to specify a default value for an append action,  
either directly or through the set_default() function, it is not handled 
correctly when this default is overruled by the user. Take for example 
the following code:

  import optparse
  parser = optparse.OptionParser()
  parser.add_option("-o", "--option", action = "append")
  parser.set_defaults(option = ["a"])
  options, args = parser.parse_args()
  print options

When this is called without arguments the following is printed:
  {'option': ['a']} # as expected

But when it is called with for example with -ob the following is 
  {'option': ['a', 'b']} # expected {'option': ['b']}

So the default option is also appended, even if the option is explicitly defined by the user.

components: Library (Lib)
messages: 80704
nosy: pycurry
severity: normal
status: open
title: optparse: inconsistent default value for append actions
type: behavior
versions: Python 2.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list