Popular recipes by Andrea Crotti http://code.activestate.com/recipes/users/4179946/2011-12-02T10:25:13-08:00ActiveState Code RecipesGenerate automatically list of actions with argparse (Python)
2011-12-02T10:25:13-08:00Andrea Crottihttp://code.activestate.com/recipes/users/4179946/http://code.activestate.com/recipes/577947-generate-automatically-list-of-actions-with-argpar/
<p style="color: grey">
Python
recipe 577947
by <a href="/recipes/users/4179946/">Andrea Crotti</a>
(<a href="/recipes/tags/argparse/">argparse</a>, <a href="/recipes/tags/cli/">cli</a>).
Revision 2.
</p>
<p>This little sample script looks up in locals() to determine all the callables in <em>main</em>.</p>
<p>Then it passes them to parse_arguments, which show an help message as following
usage:</p>
<pre class="prettyprint"><code>prova.py [-h] {func2,func1}
positional arguments:
{func2,func1}
optional arguments:
-h, --help show this help message and exit
</code></pre>
<p>Useful to avoid duplication and make it easy to extend script functions.</p>