[New-bugs-announce] [issue17965] argparse does not dest.replace('-', '_') for postionals

paul j3 report at bugs.python.org
Mon May 13 04:23:29 CEST 2013


New submission from paul j3:

"16.4.3.11. dest
For optional argument actions,... Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name."

In _get_optional_kwargs(), dest = dest.replace('-', '_'); but there is nothing like this in _get_positional_kwargs()

Thus if

   parser.add_argument('foo-bar',...)

this attribute can only be accessed with 

   getattr(namespace, 'foo-bar').

Alternatives:
- ignore this since no one has complained about it
- stress in the documentation that the positionals name should be a valid python attribute name
- test the name during add_argument
- add the dest.replace('-','_') to positionals

But as things stand, even an optional can be given a weird dest - 

    a=p.add_argument('--foo*[]-bar')

requiring

    getattr(args,'foo*[]_bar')

I assume that optionals have this dash replacement because historically some unix (or other) programs have accepted options like '--foo-bar' (though off hand I can't think of any).

----------
components: Library (Lib)
messages: 189090
nosy: paul.j3
priority: normal
severity: normal
status: open
title: argparse does not dest.replace('-', '_')  for postionals
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17965>
_______________________________________


More information about the New-bugs-announce mailing list