argparse: flatten the result of action='append' : <p>I'd like to make a script that supports an argument list of the form</p>

<pre><code>./myscript --env ONE=1,TWO=2 --env THREE=3
</code></pre>

<p>Here's my attempt:</p>

<pre><code>import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
    '--env',
    type=lambda s: s.split(','),
    action='append',
)
options = parser.parse_args()
print options.env

$ ./myscript --env ONE=1,TWO=2 --env THREE=3
[['ONE=1', 'TWO=2'], ['THREE=3']]
</code></pre>

<p>Sure I can fix this in postprocessing:</p>

<pre><code>options.env = [x for y in options.env for x in y]
</code></pre>

<p>but I'm wondering if there's some way to get the flattened list directly from argparse, so that I don't have to maintain a list of "things I need to flatten afterwards" in my head as I'm adding new options to the program.</p>

<p>The same question applies if I were to use <code>nargs='*'</code> instead of <code>type=lambda...</code>.</p>

<pre><code>import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
    '--env',
    nargs='+',
    action='append',
)
options = parser.parse_args()
print options.env

$ ./myscript --env ONE=1 TWO=2 --env THREE=3
[['ONE=1', 'TWO=2'], ['THREE=3']]
</code></pre>
