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>There might be a better way, but I don't see any in the documentation. If you know the parser option you should be able to do:</p>

<pre><code>parser = argparse.ArgumentParser()
parser.add_argument("--location", choices=("here", "there", "everywhere"))

storeaction = next(a for a in parser._actions if "--location" in a.option_strings)

storeaction.choices
# ('here', 'there', 'everywhere')
</code></pre>

<p>As in mgilson's answer, accessing the <code>_actions</code> attribute is undocumented and the underscored prefix means "Hey, you probably shouldn't be messing with me." Don't be surprised if this breaks between versions of Python.</p>

python argparse