Correct way to get allowed arguments from ArgumentParser
<p><strong>Question:</strong> What is the intended / official way of accessing possible arguments from an existing <code>argparse.ArgumentParser</code> object?</p>

<p><strong>Example:</strong> Let's assume the following context:</p>

<pre><code>import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', '-f', type=str)
</code></pre>

<p>Here I'd like to get the following list of allowed arguments:</p>

<pre><code>['-h', '--foo', '--help', '-f']
</code></pre>

<p>I found the following workaround which does the trick for me</p>

<pre><code>parser._option_string_actions.keys()
</code></pre>

<p>But I'm not happy with it, as it involves accessing a <code>_</code>-member that is not officially documented. Whats the correct alternative for this task?</p>

<p>I don't think there is a "better" way to achieve what you want.</p>

<hr>

<p>If you really don't want to use the <code>_option_string_actions</code> attribute, you could process the <code>parser.format_usage()</code> to retrieve the options, but doing this, you will get only the short options names.</p>

<p>If you want both short and long options names, you could process the <code>parser.format_help()</code> instead.</p>

<p>This process can be done with a very simple regular expression: <code>-+\w+</code></p>

<pre><code>import re

OPTION_RE = re.compile(r"-+\w+")
PARSER_HELP = """usage: test_args_2.py [-h] [--foo FOO] [--bar BAR]

optional arguments:
  -h, --help         show this help message and exit
  --foo FOO, -f FOO  a random options
  --bar BAR, -b BAR  a more random option
"""

options = set(OPTION_RE.findall(PARSER_HELP))

print(options)
# set(['-f', '-b', '--bar', '-h', '--help', '--foo'])
</code></pre>

<hr>

<p>Or you could first make a dictionnary which contains the argument parser configuration and then build the argmuent parser from it. Such a dictionnary could have the option names as key and the option configuration as value. Doing this, you can access the options list via the dictionnary keys flattened with <a href="https://docs.python.org/2/library/itertools.html?highlight=itertools#itertools.chain" rel="nofollow">itertools.chain</a>:</p>

<pre><code>import argparse
import itertools

parser_config = {
    ('--foo', '-f'): {"help": "a random options", "type": str},
    ('--bar', '-b'): {"help": "a more random option", "type": int, "default": 0}
}

parser = argparse.ArgumentParser()
for option, config in parser_config.items():
    parser.add_argument(*option, **config)

print(parser.format_help())
# usage: test_args_2.py [-h] [--foo FOO] [--bar BAR]
# 
# optional arguments:
#   -h, --help         show this help message and exit
#   --foo FOO, -f FOO  a random options
#   --bar BAR, -b BAR  a more random option

print(list(itertools.chain(*parser_config.keys())))
# ['--foo', '-f', '--bar', '-b']
</code></pre>

<p>This last way is what I would do, if I was reluctant to use <code>_option_string_actions</code>.</p>

python argparse