[issue34188] Allow dict choices to "transform" values in argpagse

paul j3 report at bugs.python.org
Sat Aug 18 16:55:45 EDT 2018


paul j3 <ajipanca at gmail.com> added the comment:

The 'choices' mechanism is quite simple.  As noted for testing it simply uses an 'in' test.  

For help formating it uses

     choice_strs = [str(choice) for choice in action.choices]
     result = '{%s}' % ','.join(choice_strs)

In other words, it's treated as a iterable. 

But it is easy to produce unmanageable displays, such as with 'range(100)'.  This has been raised in other bug/issues.  The best way around that is with the 'metavar', which lets you customize the 'usage' and 'help'.  One thing that 'metavar' does not help with is the error display.

I'm not privy to the original author's thinking, but I don't think 'choices' was ever meant to be a high power tool.  

With a custom Action you can do almost anything that you could do after parsing.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34188>
_______________________________________


More information about the Python-bugs-list mailing list