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.

classification
Title: argparse: Document how to use argument names that are not Python identifiers
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.9, Python 3.8, Python 3.7, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Dan Arad, Joseph.Birr-Pixton, bethard, docs@python, eric.araujo, rhettinger
Priority: normal Keywords: easy

Created on 2012-02-28 12:23 by Joseph.Birr-Pixton, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
argparsetest.py Joseph.Birr-Pixton, 2012-02-28 12:23 test case
Messages (8)
msg154550 - (view) Author: Joseph Birr-Pixton (Joseph.Birr-Pixton) Date: 2012-02-28 12:23
Say I have an argument with the name 'foo-bar'.  Argparse accepts and parses arguments, but Namespace does not allow me to access the value.

Yes, I can use getattr or Namespace.__dict__.  But that's ugly.
Yes, I can change the name of the argument, but that's not what I want in my help output.

I think it should either:
- Collapse names to valid python identifiers (optparse did this).
- Namespace should act like an object and dict.
msg154597 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-29 04:13
If argparse munged foo-bar to foo_bar to allow attribute access, then it’d need to disallow ambiguous cases like add_argument('foo_bar', ...); add_argument('foo-bar', ...).  I’m not sure if there is real, sensible code that does that, though.

> Namespace should act like an object and dict.
I don’t understand, can you rephrase?
msg154602 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-02-29 08:11
For optional flags like --foo-bar, argparse does munge the "dest" to "foo_bar", following optparse. For positional arguments, arpgarse doesn't munge things this way, but if you want the argument named "foo-bar" in help messages and "foo_bar" on the Namespace object, you just need to do something like:

add_argument('foo_bar', metavar='foo-bar', ...)

Perhaps the docs could make this clearer.
msg154614 - (view) Author: Joseph Birr-Pixton (Joseph.Birr-Pixton) Date: 2012-02-29 11:19
> I don’t understand, can you rephrase?

Sorry, I mean making Namespace subscriptable.  eg:

>>> v = argparse.Namespace(abc = 123)
>>> v
Namespace(abc=123)
>>> v.abc
123
>>> v['abc']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Namespace' object is not subscriptable

> add_argument('foo_bar', metavar='foo-bar', ...)

This works. Thanks!

Cheers,
Joe
msg154640 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-02-29 15:25
> making Namespace subscriptable

This has been discussed before - see issue 11076. I prefer to keep Namespace as simple as possible. For subscripting, just use the standard Python idiom of vars as suggested in the docs:

http://docs.python.org/library/argparse.html#the-namespace-object
msg223223 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-16 15:38
#11076 was closed as a duplicate of #8982 which was closed as fixed so can this also be closed as a duplicate?
msg361450 - (view) Author: Dan Arad (Dan Arad) Date: 2020-02-05 20:23
Hi everyone!

I thought to take this issue up by updating the documentation. Is this still relevant?

If so I think the best place to add the documentation is in the "name or flags" section, where people will go looking first. I think to add both the metavar and the vars solutions discussed below.
msg361463 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-02-06 00:55
I think this one should have been closed long ago.
History
Date User Action Args
2022-04-11 14:57:27adminsetgithub: 58357
2020-02-06 00:55:10rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg361463

resolution: out of date
stage: needs patch -> resolved
2020-02-05 20:23:46Dan Aradsetnosy: + Dan Arad
messages: + msg361450
2019-04-27 14:22:57fdrakesetversions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.2, Python 3.3
2019-04-26 19:46:29BreamoreBoysetnosy: - BreamoreBoy
2014-07-16 15:38:34BreamoreBoysetnosy: + BreamoreBoy
messages: + msg223223
2012-02-29 15:25:31bethardsetmessages: + msg154640
2012-02-29 11:19:09Joseph.Birr-Pixtonsetmessages: + msg154614
2012-02-29 11:14:56eric.araujosetassignee: docs@python
title: argparse usage model requires argument names to be python identifiers -> argparse: Document how to use argument names that are not Python identifiers
components: + Documentation, - Library (Lib)

keywords: + easy
nosy: + docs@python
versions: + Python 2.7, Python 3.2
stage: needs patch
2012-02-29 08:11:05bethardsetmessages: + msg154602
2012-02-29 04:13:39eric.araujosetnosy: + bethard, eric.araujo
title: Argparse usage model requires argument names to be python identifiers -> argparse usage model requires argument names to be python identifiers
messages: + msg154597

versions: + Python 3.3, - Python 2.7
2012-02-28 12:23:55Joseph.Birr-Pixtoncreate