Issue 17965: argparse does not dest.replace('-', '_') for positionals (original) (raw)
"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).