classification
Title: argparse: Document how to use argument names that are not Python identifiers
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: BreamoreBoy, Joseph.Birr-Pixton, bethard, docs@python, eric.araujo
Priority: normal Keywords: easy

Created on 2012-02-28 12:23 by Joseph.Birr-Pixton, last changed 2014-07-16 15:38 by BreamoreBoy.

Files
File name Uploaded Description Edit
argparsetest.py Joseph.Birr-Pixton, 2012-02-28 12:23 test case
Messages (6)
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?
History
Date User Action Args
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