[issue25061] Add native enum support for argparse

2019-08-29 Thread Raymond Hettinger
Raymond Hettinger added the comment: Even with the proposed converter class, I don't see a straight-forward way to meet the OP's goal of just specifying type=EnumConverter(MyEnum) to get all of: * display all possible values in help * display them in lowercase * accept them in lowercase * and

[issue25061] Add native enum support for argparse

2019-08-29 Thread Raymond Hettinger
Raymond Hettinger added the comment: Depending on how you want to expose enums to end-users, some reasonable options already exist: import argparse from enum import Enum class Shake(Enum): VANILLA = 7 CHOCOLATE = 4 COOKIES = 9 MINT = 3 # Optio

[issue25061] Add native enum support for argparse

2019-08-29 Thread Raymond Hettinger
Change by Raymond Hettinger : -- assignee: bethard -> rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscrib

[issue25061] Add native enum support for argparse

2016-04-11 Thread Raymond Hettinger
Raymond Hettinger added the comment: Enum is just one tool among many, no more special than named tuples, nested dicts, string.Templates, regexes, pickles etc. The second issue is keeping the scope of argparse focused on its core task rather than trying to incorporate other parts of the stan

[issue25061] Add native enum support for argparse

2016-04-11 Thread paul j3
paul j3 added the comment: desbma: Rereading your latest code and comment: > * The meaning of the 'type' parameter for StoreEnumAction is somewhat > different than for other actions (enum class vs callable that validates) it occurred to me that that parameter does not have to be named 'type'.

[issue25061] Add native enum support for argparse

2016-04-11 Thread paul j3
paul j3 added the comment: The best way to get an idea added is to write a good complete patch. 2nd best is to provide constructive input on ideas that are already here. 3rd is to illustrate how you would hope to use such a feature. Include ideas on how the usage/help/error display would wo

[issue25061] Add native enum support for argparse

2016-04-10 Thread leycec
leycec added the comment: I strongly support this feature request. Unsurprisingly, I wholeheartedly agree with desbma's heroic persistence and wholeheartedly disagree with rhettinger's curt dismissal. > IMO, this adds more complexity than it solves. Strongly disagree. Because "argparse" fails

[issue25061] Add native enum support for argparse

2015-11-04 Thread paul j3
paul j3 added the comment: The choice of 'type' for this parameter is occasionally confusing, because the connection to the Python 'type()' function or what we think of as 'native types' is only tangential. A name like 'converter' or 'string_converter' would be more accurate (but I'm not advo

[issue25061] Add native enum support for argparse

2015-11-04 Thread desbma
desbma added the comment: I guess the question is whether Enum should be considered a first class 'native' type that deserves support in argparse, or just a tool among others in the stdlib. The fact that Enum is implemented as a class, and lives in a module, tends to lead to the second, but t

[issue25061] Add native enum support for argparse

2015-11-03 Thread Raymond Hettinger
Raymond Hettinger added the comment: IMO, this adds more complexity than it solves. Argparse already has more options than people can remember. Also, it isn't clear whether this logic should go into the parser or into the business logic (consider for example that the requests package doesn'

[issue25061] Add native enum support for argparse

2015-11-03 Thread desbma
desbma added the comment: I came up with something that satisfies my needs (no boilerplate code, and intuitive add_argument call). I modified your factory, and defined a simple action class (this is a quick and dirty prototype for discussion, I am in no way asking that such thing should be me

[issue25061] Add native enum support for argparse

2015-09-17 Thread desbma
desbma added the comment: Thanks for sharing this code, I like the factory idea. I'll have a look at creating a custom Action class too. -- ___ Python tracker ___ __

[issue25061] Add native enum support for argparse

2015-09-14 Thread paul j3
paul j3 added the comment: Here's a EnumType factory class, modeled on FileType. class EnumType(object): """Factory for creating enum object types """ def __init__(self, enumclass): self.enums = enumclass def __call__(self, astring): name = self.enums.__name__

[issue25061] Add native enum support for argparse

2015-09-14 Thread desbma
desbma added the comment: > With my type function, the string input is converted to an enum object and > that is stored in the Namespace. You can't be any more direct than that. Yes I know, but in that case it's missing the autogenerated help message with the possible choices. I know I can g

[issue25061] Add native enum support for argparse

2015-09-13 Thread paul j3
paul j3 added the comment: I'm not quite sure what you mean by 'the enum type to be stored directly'. With my type function, the string input is converted to an enum object and that is stored in the Namespace. You can't be any more direct than that. Or are you thinking that `argparse` has som

[issue25061] Add native enum support for argparse

2015-09-13 Thread desbma
desbma added the comment: I would like the enum type to be stored directly. With your approach, the user does not know what are the possible choices, until he/she tries a invalid value and get the exception. If I pass the enum type to the choice parameter, the help message is not very user fri

[issue25061] Add native enum support for argparse

2015-09-10 Thread paul j3
paul j3 added the comment: Here's a type function that enumerates the enum in the error message: def enumtype(astring): try: return CustomEnumType[astring.upper()] except KeyError: msg = ', '.join([t.name.lower() for t in CustomEnumType]) msg = 'CustomEnumType: us

[issue25061] Add native enum support for argparse

2015-09-10 Thread paul j3
paul j3 added the comment: The `type` parameter is a *function* that takes a string, and returns a valid value. If it can't convert the string it is supposed to raise an error. The default one is a do-nothing identity (take a string, return the string). `int` is the Python function that con

[issue25061] Add native enum support for argparse

2015-09-10 Thread desbma
Changes by desbma : -- components: +Library (Lib) ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.

[issue25061] Add native enum support for argparse

2015-09-10 Thread Barry A. Warsaw
Changes by Barry A. Warsaw : -- nosy: +barry ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.pytho

[issue25061] Add native enum support for argparse

2015-09-10 Thread desbma
New submission from desbma: I often find myself using the following pattern with argparse: import argparse import enum CustomEnumType = enum.Enum("CustomEnumType", ("VAL1", "VAL2", "VAL3", ...)) arg_parser = argparse.ArgumentParser(...) ... arg_parser.add_argument("-