classification
Title: argparse does not dest.replace('-', '_') for positionals
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: argparse: positional arguments containing - in name not handled well
View: 15125
Assigned To: Nosy List: bethard, paul.j3, r.david.murray
Priority: normal Keywords:

Created on 2013-05-13 02:23 by paul.j3, last changed 2013-05-17 15:30 by r.david.murray. This issue is now closed.

Messages (3)
msg189090 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-05-13 02:23
"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).
msg189457 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-05-17 14:27
This is a duplicate of issue 15125. (FYI there are many many unix commands that use options of the form -XXX-YYYY.  This is the standard for the gnu 'long option' format.)
msg189462 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-05-17 15:30
That should have been --XXX-YYY, of course.
History
Date User Action Args
2013-05-17 15:30:40r.david.murraysetmessages: + msg189462
2013-05-17 14:27:19r.david.murraysetstatus: open -> closed

superseder: argparse: positional arguments containing - in name not handled well

nosy: + r.david.murray
messages: + msg189457
resolution: duplicate
stage: resolved
2013-05-15 21:29:40terry.reedysetnosy: + bethard
2013-05-13 06:45:16paul.j3settitle: argparse does not dest.replace('-', '_') for postionals -> argparse does not dest.replace('-', '_') for positionals
2013-05-13 02:23:29paul.j3create