
    ^h&                     ~   S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  \R                  " S5      r\ " S S	\R                  5      5       r " S
 S\R                  5      rS rS rS r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r/ SQrg)a  A decorator-based method of constructing IPython magics with `argparse`
option handling.

New magic functions can be defined like so::

    from IPython.core.magic_arguments import (argument, magic_arguments,
        parse_argstring)

    @magic_arguments()
    @argument('-o', '--option', help='An optional argument.')
    @argument('arg', type=int, help='An integer positional argument.')
    def magic_cool(self, arg):
        """ A really cool magic command.

    """
        args = parse_argstring(magic_cool, arg)
        ...

The `@magic_arguments` decorator marks the function as having argparse arguments.
The `@argument` decorator adds an argument using the same syntax as argparse's
`add_argument()` method. More sophisticated uses may also require the
`@argument_group` or `@kwds` decorator to customize the formatting and the
parsing.

Help text for the magic is automatically generated from the docstring and the
arguments::

    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.

Here is an elaborated example that uses default parameters in `argument` and calls the `args` in the cell magic::

    from IPython.core.magic import register_cell_magic
    from IPython.core.magic_arguments import (argument, magic_arguments,
                                            parse_argstring)


    @magic_arguments()
    @argument(
        "--option",
        "-o",
        help=("Add an option here"),
    )
    @argument(
        "--style",
        "-s",
        default="foo",
        help=("Add some style arguments"),
    )
    @register_cell_magic
    def my_cell_magic(line, cell):
        args = parse_argstring(my_cell_magic, line)
        print(f"{args.option=}")
        print(f"{args.style=}")
        print(f"{cell=}")

In a jupyter notebook, this cell magic can be executed like this::

    %%my_cell_magic -o Hello
    print("bar")
    i = 42

Inheritance diagram:

.. inheritance-diagram:: IPython.core.magic_arguments
   :parts: 3

    N
UsageError)undoc)	arg_split)dedentz[a-zA-Z][a-zA-Z0-9_-]*$c                   <   ^  \ rS rSrSrS rS rSU 4S jjrSrU =r	$ )MagicHelpFormatter`   z@A HelpFormatter with a couple of changes to meet our needs.
    c                 V    [         R                  R                  U [        U5      X#5      $ N)argparseRawDescriptionHelpFormatter
_fill_textr   )selftextwidthindents       V/home/james-whalen/.local/lib/python3.13/site-packages/IPython/core/magic_arguments.pyr   MagicHelpFormatter._fill_texte   s"    33>>tVD\SXaa    c                    UR                   (       d%  U R                  XR                  5      " S5      u  nU$ / nUR                  S:X  a  UR	                  UR                   5        OuUR                  R                  5       nU R                  X5      n[        R                  U5      (       d  SU-  nUR                    H  nUR                  U< SU< 35        M     SR                  U5      $ )N   r   z<%s> z, )option_strings_metavar_formatterdestnargsextendupper_format_argsNAME_REmatchappendjoin)r   actionmetavarpartsdefaultargs_stringoption_strings          r   _format_action_invocation,MagicHelpFormatter._format_action_invocationi   s    $$..v{{CAFHGN E ||q V223
 !++++-"//@ }}[11"(;"6K%+%:%:MLLM;!GH &; 99U##r   c                 .   > [         [        U ]  XX45        g r   )superr	   	add_usage)r   usageactionsgroupsprefix	__class__s        r   r/   MagicHelpFormatter.add_usage   s     $1%&Qr    )z::

  %)
__name__
__module____qualname____firstlineno____doc__r   r+   r/   __static_attributes____classcell__r4   s   @r   r	   r	   `   s    b$:R Rr   r	   c            
       P   ^  \ rS rSrSrSSSSS\SSSS4
U 4S jjrS rS	 rS
r	U =r
$ )MagicArgumentParser   z9An ArgumentParser tweaked for use by IPython magics.
    N-errorFc                 :   > Uc  / n[         [        U ]  XX4XVXxXS9
  g )N)
progr0   descriptionepilogparentsformatter_classprefix_charsargument_defaultconflict_handleradd_help)r.   r@   __init__)r   rE   r0   rF   rG   rH   rI   rJ   rK   rL   rM   r4   s              r   rN   MagicArgumentParser.__init__   s3     ?G!41t#%-	 	2 	Br   c                     [        U5      e)z4Raise a catchable error instead of exiting.
        r   )r   messages     r   rC   MagicArgumentParser.error   s     !!r   c                 :    [        U5      nU R                  U5      $ )zKSplit a string into an argument list and parse that argument list.
        )r   
parse_args)r   	argstringargvs      r   parse_argstring#MagicArgumentParser.parse_argstring   s     #t$$r   r6   )r7   r8   r9   r:   r;   r	   rN   rC   rW   r<   r=   r>   s   @r   r@   r@      s=     !!3!"&")B&"
% %r   r@   c                 
   [        U S0 5      nSU;  a  [        U SS5      US'   [        U 5      n[        U40 UD6nSnU R                  SSS2    H  nUR	                  X45      nUc  M  UnM     UR                  5       U l        U$ )zAConstruct an argument parser using the function decorations.
    argcmd_kwdsrF   r;   N)getattr	real_namer@   
decoratorsadd_to_parserformat_helpr;   )
magic_funckwdsarg_nameparsergroupdecoresults          r   construct_parserrh      s     :}b1DD %j)TB]$H 2T2F E%%dd+##F2E ,  ++-JMr   c                 8    U R                   R                  U5      $ )z@Parse the string of arguments for the given magic function.
    )rd   rW   )ra   rU   s     r   rW   rW      s     ,,Y77r   c                 |    U R                   nUR                  S5      (       a  U[        S5      S n[        U SU5      $ )z%Find the real name of the magic.
    magic_Nargcmd_name)r7   
startswithlenr\   )ra   
magic_names     r   r]   r]      s?     $$JX&&H/
:}j99r   c                   $    \ rS rSrSrS rS rSrg)ArgDecorator   zMBase class for decorators to add ArgumentParser information to a method.
    c                 |    [        USS5      (       d  SUl        / Ul        UR                  R                  U 5        U$ Nhas_argumentsFT)r\   ru   r^   r#   r   funcs     r   __call__ArgDecorator.__call__   s7    t_e44!%D DOt$r   c                     g)zCAdd this object's information to the parser, if necessary.
        Nr6   r   rd   re   s      r   r_   ArgDecorator.add_to_parser   s     	r   r6   N)r7   r8   r9   r:   r;   rx   r_   r<   r6   r   r   rq   rq      s    r   rq   c                   (    \ rS rSrSrSS jrS rSrg)magic_arguments   zJMark the magic as having argparse arguments and possibly adjust the
name.
Nc                     Xl         g r   name)r   r   s     r   rN   magic_arguments.__init__       	r   c                     [        USS5      (       d  SUl        / Ul        U R                  b  U R                  Ul        [        U5      Ul        U$ rt   )r\   ru   r^   r   rl   rh   rd   rv   s     r   rx   magic_arguments.__call__   sI    t_e44!%D DO99 #yyD 't,r   r   r   )r7   r8   r9   r:   r;   rN   rx   r<   r6   r   r   r~   r~      s    	r   r~   c                   0    \ rS rSr% Sr\\S'   S rS rSr	g)ArgMethodWrapper   z
Base class to define a wrapper for ArgumentParser method.

Child class must define either `_method_name` or `add_to_parser`.

_method_namec                     Xl         X l        g r   argsrb   )r   r   rb   s      r   rN   ArgMethodWrapper.__init__   s    		r   c                 l    Ub  Un[        XR                  5      " U R                  0 U R                  D6  g)5Add this object's information to the parser.
        N)r\   r   r   rb   r{   s      r   r_   ArgMethodWrapper.add_to_parser  s2     F))*DIICCr   r   N)
r7   r8   r9   r:   r;   str__annotations__rN   r_   r<   r6   r   r   r   r      s     r   r   c                       \ rS rSrSrSrSrg)argumenti  zkStore arguments and keywords to pass to add_argument().

Instances also serve to decorate command methods.
add_argumentr6   Nr7   r8   r9   r:   r;   r   r<   r6   r   r   r   r          "Lr   r   c                       \ rS rSrSrSrSrg)defaultsi  zkStore arguments and keywords to pass to set_defaults().

Instances also serve to decorate command methods.
set_defaultsr6   Nr   r6   r   r   r   r     r   r   r   c                       \ rS rSrSrS rSrg)argument_groupi  zqStore arguments and keywords to pass to add_argument_group().

Instances also serve to decorate command methods.
c                 N    UR                   " U R                  0 U R                  D6$ )r   )add_argument_groupr   rb   r{   s      r   r_   argument_group.add_to_parser#  s"     (($))AtyyAAr   r6   N)r7   r8   r9   r:   r;   r_   r<   r6   r   r   r   r     s    
Br   r   c                   2   ^  \ rS rSrSrS rU 4S jrSrU =r$ )rb   i)  z:Provide other keywords to the sub-parser constructor.
    c                     Xl         g r   rb   )r   rb   s     r   rN   kwds.__init__,  r   r   c                 P   > [         [        U ]  U5      nU R                  Ul        U$ r   )r.   rb   rx   rZ   )r   rw   r4   s     r   rx   kwds.__call__/  s%    T4)$/99r   r   )	r7   r8   r9   r:   r;   rN   rx   r<   r=   r>   s   @r   rb   rb   )  s     r   rb   )r~   r   r   rb   rW   )r;   r   reIPython.core.errorr   IPython.utils.decoratorsr   IPython.utils.processr   IPython.utils.textr   compiler!   r   r	   ArgumentParserr@   rh   rW   r]   objectrq   r~   r   r   r   r   rb   __all__r6   r   r   <module>r      s   Lh  	 * * + %
**/
0&R== &R &RP%(11 %D,8:6 "l (| 0" "" "	B% 	B	< 	r   