Title: Getter/setter for argparse keys
Type: enhancement Stage:
Components: Documentation Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Sworddragon, bethard, docs@python, paul.j3, r.david.murray
Priority: normal Keywords:

Created on 2015-09-08 23:01 by Sworddragon, last changed 2016-12-12 06:06 by rhettinger.

Messages (8)
msg250263 - (view) Author: (Sworddragon) Date: 2015-09-08 23:01
On making a look at the argparse documentation to figure out if I can get the value of the choices key of a specific argument after argument parsing or if I have to implement it myself I noticed that there is a getter/setter for the default key (ArgumentParser.get_default and ArgumentParser.set_defaults) but not for the other keys. Maybe this could also be implemented for the other keys, probably as a generic function that takes as an extra argument the requested key.
msg250305 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-09-09 13:05
Can you provide some specific use cases, please?
msg250330 - (view) Author: (Sworddragon) Date: 2015-09-09 20:13
I was myself in the case where I needed the values of the choices key of 2 specific arguments. Currently I'm solving this by storing them in variables but probably it could be cleaner by using a getter.
msg250350 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-09-10 02:50
`parser.set_defaults` lets you set a default for any `dest`.  It does not have to be connected with an argument.  See what the docs say about using it to set an action linked to a subparser.

`arg1 = parser.add_argument(...)` returns an `Action` object.  The parser collects these objects in its lists, but you can also save a reference to it yourself.

I'd suggest doing this in an interactive shell, and see for yourself the attributes of this object.  Simply doing a 'print' on the object shows a number of the attributes (but not all).  It is possible to view, and in some cases, even modify these attributes directly.

`args.choices` gives you access to the `choices` attribute.

I don't think there's a need for getter/setter methods for Action attributes.  The attributes are accessible by normal Python object approaches.

p.s. Another forum for asking argparse how-to questions is Stackoverflow.  Just tag it 'argparse'.
msg250352 - (view) Author: (Sworddragon) Date: 2015-09-10 03:41
I'm actually not fully sure why you are telling me this all, especially in this specific way.

But I would also go the other way, by removing ArgumentParser.get_default and ArgumentParser.set_defaults if we think the current ways of getting/setting are enough. Mainly I think consistence is the important here.
msg250353 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-09-10 04:24
`get_default` and `set_defaults` are parser methods, and do more than set or get a particular Action attribute.  'Set' for example changes both the 'parser._defaults' attribute, and a 'action.default' attribute.  Defaults are complex and can be defined in at least 3 different ways.

'choices' (and other things like 'required', 'nargs', even 'default') is an attribute of a specific Action (argument object).  You normally only set these in one way, as parameters of the `add_argument` method.

'action.default' and 'action.choices' are 'public' attributes.  'parser._defaults' is a 'private' attribute.  A setter method is the right way to change a 'private' attribute (if needed).  It usually isn't need for a 'public' one.
msg250423 - (view) Author: (Sworddragon) Date: 2015-09-10 21:55
In this case probably all is fine then. But there is a minor thing I noticed from one of your previous posts: You said parser.add_argument returns an Action object but I noticed that this is not mentioned on "16.4.3. The add_argument() method". Is it maybe mentioned somewhere else or probably just missing?
msg282878 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2016-12-10 21:30
Yes, the information that `add_argument` returns a Action object is missing from the documentation.  It might be useful addition.

The documentation has many faults.  It isn't basic enough to be a tutorial, but many first time users use it as such, and get confused by its complexity.  But it also is not a formal reference document. And the automatic 'help(argparse)' isn't much better.

A regular user does not need to use the Action object directly.  But as a developer I often test argparse in an interactive shell (IPython), and the 'out' line shows a 'str' of the object (as a summary of its attributes).  So assigning that object to a variable is just as natural to me as saving the parser object returned by argparse.ArgumentParser, or the group object returned by parser.add_argument_group.
Date User Action Args
2016-12-12 06:06:04rhettingersetnosy: + docs@python, bethard

assignee: docs@python
components: + Documentation, - Library (Lib)
versions: + Python 3.7, - Python 3.4
2016-12-10 21:30:30paul.j3setmessages: + msg282878
2015-09-10 21:55:03Sworddragonsetmessages: + msg250423
2015-09-10 04:24:08paul.j3setmessages: + msg250353
2015-09-10 03:41:08Sworddragonsetmessages: + msg250352
2015-09-10 02:50:45paul.j3setnosy: + paul.j3
messages: + msg250350
2015-09-09 20:13:30Sworddragonsetmessages: + msg250330
2015-09-09 13:05:24r.david.murraysetnosy: + r.david.murray
messages: + msg250305
2015-09-08 23:01:55Sworddragoncreate