Accessing the choices passed to argument in argparser?
<p>Is it possible to access the tuple of choices passed to an argument? If so, how do I go about it</p>

<p>for example if I have </p>

<pre><code>parser = argparse.ArgumentParser(description='choose location')
parser.add_argument(
    "--location",
    choices=('here', 'there', 'anywhere')
)
args = parser.parse_args()
</code></pre>

<p>can I access the tuple <code>('here', 'there', 'anywhere')</code>?</p>

<p>On the question of what <code>add_argument</code> returns, if you do any testing in an interactive session like <code>ipython</code>, the return stares you in the face:</p>

<pre><code>In [73]: import argparse
In [74]: parser=argparse.ArgumentParser()
In [75]: parser.add_argument('foo',choices=['one','two','three'])
Out[75]: _StoreAction(option_strings=[], dest='foo', nargs=None, const=None, default=None, type=None, choices=['one', 'two', 'three'], help=None, metavar=None)
In [76]: _.choices
Out[76]: ['one', 'two', 'three']
</code></pre>

<p>Note that other methods like <code>add_argument_group</code>, <code>add_subparsers</code>, <code>add_parser</code>, <code>add_mutually_exclusive_group</code> all return objects that can be used.  The fact that <code>add_argument</code> is not documented as returning an object is, I think, a documentation oversight.  Usually users don't need to use it, but as a semi-developer I use it all the time.  The documentation for <code>argparse</code> is not a formal specification of what the module can or can not do; it is more of an instruction manual, a step up from a tutorial, but clearly not a reference.</p>

<p>The use of <code>parser._actions</code> is handy, but a step deeper into the guts.  I have followed nearly all the bug/issues and can't think of any that would trigger a change in this.  There's a big backlog of potential changes, but developers have been reduced to near-immobility over fears of creating backward compatibility issues.  It is easier to change the documentation than to change the functionality of <code>argparse</code>.</p>

python argparse