This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author SylvainDe
Recipients Markus.Amalthea.Magnuson, SylvainDe, bethard, docs@python, paul.j3, r.david.murray
Date 2014-06-19.11:23:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1403176987.86.0.170251328022.issue16399@psf.upfronthosting.co.za>
In-reply-to
Content
As this is likely not to get solved, is there a recommanded way to work around this issue ?

Here's what I have done :

  import argparse
  def main():
      """Main function"""
      parser = argparse.ArgumentParser()
      parser.add_argument('--foo', action='append')
      for arg_str in ['--foo 1 --foo 2', '']:
          args = parser.parse_args(arg_str.split())
          if not args.foo:
              args.foo = ['default', 'value']
          print(args)

printing

  Namespace(foo=['1', '2'])
  Namespace(foo=['default', 'value'])

as expected but I wanted to know if there a more argparse-y way to do this. I have tried using `set_defaults` without any success.

Also, as pointed out the doc for optparse describes the behavior in a simple way : "The append action calls the append method on the current value of the option. This means that any default value specified must have an append method. It also means that if the default value is non-empty, the default elements will be present in the parsed value for the option, with any values from the command line appended after those default values".
History
Date User Action Args
2014-06-19 11:23:07SylvainDesetrecipients: + SylvainDe, bethard, r.david.murray, docs@python, paul.j3, Markus.Amalthea.Magnuson
2014-06-19 11:23:07SylvainDesetmessageid: <1403176987.86.0.170251328022.issue16399@psf.upfronthosting.co.za>
2014-06-19 11:23:07SylvainDelinkissue16399 messages
2014-06-19 11:23:07SylvainDecreate