Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse: document that add_argument returns a Action object #69223

Open
Sworddragon mannequin opened this issue Sep 8, 2015 · 10 comments
Open

argparse: document that add_argument returns a Action object #69223

Sworddragon mannequin opened this issue Sep 8, 2015 · 10 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@Sworddragon
Copy link
Mannequin

Sworddragon mannequin commented Sep 8, 2015

BPO 25035
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2015-09-08.23:01:55.636>
labels = ['type-feature', '3.7', 'docs']
title = 'Getter/setter for argparse keys'
updated_at = <Date 2016-12-12.06:06:04.814>
user = 'https://bugs.python.org/Sworddragon'

bugs.python.org fields:

activity = <Date 2016-12-12.06:06:04.814>
actor = 'rhettinger'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2015-09-08.23:01:55.636>
creator = 'Sworddragon'
dependencies = []
files = []
hgrepos = []
issue_num = 25035
keywords = []
message_count = 8.0
messages = ['250263', '250305', '250330', '250350', '250352', '250353', '250423', '282878']
nosy_count = 5.0
nosy_names = ['bethard', 'r.david.murray', 'docs@python', 'Sworddragon', 'paul.j3']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue25035'
versions = ['Python 3.7']

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Sep 8, 2015

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.

@Sworddragon Sworddragon mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 8, 2015
@bitdancer
Copy link
Member

Can you provide some specific use cases, please?

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Sep 9, 2015

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.

@paulj3
Copy link
Mannequin

paulj3 mannequin commented Sep 10, 2015

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'.

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Sep 10, 2015

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.

@paulj3
Copy link
Mannequin

paulj3 mannequin commented Sep 10, 2015

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.

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Sep 10, 2015

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?

@paulj3
Copy link
Mannequin

paulj3 mannequin commented Dec 10, 2016

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.

@rhettinger rhettinger added docs Documentation in the Doc dir 3.7 (EOL) end of life and removed stdlib Python modules in the Lib dir labels Dec 12, 2016
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel iritkatriel added 3.11 only security fixes easy 3.10 only security fixes 3.12 bugs and security fixes labels Nov 8, 2022
@iritkatriel iritkatriel changed the title Getter/setter for argparse keys argparse: document that add_argument returns a Action object Nov 8, 2022
@hadrizi
Copy link

hadrizi commented Sep 25, 2023

@iritkatriel is this issue still relevant?
looking for a first issue

afaics this is still not documented

@iritkatriel
Copy link
Member

This is probably not a good first issue because the argparse docs need a deeper review and we don’t have an active maintainer at the moment working on that. I will remove the easy label.

@iritkatriel iritkatriel removed easy 3.7 (EOL) end of life labels Sep 27, 2023
@erlend-aasland erlend-aasland added 3.13 new features, bugs and security fixes and removed 3.10 only security fixes labels Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
Status: Features
Development

No branches or pull requests

5 participants