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.
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:27 | admin | set | github: 58357 |
| 2020年02月06日 00:55:10 | rhettinger | set | status: open -> closed nosy: + rhettinger messages: + msg361463 resolution: out of date stage: needs patch -> resolved |
| 2020年02月05日 20:23:46 | Dan Arad | set | nosy:
+ Dan Arad messages: + msg361450 |
| 2019年04月27日 14:22:57 | fdrake | set | versions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.2, Python 3.3 |
| 2019年04月26日 19:46:29 | BreamoreBoy | set | nosy:
- BreamoreBoy |
| 2014年07月16日 15:38:34 | BreamoreBoy | set | nosy:
+ BreamoreBoy messages: + msg223223 |
| 2012年02月29日 15:25:31 | bethard | set | messages: + msg154640 |
| 2012年02月29日 11:19:09 | Joseph.Birr-Pixton | set | messages: + msg154614 |
| 2012年02月29日 11:14:56 | eric.araujo | set | assignee: 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:05 | bethard | set | messages: + msg154602 |
| 2012年02月29日 04:13:39 | eric.araujo | set | nosy:
+ 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:55 | Joseph.Birr-Pixton | create | |