
    Kimm                        d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZmZmZmZmZmZm Z m!Z!m"Z" d d
l#m$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z* ejV                  dk  rn	 erd dl,m-Z-  ee'd      Z. G d de      Z/dde0z  ee0   z  de1e0   fdZ2de3e0df   fdZ4ddddedee0   de0de5de3e1e0   e6dz  f   f
dZ7dede6de6fd Z8dee0   de0de1e3e0e5f      fd!Z9ddd"dede1e0   de0d#e6dz  de1e0   f
d$Z:defd%Z;ded&efd'Z<defd(Z=de1e3d)ef      fd*Z>dd+d&edede1e0   d,ee   de0de3e j~                  e1e0   f   fd-Z@y).    N)CallableIterableSequence)suppress)partial)TYPE_CHECKINGAny
NamedTuple
get_origin)_bool)resolve_optional)ArgumentArgumentCollection)	ArgumentOrderErrorCoercionErrorCombinedShortOptionErrorConsumeMultipleErrorCycloptsErrorMissingArgumentErrorRequiresEqualsErrorUnknownOptionErrorValidationError)POSITIONAL_ONLYPOSITIONAL_OR_KEYWORD)Token)UNSETis_option_like)      )Groupcli)sourcec                   J    e Zd ZU dZeed<   	 eed<   	 eedf   ed<   	 eed<   y)_KeywordMatchz?Represents a matched CLI token with its corresponding argument.matched_tokenargument.keysimplicit_valueN)	__name__
__module____qualname____doc__str__annotations__r   tupler	        Y/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/bind.pyr$   r$   )   s1    II&
S/-<r1   r$   tokensreturnc                     | t         j                  dd  } | S t        | t              rt	        j
                  |       } | S t        |       } | S )N   )sysargv
isinstancer-   shlexsplitlist)r3   s    r2   normalize_tokensr=   9   sN    ~!"
 M	 
FC	 V$ M fMr1   .c                    | sy| d   j                   }| dd  D ]r  }|j                   s yt        t        ||j                   d            D ]&  \  }\  }}||k7  s|dk(  r  y|j                   d | } n |d t        |j                          }t |S )Nr0   r   r6   F)strict)r'   	enumerateziplen)argument_collectioncommonr&   i
common_keyargument_keys         r2   _common_root_keysrH   C   s     #((F'+ 
.}}-6s68==Y^7_-` 	)A)
L\)6!r*	 ,#hmm,-
. Mr1   --F)end_of_options_delimiterstop_at_first_unknownrC   rJ   rK   c                \   g g }}g }d}|r	 |j                  |      }||d }|d| }t        |      D ]  \  }	}
|dkD  r|dz  }t        |
d      s`|r;|j	                  ||	d        |j	                  t        |	t        |                    nE|j                  |
       |j                  |	       g }d}|
j                  d      xr |
j                  d       }d|
v r@|
j                  dd      \  }}	 | j                  |       |j                  |       |dz  }d	}n|
}g }d}	 |j                  t        |g| j                  |              t        |      D ]r  \  }}|"|t        |      dz
  k(  r|j                  |       |j                   j                  j                  r1|j                   j                  t#        |j$                  d             ||j&                  t        ur|r	 t)        |d         }|r;|j                   j                  t#        |j$                  |j&                               t/        |j&                  t0              r=|j                   j                  t#        |j$                  |j&                                35|j                   j                  t#        |j$                  |j&                               qt        |      dkD  r&t3        d |D              }|dkD  rt5        d|       |j                   j7                  |j8                        \  }} |j                   j                  j:                  r>|j$                  j                  d      r#|s!t=        |j                   |j$                        |j                   j                  j>                  }!|!t/        |!t@              sJ |!|!nd\  }"}#tC        tD              5  | rj|!htG        j                         D ]P  }$||	dz   |$z      }
|j                   j                  jH                  st        |
      r n|j                  |
       |dz  }R n||z  }t        |      D ]  }$t        |      dk(  rE|j                   jK                  |      s&|j                   jM                  ||j8                        rd} np||	dz   |$z      }
|j                   j                  jH                  s-t        |
      r"tO        |j                   ||j$                        |j                  |
       |dz  } ddd       |s| r|!|"dkD  r%tQ        |j                   ||j$                  |"|#d      tS        |j                   jT                        }% tW        |%      xs |%       }&|j                   j                  t#        |j$                  |&|j8                               btO        |j                   ||j$                        t        |      |z  r"tO        |j                   ||j$                        |!nt        |      tY        d|      z  }'|'|"k  r%tQ        |j                   ||j$                  |"|#|'      |#*|'|#kD  r%tQ        |j                   ||j$                  |"|#|'      t        |      D ]A  \  }(}|j                   j                  t#        |j$                  ||(|j8                               C u  d})t        dt        |            D ]  }$||$   ||$dz
     dz   k7  s|$}) n |j	                  |       ||)fS # t        $ r Y w xY w# t        $ r |
}Y w xY w# t        $ r |r9t        |
      d
kD  r*t        |
d	      r|j                  d      }d}|t        |      k  r||   }d| }	 | j                  |      \  }}}|t        us|j                  j                  r#|j                  t        ||||             |dz  }n/||dz   d }|j                  t        ||||             |r|}|dz  }nfnV# t        $ rJ |r|j	                  ||	d        |dfcY cY c S |j                  |       |j                  |	       |dz  }Y nw xY w|t        |      k  r|sFY O|r|j	                  ||	d        |dfcY c S |j                  |
       |j                  |	       Y Y xw xY w# t*        $ rJ}|j,                  t#        |j$                        |_        |j                   |j                   |_         d}~ww xY w# 1 sw Y   xY w)a?  Extract keyword arguments and flags from the token stream.

    Returns
    -------
    unused_tokens: list[str]
        Tokens not consumed by any keyword or flag.
    contiguous_positional_count: int | None
        Number of leading contiguous non-option tokens before the first gap
        caused by keyword extraction. ``None`` if all non-option tokens are
        contiguous (i.e. no keywords were interleaved among positional tokens).

        For example, given ``a b c --bar 8 --baz 10 d``, the unused tokens are
        ``['a', 'b', 'c', 'd']`` with original indices ``[0, 1, 2, 6]``.
        The gap between indices 2 and 6 yields ``contiguous_positional_count=3``.
        This is used by ``_parse_pos`` to prevent positional-only list parameters
        from consuming tokens that appeared after keyword arguments.
    r   Nr6   T)allow_numbers-rI   =F   )keywordr(   )rQ   c              3      K   | ]:  }|j                   t        u s|j                  j                  j                  r7d  < yw)r6   N)r(   r   r&   	parametercount).0ms     r2   	<genexpr>z&_parse_kw_and_flags.<locals>.<genexpr>  s7      -a.>.>%.GPQPZPZPdPdPjPj-s   A AAz6Cannot combine multiple value-taking options in token )msg)r&   rQ   )r   N)r&   tokens_so_farrQ   )r&   rZ   rQ   min_requiredmax_allowedactual_count)rQ   r(   r'   )rQ   valueindexr'   )-r_   
ValueErrorr@   r   extendrangerB   append
startswithr;   matchr$   lstripr   rT   rU   r&   CliTokenr%   r(   r   r   tokenr9   boolsumr   token_countr'   requires_equalsr   consume_multipler/   r   
IndexError	itertoolsallow_leading_hyphen_should_attempt_json_dict_should_attempt_json_listr   r   r   hintr   max)*rC   r3   rJ   rK   unused_tokenspositional_only_tokensunused_token_original_indicesskip_next_iterationsdelimiter_indexrE   rh   
cli_valuesconsume_countallow_combined_flags
cli_option	cli_valuematchesattached_valuecharspositionchar	test_flagargr'   implicit	remaindermatch_indexre   coerced_valueevalue_taking_counttokens_per_elementconsume_all	cm_boundscm_mincm_maxjrs   empty_container
n_elementsr_   contiguous_positional_counts*                                             r2   _parse_kw_and_flagsr   U   s>	   0 -/)M/1!	.$ll+CDO &,O,<%=",_-Ff% r5!# A% e48$$$VABZ0-44U1c&k5JK  ')003 "
  %//4SU=M=Md=S9S %<$)KKQ$7!J		##))*5!!),"',$ J')%)5	NN=\6I6O6OPZ6[\]j #,G"4 S	K )kS\A=M.M!!.1~~''--%%hu7J7J[\&]^%%U2(-jn(= %--$U-@-@QVQeQef &e&:&:DA!NN11 (1D1DY^YmYmUm n !NN))(5;N;N_d_s_s*tu w<!#), -#*- *& *A-6"XYcXd e  38..2L2LUZZ2X/"K>>++;;@S@S@^@^_c@dmw-!& % 3 3  "NN44EE	 (Jy%,HHH.7.Cj) 6"y'<!*!2 6A$*1q519$5E#(>>#;#;#P#PUcdiUj %&--e40A506 &);;!&}!5 6A":!3 % H H T#(>>#K#KJX]XbXb#c56 2 !&$*1q519$5E#(>>#;#;#P#PUcdiUj&:-2^^2<,1,?,?'" !"
 '--e40A50%66< ""y'<!A:"6)..8(-(;(;-3,2-.#   00C0CD+C:d+;+Ct*F--$U-@-@Q`glgqgqr
 3%*^^:W\WjWj  _'99.!&zSXSfSf 
 !,%(_A?Q8R%R
%."6)..8(-(;(;-3,2-7#  "-*v2E"6)..8(-(;(;-3,2-7#  -6j,A (y--$U-@-@	Y^ejeoeopaS	rp /31c789 (+/LQQRU/SVW/WW*+'
 /0555I  		J  # #
#  3	 $E
Q>%_d;e #))#.U+ ?D"#D6
I&.A.G.G	.R+T8#50CMM4G4G#NN=Cx+XY$MH ).hln(=I#NN=Cx+XY( 2; - 2! % & &0)00<#0$#66%,,Y75<<Q? A&/ U+@ (!((4($..$$U+-44Q7 S3	B ) 77?&.u7J7J&KAG::-).AJb6 6s   \ 4)\)%*\;+cEd!	\&%\&)\87\8;AcB`c#a*:c 'a*'c)a**c<cc"cc	dAdd!d+starting_indexc                    d}t        j                         D ]_  }	 | j                  ||z         \  }}}|j                  j
                  t        ur |S |j                         \  }}|rt        d      ||z  }a |S # t        $ r Y  |S w xY w)Nr   z1Cannot have 2 all-consuming positional arguments.)ro   rU   re   r`   
field_infokindr   rk   )rC   r   n_tokens_to_leaverE   r&   _future_tokens_per_elementfuture_consume_alls           r2   #_future_positional_only_token_countr   z  s    __ 
7	066~7IJNHa ##?:
 	 9A8L8L8N5!#5PQQ66
7   	 	s   A;;	B	B	c                     	 | j                  |      }| d | D cg c]  }|df c}| |dz   d  D cg c]  }|df c}z   S c c}w c c}w # t        $ r | D cg c]  }|df nc c}w c}cY S w xY w)NFr6   T)r_   r`   )r3   rJ   ry   ts       r2   _preprocess_positional_tokensr     s    , ,,'?@$*+;O$<=qE
=TZ[jmn[n[pTq@rq!T@rrr=@r ,$*+qE
+++,s7   A AA AA 
A A/A$#	A/.A/rJ   r   r   c          	         g }|sg S t        ||      }t        j                         D ]  }	 | j                  |      \  }}}|j
                  j                  t        u r|j                  r+|j                  d   j                  |j                  |       m|rQ|d   }	|j                  j                  s"t        |	      rt        t        |	      |       t!        |||d   d         |j#                         \  }
}t%        d|
      }
|rF|j
                  j                  t&        u r*t)        | |dz         }|t%        |t+        |      |z
        }nd}g }t+        |      |z
  dkD  rt+        |      |z
  |
k  rt-        ||D cg c]  }|d   	 c}      t/        |d|
       D ]^  \  }\  }	}|s8|j                  j                  s"t        |	      rt        t        |	      |       |j                  t        |	|             ` ||
d }|snt+        |      |z
  dkD  r||j                  dd |r n |D cg c]  }|d   	 c}S # t        $ r Y  w xY wc c}w c c}w )	aa  Assign positional tokens to positional parameters.

    Parameters
    ----------
    argument_collection: ArgumentCollection
        Arguments whose keyword/flag tokens have already been consumed.
    tokens: list[str]
        Unused tokens from ``_parse_kw_and_flags``.
    end_of_options_delimiter: str
        Delimiter after which all tokens are forced positional.
    contiguous_positional_count: int | None
        If not ``None``, the number of leading contiguous positional tokens
        that were adjacent in the original CLI input (before keyword extraction
        created a gap). Used to cap how many tokens a ``POSITIONAL_ONLY``
        list/iterable parameter may consume, preventing it from greedily
        swallowing tokens that originally appeared after keyword arguments.
        See ``_parse_kw_and_flags`` for how this value is computed.
    r   N)r^   )rh   rC   )r&   9prior_positional_or_keyword_supplied_as_keyword_argumentsrh   r6   )r&   rZ   )r^   r_   )r   ro   rU   re   r`   r   r   r   r3   rQ   rc   rT   rp   r   r   rg   r   rk   rt   r   r   rB   r   r@   )rC   r3   rJ   r   r   tokens_and_force_positionalrE   r&   r   rh   r   r   r   
new_tokensxr_   force_positionals                    r2   
_parse_posr     s   2 AC=	"?H`"a__ <	066q9NHa ##'<<8??1#5#=#=#IIPPQYZHq	))>>>RWCX -8%3H^qrr,!) SL9!<Q?  +3*>*>*@'K $678..33F !DDWYZ]^Y^ _ +6$'%s+F'GJe'e%! !"
./2CCqH/03DDHZZ*%1L"MA1Q4"M 
 5>>YZm[m>n4o F00/'0B0B0W0W\jkp\q,8%3H^qrr!!(e"DEF +FFXFY*Z' ./2CCqH )*y<| 66QAaD66w  		Z #N 7s   I>I5I	IIc           	      (   | D ]~  }|j                   r|j                  j                  J |j                  j                  D ]>  }	 t        j                  |   }|j                   j                  t        ||d              ~  y # t        $ r Y Ow xY w)Nenv)rQ   r^   r"   )r3   rT   env_varosenvironrc   r   KeyError)rC   r&   env_var_nameenv_var_values       r2   
_parse_envr     s    ' ??!!))555$..66 	L "

< 8 &&u\_d'ef	  s   B	BBfuncc                     t        j                  |      j                         }| j                  D ]B  }|j                  t
        us|j                  |j                  |j                  j                  <   D |S )z+Bind the mapping to the function signature.)	inspect	signaturebind_partial_root_argumentsr^   r   	argumentsr   name)rC   r   boundr&   s       r2   _bindr     se    
 d#002E'77 G>>&8@EOOH//445G Lr1   c                 "    |D ]
  } ||         y Nr0   )rC   configsconfigs      r2   _parse_configsr     s     $"#$r1   r    c                 ,   i }t        t        | j                  d             D ];  \  }}| j                  |      }t	        |      }||j                  |      f|||f<   = t        |j                         d      D cg c]  \  }}|	 c}}S c c}}w )zSort groups into "deepest common-root-keys first" order.

    This is imperfect, but probably works sufficiently well for practical use-cases.
    c                     | j                   S r   )r   )r   s    r2   <lambda>z_sort_group.<locals>.<lambda>  s
    qvv r1   )key)group)keys_prefixT)reverse)r@   sortedgroups	filter_byrH   items)rC   outrE   r   group_argumentscommon_root_keysr   gas           r2   _sort_groupr     s    
 Cf%8%?%?EUVW f5-77e7D,_=&+_-F-FSc-F-d%eq!"	f
 #399;=>51bB>>>s    BrJ   r   c                   |}	 t        |||      \  }}t        ||||      }t        |       t        ||       |j	                          t        |      }	 |D ]  \  }}	|j                  D ]
  }
 |
|	          	 |D ]K  }|j                  s|j                  j                  s'|j                  r4|j                   rAt#        |       t%        ||       }||fS # t        t        t        f$ r.}t        |j                  r|j                  d   nd      |d}~ww xY w# t&        $ r}||_        ||_         d}~ww xY w)a  Parse and coerce CLI tokens to match a function's signature.

    Parameters
    ----------
    func: Callable
        Function.
    argument_collection: ArgumentCollection
    tokens: list[str]
        CLI tokens to parse and coerce to match ``f``'s signature.
    configs: Iterable[Callable]
    end_of_options_delimiter: str
        Everything after this special token is forced to be supplied as a positional argument.

    Returns
    -------
    bound: inspect.BoundArguments
        The converted and bound positional and keyword arguments for ``f``.

    unused_tokens: list[str]
        Remaining tokens that couldn't be matched to ``f``'s signature.
    r   r   r    )exception_messager   N)r&   )r   r   r   r   _convertr   	validatorAssertionErrorr`   	TypeErrorr   argsparser   requiredr'   
has_tokensr   r   r   root_input_tokensru   )r   rC   r3   r   rJ   ru   r   groups_with_argumentsr   r   r   r   r&   r   s                 r2   create_bound_argumentsr   $  s^   : M!5HIa6
22 #%=(C	
 	&'*G4$$& +,? @	e*? /&!& /Io.// , 	>H ~~("5"5">">x}}]e]p]p*H==		> )40 - 
I6 	e!AFF1IRW\]cdd	e  $'sT   AD  #C :D  D  $D  1D  >D  D/)DDD   	D=)D88D=)Ar   ro   r   r:   r7   collections.abcr   r   r   
contextlibr   	functoolsr   typingr   r	   r
   r   cyclopts._convertr   cyclopts.annotationsr   cyclopts.argumentr   r   cyclopts.exceptionsr   r   r   r   r   r   r   r   r   cyclopts.field_infor   r   cyclopts.tokenr   cyclopts.utilsr   r   version_infocyclopts.groupr    rg   r$   r-   r<   r=   r/   rH   ri   intr   r   r   r   r   r   r   r   BoundArgumentsr   r0   r1   r2   <module>r      s\     	  
 8 8   = = # 1 :
 
 
 G   0g $5'=J = TCZ(3-7 DI eCHo , %)"'b6+b6SMb6 "	b6
  b6 49cDj !b6J	=O ad il  ,(3- ,SV ,[_`efikofo`p[q , %).2^7+^7I^7 "	^7
 "%t^7 
#Y^7B$6  	+	
	$(: $?U7<N3N-O(P ?* %)B 
B +B  IB  h	B  "B  7!!49,-B r1   