<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Module: core.magic_arguments — IPython 2.3.0 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '2.3.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="IPython 2.3.0 documentation" href="../../index.html" /> <link rel="up" title="The IPython API" href="../index.html" /> <link rel="next" title="Module: core.magics" href="IPython.core.magics.html" /> <link rel="prev" title="Module: core.magic" href="IPython.core.magic.html" /> </head> <body> <div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px"> <a href="http://ipython.org/"><img src="../../_static/logo.png" border="0" alt="IPython Documentation"/></a> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="IPython.core.magics.html" title="Module: core.magics" accesskey="N">next</a> |</li> <li class="right" > <a href="IPython.core.magic.html" title="Module: core.magic" accesskey="P">previous</a> |</li> <li><a href="http://ipython.org">home</a>| </li> <li><a href="../../search.html">search</a>| </li> <li><a href="../../index.html">documentation </a> »</li> <li><a href="../index.html" accesskey="U">The IPython API</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Module: <tt class="docutils literal"><span class="pre">core.magic_arguments</span></tt></a><ul> <li><a class="reference internal" href="#classes">8 Classes</a></li> <li><a class="reference internal" href="#functions">3 Functions</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="IPython.core.magic.html" title="previous chapter">Module: <tt class="docutils literal"><span class="pre">core.magic</span></tt></a></p> <h4>Next topic</h4> <p class="topless"><a href="IPython.core.magics.html" title="next chapter">Module: <tt class="docutils literal"><span class="pre">core.magics</span></tt></a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/api/generated/IPython.core.magic_arguments.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-IPython.core.magic_arguments"> <span id="module-core-magic-arguments"></span><h1>Module: <tt class="xref py py-mod docutils literal"><span class="pre">core.magic_arguments</span></tt><a class="headerlink" href="#module-IPython.core.magic_arguments" title="Permalink to this headline">¶</a></h1> <p>A decorator-based method of constructing IPython magics with <cite>argparse</cite> option handling.</p> <p>New magic functions can be defined like so:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">IPython.core.magic_arguments</span> <span class="kn">import</span> <span class="p">(</span><span class="n">argument</span><span class="p">,</span> <span class="n">magic_arguments</span><span class="p">,</span> <span class="n">parse_argstring</span><span class="p">)</span> <span class="nd">@magic_arguments</span><span class="p">()</span> <span class="nd">@argument</span><span class="p">(</span><span class="s">'-o'</span><span class="p">,</span> <span class="s">'--option'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'An optional argument.'</span><span class="p">)</span> <span class="nd">@argument</span><span class="p">(</span><span class="s">'arg'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'An integer positional argument.'</span><span class="p">)</span> <span class="k">def</span> <span class="nf">magic_cool</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span> <span class="sd">""" A really cool magic command.</span> <span class="sd">"""</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parse_argstring</span><span class="p">(</span><span class="n">magic_cool</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span> <span class="o">...</span> </pre></div> </div> <p>The <cite>@magic_arguments</cite> decorator marks the function as having argparse arguments. The <cite>@argument</cite> decorator adds an argument using the same syntax as argparse’s <cite>add_argument()</cite> method. More sophisticated uses may also require the <cite>@argument_group</cite> or <cite>@kwds</cite> decorator to customize the formatting and the parsing.</p> <p>Help text for the magic is automatically generated from the docstring and the arguments:</p> <div class="highlight-python"><div class="highlight"><pre>In[1]: %cool? %cool [-o OPTION] arg A really cool magic command. positional arguments: arg An integer positional argument. optional arguments: -o OPTION, --option OPTION An optional argument. </pre></div> </div> <p>Inheritance diagram:</p> <p class="graphviz"> digraph inheritance20cd358b90 { rankdir=LR; size="8.0, 12.0"; "argparse.ArgumentParser" [style="setlinewidth(0.5)",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Object for parsing command line strings into Python objects.",height=0.25,shape=box,fontsize=10]; "argparse.HelpFormatter" [style="setlinewidth(0.5)",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Formatter for generating usage messages and argument help strings.",height=0.25,shape=box,fontsize=10]; "argparse.RawDescriptionHelpFormatter" [style="setlinewidth(0.5)",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Help message formatter which retains any formatting in descriptions.",height=0.25,shape=box,fontsize=10]; "argparse.HelpFormatter" -> "argparse.RawDescriptionHelpFormatter" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.ArgDecorator" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.ArgDecorator",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Base class for decorators to add ArgumentParser information to a method.",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgMethodWrapper" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.ArgMethodWrapper",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Base class to define a wrapper for ArgumentParser method.",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgDecorator" -> "core.magic_arguments.ArgMethodWrapper" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.MagicArgumentParser" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.MagicArgumentParser",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="An ArgumentParser tweaked for use by IPython magics.",height=0.25,shape=box,fontsize=10]; "argparse.ArgumentParser" -> "core.magic_arguments.MagicArgumentParser" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.MagicHelpFormatter" [style="setlinewidth(0.5)",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="A HelpFormatter with a couple of changes to meet our needs.",height=0.25,shape=box,fontsize=10]; "argparse.RawDescriptionHelpFormatter" -> "core.magic_arguments.MagicHelpFormatter" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.argument" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.argument",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Store arguments and keywords to pass to add_argument().",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgMethodWrapper" -> "core.magic_arguments.argument" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.argument_group" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.argument_group",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Store arguments and keywords to pass to add_argument_group().",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgMethodWrapper" -> "core.magic_arguments.argument_group" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.defaults" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.defaults",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Store arguments and keywords to pass to set_defaults().",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgMethodWrapper" -> "core.magic_arguments.defaults" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.kwds" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.kwds",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Provide other keywords to the sub-parser constructor.",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgDecorator" -> "core.magic_arguments.kwds" [arrowsize=0.5,style="setlinewidth(0.5)"]; "core.magic_arguments.magic_arguments" [style="setlinewidth(0.5)",URL="#IPython.core.magic_arguments.magic_arguments",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",tooltip="Mark the magic as having argparse arguments and possibly adjust the",height=0.25,shape=box,fontsize=10]; "core.magic_arguments.ArgDecorator" -> "core.magic_arguments.magic_arguments" [arrowsize=0.5,style="setlinewidth(0.5)"]; } </p> <div class="section" id="classes"> <h2>8 Classes<a class="headerlink" href="#classes" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="IPython.core.magic_arguments.MagicArgumentParser"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">MagicArgumentParser</tt><big>(</big><em>prog=None</em>, <em>usage=None</em>, <em>description=None</em>, <em>epilog=None</em>, <em>parents=None</em>, <em>formatter_class=<class 'IPython.core.magic_arguments.MagicHelpFormatter'></em>, <em>prefix_chars='-'</em>, <em>argument_default=None</em>, <em>conflict_handler='error'</em>, <em>add_help=False</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.MagicArgumentParser" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference external" href="http://docs.python.org/2/library/argparse.html#argparse.ArgumentParser" title="(in Python v2.7)"><tt class="xref py py-class docutils literal"><span class="pre">argparse.ArgumentParser</span></tt></a></p> <p>An ArgumentParser tweaked for use by IPython magics.</p> <dl class="method"> <dt id="IPython.core.magic_arguments.MagicArgumentParser.__init__"> <tt class="descname">__init__</tt><big>(</big><em>prog=None</em>, <em>usage=None</em>, <em>description=None</em>, <em>epilog=None</em>, <em>parents=None</em>, <em>formatter_class=<class 'IPython.core.magic_arguments.MagicHelpFormatter'></em>, <em>prefix_chars='-'</em>, <em>argument_default=None</em>, <em>conflict_handler='error'</em>, <em>add_help=False</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.MagicArgumentParser.__init__" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="method"> <dt id="IPython.core.magic_arguments.MagicArgumentParser.error"> <tt class="descname">error</tt><big>(</big><em>message</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.MagicArgumentParser.error" title="Permalink to this definition">¶</a></dt> <dd><p>Raise a catchable error instead of exiting.</p> </dd></dl> <dl class="method"> <dt id="IPython.core.magic_arguments.MagicArgumentParser.parse_argstring"> <tt class="descname">parse_argstring</tt><big>(</big><em>argstring</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.MagicArgumentParser.parse_argstring" title="Permalink to this definition">¶</a></dt> <dd><p>Split a string into an argument list and parse that argument list.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.ArgDecorator"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">ArgDecorator</tt><a class="headerlink" href="#IPython.core.magic_arguments.ArgDecorator" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference external" href="http://docs.python.org/2/library/functions.html#object" title="(in Python v2.7)"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a></p> <p>Base class for decorators to add ArgumentParser information to a method.</p> <dl class="method"> <dt id="IPython.core.magic_arguments.ArgDecorator.add_to_parser"> <tt class="descname">add_to_parser</tt><big>(</big><em>parser</em>, <em>group</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.ArgDecorator.add_to_parser" title="Permalink to this definition">¶</a></dt> <dd><p>Add this object’s information to the parser, if necessary.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.magic_arguments"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">magic_arguments</tt><big>(</big><em>name=None</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.magic_arguments" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference internal" href="#IPython.core.magic_arguments.ArgDecorator" title="IPython.core.magic_arguments.ArgDecorator"><tt class="xref py py-class docutils literal"><span class="pre">IPython.core.magic_arguments.ArgDecorator</span></tt></a></p> <p>Mark the magic as having argparse arguments and possibly adjust the name.</p> <dl class="method"> <dt id="IPython.core.magic_arguments.magic_arguments.__init__"> <tt class="descname">__init__</tt><big>(</big><em>name=None</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.magic_arguments.__init__" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.ArgMethodWrapper"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">ArgMethodWrapper</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.ArgMethodWrapper" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference internal" href="#IPython.core.magic_arguments.ArgDecorator" title="IPython.core.magic_arguments.ArgDecorator"><tt class="xref py py-class docutils literal"><span class="pre">IPython.core.magic_arguments.ArgDecorator</span></tt></a></p> <p>Base class to define a wrapper for ArgumentParser method.</p> <p>Child class must define either <cite>_method_name</cite> or <cite>add_to_parser</cite>.</p> <dl class="method"> <dt id="IPython.core.magic_arguments.ArgMethodWrapper.__init__"> <tt class="descname">__init__</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.ArgMethodWrapper.__init__" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="method"> <dt id="IPython.core.magic_arguments.ArgMethodWrapper.add_to_parser"> <tt class="descname">add_to_parser</tt><big>(</big><em>parser</em>, <em>group</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.ArgMethodWrapper.add_to_parser" title="Permalink to this definition">¶</a></dt> <dd><p>Add this object’s information to the parser.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.argument"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">argument</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.argument" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference internal" href="#IPython.core.magic_arguments.ArgMethodWrapper" title="IPython.core.magic_arguments.ArgMethodWrapper"><tt class="xref py py-class docutils literal"><span class="pre">IPython.core.magic_arguments.ArgMethodWrapper</span></tt></a></p> <p>Store arguments and keywords to pass to add_argument().</p> <p>Instances also serve to decorate command methods.</p> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.defaults"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">defaults</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.defaults" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference internal" href="#IPython.core.magic_arguments.ArgMethodWrapper" title="IPython.core.magic_arguments.ArgMethodWrapper"><tt class="xref py py-class docutils literal"><span class="pre">IPython.core.magic_arguments.ArgMethodWrapper</span></tt></a></p> <p>Store arguments and keywords to pass to set_defaults().</p> <p>Instances also serve to decorate command methods.</p> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.argument_group"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">argument_group</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.argument_group" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference internal" href="#IPython.core.magic_arguments.ArgMethodWrapper" title="IPython.core.magic_arguments.ArgMethodWrapper"><tt class="xref py py-class docutils literal"><span class="pre">IPython.core.magic_arguments.ArgMethodWrapper</span></tt></a></p> <p>Store arguments and keywords to pass to add_argument_group().</p> <p>Instances also serve to decorate command methods.</p> <dl class="method"> <dt id="IPython.core.magic_arguments.argument_group.add_to_parser"> <tt class="descname">add_to_parser</tt><big>(</big><em>parser</em>, <em>group</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.argument_group.add_to_parser" title="Permalink to this definition">¶</a></dt> <dd><p>Add this object’s information to the parser.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="IPython.core.magic_arguments.kwds"> <em class="property">class </em><tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">kwds</tt><big>(</big><em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.kwds" title="Permalink to this definition">¶</a></dt> <dd><p>Bases: <a class="reference internal" href="#IPython.core.magic_arguments.ArgDecorator" title="IPython.core.magic_arguments.ArgDecorator"><tt class="xref py py-class docutils literal"><span class="pre">IPython.core.magic_arguments.ArgDecorator</span></tt></a></p> <p>Provide other keywords to the sub-parser constructor.</p> <dl class="method"> <dt id="IPython.core.magic_arguments.kwds.__init__"> <tt class="descname">__init__</tt><big>(</big><em>**kwds</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.kwds.__init__" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> </dd></dl> </div> <div class="section" id="functions"> <h2>3 Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="IPython.core.magic_arguments.construct_parser"> <tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">construct_parser</tt><big>(</big><em>magic_func</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.construct_parser" title="Permalink to this definition">¶</a></dt> <dd><p>Construct an argument parser using the function decorations.</p> </dd></dl> <dl class="function"> <dt id="IPython.core.magic_arguments.parse_argstring"> <tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">parse_argstring</tt><big>(</big><em>magic_func</em>, <em>argstring</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.parse_argstring" title="Permalink to this definition">¶</a></dt> <dd><p>Parse the string of arguments for the given magic function.</p> </dd></dl> <dl class="function"> <dt id="IPython.core.magic_arguments.real_name"> <tt class="descclassname">IPython.core.magic_arguments.</tt><tt class="descname">real_name</tt><big>(</big><em>magic_func</em><big>)</big><a class="headerlink" href="#IPython.core.magic_arguments.real_name" title="Permalink to this definition">¶</a></dt> <dd><p>Find the real name of the magic.</p> </dd></dl> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="IPython.core.magics.html" title="Module: core.magics" >next</a> |</li> <li class="right" > <a href="IPython.core.magic.html" title="Module: core.magic" >previous</a> |</li> <li><a href="http://ipython.org">home</a>| </li> <li><a href="../../search.html">search</a>| </li> <li><a href="../../index.html">documentation </a> »</li> <li><a href="../index.html" >The IPython API</a> »</li> </ul> </div> <div class="footer"> © Copyright The IPython Development Team. Last updated on Sep 02, 2015. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3. </div> </body> </html>