
    k7iI                        S r SSKrSSKJr  SSKJrJrJrJrJ	r	  \(       a  SSK
Jr  SSS\S	\4S
 jrS\S	\4S jrS\S	\4S jrS\S	\4S jrS\\\S4   \4   S\SSS	\\   4S jrS\\\S4   \4   S	\\   4S jrS\\\S4   \4   S\SSS	\\   4S jrS\\\S4   \4   S\\S4   S\S\\S4   S\\S4   S	\\   4S jrS\S	\\   4S jrS	\4S jrS\\   S\S	\\   4S jrg)zBash completion script generator.

Generates static bash completion scripts using COMPREPLY and compgen.
Targets bash 3.2+ with no external dependencies.
    N)TYPE_CHECKING)CompletionActionCompletionDataclean_choice_textextract_completion_dataget_completion_action)Appappr	   	prog_namereturnc                    U(       a  [         R                  " SU5      (       d  [        SU< S35      eUR                  SS5      n[	        U 5      nSU 3SSSU S	3S
S/nUR                  [        X1U 5      5        UR                  S/5        UR                  S5        UR                  SU SU 35        UR                  S5        SR                  U5      $ )aG  Generate bash completion script.

Parameters
----------
app : App
    The Cyclopts application to generate completion for.
prog_name : str
    Program name (alphanumeric with hyphens/underscores).

Returns
-------
str
    Complete bash completion script.

Raises
------
ValueError
    If prog_name contains invalid characters.
z^[a-zA-Z0-9_-]+$zInvalid prog_name: z0. Must be alphanumeric with hyphens/underscores.-_z# Bash completion for z# Generated by Cyclopts z() {z  local cur prev}zcomplete -F _ 
)	rematch
ValueErrorreplacer   extend"_generate_completion_function_bodyappendjoin)r
   r   	func_namecompletion_dataliness        R/home/james-whalen/.local/lib/python3.13/site-packages/cyclopts/completion/bash.pygenerate_completion_scriptr       s    ( BHH%8)DD.ym;klmm!!#s+I-c2O !,!

I;e
E 
LL3OPSTU	LL#	LL	LL=1YK89	LL99U    choicec                 &    U R                  SS5      $ )z&Escape single quotes for bash strings.'z'\''r   )r"   s    r   _escape_bash_choicer&   C   s    >>#w''r!   textc                 N    U R                  SS5      n U R                  SS5      n U $ )z*Escape description text for bash comments.r   r   r%   )r'   s    r   _escape_bash_descriptionr*   H   s'    <<c"D<<c"DKr!   actionc                 X    U [         R                  :X  a  gU [         R                  :X  a  gg)zMap completion action to bash compgen flags.

Parameters
----------
action : CompletionAction
    Completion action type.

Returns
-------
str
    Compgen flags ("-f", "-d", or "").
z-fz-dr   )r   FILESDIRECTORIES)r+   s    r   _map_completion_action_to_bashr/   O   s+     !'''	#//	/r!   r   .c                     / nUR                  S5        UR                  S5        UR                  S5        UR                  [        U 5      5        UR                  S5        UR                  [        XU5      5        U$ )a  Generate the body of the bash completion function.

Parameters
----------
completion_data : dict
    All extracted completion data.
prog_name : str
    Program name.
app : App
    Application instance.

Returns
-------
list[str]
    Lines of bash code for the completion function body.
z!  cur="${COMP_WORDS[COMP_CWORD]}"z$  prev="${COMP_WORDS[COMP_CWORD-1]}"r   )r   r    _generate_command_path_detection_generate_completion_logic)r   r   r
   r   s       r   r   r   c   sf    * E	LL45	LL78	LL	LL1/BC	LL	LL+OLMLr!   c                 
   [        5       n[        5       nU R                  5        H  nUR                   H}  nUR                  5       (       a  M  UR                  R
                  (       d  M7  UR                  R
                   H,  nUR                  S5      (       d  M  UR                  U5        M.     M     UR                   H?  nUR                   H,  nUR                  S5      (       a  M  UR                  U5        M.     MA     M     / nUR                  S5        U(       aK  [        U5       V	s/ s H  n	[        U	5      PM     n
n	SR                  U
5      nUR                  SU S35        OUR                  S5        UR                  S5        UR                  S5        U(       aK  [        U5       Vs/ s H  n[        U5      PM     nnSR                  U5      nUR                  S	U S35        OUR                  S
5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S 5        UR                  S!5        UR                  S"5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S#5        UR                  S$5        UR                  S%5        UR                  S&5        UR                  S'5        UR                  S(5        UR                  S)5        UR                  S5        UR                  S5        UR                  S5        U$ s  sn	f s  snf )*a  Generate bash code to detect the current command path.

This function generates two passes through COMP_WORDS:
1. First pass builds cmd_path by identifying valid command names
2. Second pass counts positionals (non-option words after the command path)

The two-pass approach is necessary because we need to know the full command
path length before we can correctly identify which words are positionals.

Note: all_commands is built globally across all command levels. If a positional
argument value happens to match a command name from a different level, it could
be incorrectly classified (though this represents poor CLI design).

Parameters
----------
completion_data : dict
    All extracted completion data.

Returns
-------
list[str]
    Lines of bash code for command path detection.
r   zD  # Build list of options that take values (to skip their arguments)r   z  local options_with_values='r$   z  local options_with_values=''r   zK  # Build list of all valid command names (to distinguish from positionals)z  local all_commands='z  local all_commands=''z>  # Detect command path by collecting valid command words onlyz  local -a cmd_path=()z  local i skip_next=0z$  for ((i=1; i<COMP_CWORD; i++)); doz!    local word="${COMP_WORDS[i]}"z#    if [[ $skip_next -eq 1 ]]; thenz      skip_next=0z      continuez    fiz    if [[ $word =~ ^- ]]; thenz*      # Check if this option takes a valuez:      if [[ " $options_with_values " =~ " $word " ]]; thenz        skip_next=1z      fiz    elsezF      # Non-option word - only add to cmd_path if it's a valid commandz3      if [[ " $all_commands " =~ " $word " ]]; thenz        cmd_path+=("$word")z  donez;  # Count positionals (non-option words after command path)z  local positional_count=0z$  local cmd_path_len=${#cmd_path[@]}z  skip_next=0z  local cmd_depth=0z      # Non-option wordz1      if [[ $cmd_depth -lt $cmd_path_len ]]; thenz        # Still in command pathz        ((cmd_depth++))z
      elsez1        # Past command path, this is a positionalz        ((positional_count++)))setvalues	argumentsis_flag	parametername
startswithaddcommandsnamesr   sortedr&   r   )r   options_with_valuesall_commandsdataargumentr9   registered_commandcmd_namer   optescaped_optsopts_strcmdescaped_cmdscmds_strs                  r   r1   r1      s   0 %5L&&(H##%%(*<*<*A*A*A$..33Ds+++//5 4 ' #'--.44**3// $$X. 5 #0 ) E	LLWX<BCV<WX<WS+C0<WX88L)4XJa@A56	LL	LL^_<B<<PQ<PS+C0<PQ88L)-hZq9:./	LL	LLQR	LL)*	LL()	LL78	LL45	LL67	LL$%	LL!"	LL	LL12	LL=>	LLMN	LL&'	LL	LL	LLYZ	LLFG	LL./	LL	LL	LL	LL	LLNO	LL-.	LL78	LL!	LL&'	LL78	LL45	LL67	LL$%	LL!"	LL	LL12	LLMN	LL&'	LL	LL	LL*+	LLDE	LL23	LL*+	LL	LLDE	LL12	LL	LL	LLLA Y Rs   /U4Uc                 &   / nUR                   (       a  [        UR                   5      OSnUR                  (       a  [        UR                  5      OSnUR                  S5        UR                  S5        [	        S U R                  5        5       5      n[        US-   5       GH1  nU R                  5        Vs/ s H  n[        U5      U:X  d  M  UPM     n	nU	(       d  M>  UR                  SU S35        US:X  a  UR                  [        U SS	XE5      5        OUR                  S
5        [        U	5       HV  nSR                  U5      n
UR                  SU
 S35        UR                  [        XSXE5      5        UR                  S5        MX     UR                  S5        UR                  S5        UR                  S5        UR                  S5        GM4     UR                  S5        UR                  S5        UR                  S5        U$ s  snf )a  Generate the main completion logic using case statements.

Parameters
----------
completion_data : dict
    All extracted completion data.
prog_name : str
    Program name.
app : App
    Application instance.

Returns
-------
list[str]
    Lines of bash code for completion logic.
 z4  # Determine command level and generate completionsz  case "${#cmd_path[@]}" inc              3   8   #    U  H  n[        U5      v   M     g 7f)N)len).0paths     r   	<genexpr>-_generate_completion_logic.<locals>.<genexpr>  s     A*@$CII*@s          )r   z      z      case "${cmd_path[@]}" inr   z	        "z")z
          z          ;;z
        *)z
      esacz      ;;z    *)z  esac)
help_flagstupleversion_flagsr   maxkeysrangerN   r   _generate_completions_for_pathr>   r   )r   r   r
   r   rV   rX   	max_depthdepthrP   relevant_pathspath_strs              r   r2   r2      s   * E*-..s~~&bJ030A0AE#++,rM	LLGH	LL./A/*>*>*@AAIy1}%+:+?+?+AX+A4SYRWEW$+AXtE7!_%A:LL7XWaqrLL9:~.88D>y
"562?,Xbr ^, / LL&LL(LL&Z - &0 
LL	LL	LLL7 Ys   9HHcommand_pathindentrV   rX   c                    X;  a  U S3/$ X   n/ n/ nUR                    Vs/ s H.  oR                  5       (       a  M  UR                  (       d  M,  UPM0     n	nU	 H  n
U
R                  R                  =(       d    /  H,  nUR                  S5      (       d  M  UR                  U5        M.     U
R                   H,  nUR                  S5      (       d  M  UR                  U5        M.     M     / nUR                   H?  nUR                   H,  nUR                  S5      (       d  M  UR                  U5        M.     MA     U H:  nUR                  S5      (       d  M  X;  d  M"  X;  d  M)  UR                  U5        M<     U H:  nUR                  S5      (       d  M  X;  d  M"  X;  d  M)  UR                  U5        M<     UR                  U5        / nUR                   H?  nUR                   H,  nUR                  S5      (       a  M  UR                  U5        M.     MA     UR                    Vs/ s H&  oR                  c  M  UR                  (       d  M$  UPM(     nnUR                  S S9  UR                  U S35        U(       aD  U Vs/ s H  n[        U5      PM     nnSR                  U5      nUR                  U SU S35        OUR                  U S	35        UR                  U S
35        [        UR                   5      nU(       a-  [!        UR                   UUU S35      nUR                  U5        OU(       aD  U Vs/ s H  n[        U5      PM     nnSR                  U5      nUR                  U SU S35        O:U(       a  UR                  [#        UU S35      5        OUR                  U S	35        UR                  U S35        U$ s  snf s  snf s  snf s  snf )a  Generate completions for a specific command path.

Parameters
----------
completion_data : dict
    All extracted completion data.
command_path : tuple[str, ...]
    Current command path.
indent : str
    Indentation string.
help_flags : tuple[str, ...]
    Help flag names.
version_flags : tuple[str, ...]
    Version flag names.

Returns
-------
list[str]
    Lines of bash code for completions at this command path.
COMPREPLY=()r   c                 8    U R                   b  U R                   $ S$ )Nr   )index)as    r   <lambda>0_generate_completions_for_path.<locals>.<lambda>s  s    !''2Eqww'L1'Lr!   )keyzif [[ ${cur} == -* ]]; thenr   z  COMPREPLY=( $(compgen -W '' -- "${cur}") )z  COMPREPLY=()else  fi)r6   is_positional_onlyshowr8   r9   r:   r   	negativesr<   r=   r   rf   sortr&   r   _check_if_prev_needs_value#_generate_value_completion_for_prev_generate_positional_completion)r   ra   rb   rV   rX   rA   r   optionsargkeyword_argsrB   r9   flag_commandsrC   flagr<   rD   positional_argsrE   escaped_optionsoptions_strneeds_value_completionvalue_completion_linesrH   escaped_commandscommands_strs                             r   r\   r\   /  s_   6 *(,'(((DEG#'>>_>C9O9O9QCVYV^V^C>L_ &&++1r1Ds##t$ 2 &&Ds##t$ ' ! M"mm&,,Ds##$$T* - ,
 ??3D$7D<UNN4   ??3D$7D<UNN4   NN=!H"mm*00H&&s++) 1 ,
 '+nn[ns		sRURZRZsnO[LM	LLF889:?FGw.s3wGhh/x;K=H\]^x~./	LLF84!7G!DNNHo&}"
 	+,	@HI/4Ixx 01x;L>I]^_	4_PRmTUx~./	LLF82LC `B \ H  Js.   OOO.OOOO"O'c           	      p   / n[        U 5      S:X  a  U S   R                  SS9n[        U S   R                  5      nU(       aN  U Vs/ s H  n[	        [        U5      5      PM     nnSR                  U5      nUR                  U SU S35        U$ [        U5      nU(       a  UR                  U SU S	35        U$ UR                  U S
35         U$ UR                  U S35        [        U 5       H  u  pU
R                  SS9n[        U
R                  5      nUR                  U SU	 S35        U(       aM  U Vs/ s H  n[	        [        U5      5      PM     nnSR                  U5      nUR                  U SU S35        O>[        U5      nU(       a  UR                  U SU S	35        OUR                  U S35        UR                  U S35        M     UR                  U S35        UR                  U S35        UR                  U S35        UR                  U S35        U$ s  snf s  snf )a  Generate position-aware positional argument completion.

Parameters
----------
positional_args : list
    List of positional arguments sorted by index.
indent : str
    Indentation string.

Returns
-------
list[str]
    Lines of bash code for position-aware positional completion.
rS   r   Tforcer   COMPREPLY=( $(compgen -W 'rk   zCOMPREPLY=( $(compgen  -- "${cur}") )rd   zcase ${positional_count} inrm   rU       COMPREPLY=( $(compgen -W '    COMPREPLY=( $(compgen     COMPREPLY=()    ;;  *)esac)
rN   get_choicesr   hintr&   r   r   r   r/   	enumerate)r{   rb   r   choicesr+   cescaped_choiceschoices_strcompgen_flagidxrB   s              r   ru   ru     sD    E
?q !!$00t0<&q'9'>'>?RYZRYQ23DQ3GHRYOZ((?3KLLF8#=k]J^_`B L? :&ALx'=l^K\]^: L7 x|456 L1 	x<=>&7MC***6G*8==9FLLF82cU!,-V]"^V]QR#67H7K#LV]"^!hh7x'Ek]Rfgh=fELLF8+El^Sd!efLLF8+;!<=LLF86*+ 8" 	xt_%x/01xv&'xt_%LG [$ #_s   H.=H3c                     U  HY  nUR                  5       (       a  M  UR                  R                  =(       d    /  H  nUR                  S5      (       d  M      g   M[     g)zCheck if any options take values, requiring prev-word completion logic.

Parameters
----------
arguments : ArgumentCollection
    Arguments to check.

Returns
-------
bool
    True if any option (starts with -) takes a value (is not a flag).
r   TF)r7   r8   r9   r:   )r6   rB   r9   s      r   rs   rs     sR     !! **//525??3'' 6 
 r!   r<   c           
         / nUR                  U S35        SnU  GHV  nUR                  5       (       a  M  UR                  R                  =(       d    /  Vs/ s H  owR	                  S5      (       d  M  UPM     nnU(       d  Mj  SnUR                  SS9n	[        UR                  5      n
U H  nUR                  U SU S35        U	(       aM  U	 Vs/ s H  n[        [        U5      5      PM     nnSR                  U5      nUR                  U S	U S
35        O>[        U
5      nU(       a  UR                  U SU S35        OUR                  U S35        UR                  U S35        M     GMY     U(       a  UR                  U S35        U(       aD  U Vs/ s H  n[        U5      PM     nnSR                  U5      nUR                  U S	U S
35        O9U(       a  UR                  [        X# S35      5        OUR                  U S35        UR                  U S35        UR                  U S35        U$ / nU(       aE  U Vs/ s H  n[        U5      PM     nnSR                  U5      nUR                  U SU S
35        U$ U(       a  UR                  [        X#5      5        U$ UR                  U S35        U$ s  snf s  snf s  snf s  snf )a~  Generate value completion based on previous word.

Parameters
----------
arguments : ArgumentCollection
    Arguments with potential values.
commands : list[str]
    Available commands at this level.
positional_args : list
    List of positional arguments sorted by index.
indent : str
    Indentation string.

Returns
-------
list[str]
    Lines of bash code for value completion.
zcase "${prev}" inFr   Tr   rm   rU   r   r   rk   r   r   r   r   r   rT   r   r   rd   )r   r7   r8   r9   r:   r   r   r   r&   r   r   r/   r   ru   )r6   r<   r{   rb   r   	has_casesrB   r9   r=   r   r+   r   r   r   r   rH   r   r   s                     r   rt   rt     s   & E	LLF8./0I#+#5#5#:#:#@b#@Z#@$__UXEY#@Z	&&T&2&x}}5DLLF82dV1-.V]"^V]QR#67H7K#LV]"^!hh7x'Ek]Rfgh=fELLF8+El^Sd!efLLF8+;!<=LLF86*+  8 xt_%DLMHS 3C 8HM88$45LLLF8#A,OcdeLL8HTX/Z[LLF8#345xv&'xt_% L DLMHS 3C 8HM88$45LLLF8#=l^K_`a L LL8QR L LLF8<01L_ [ #_  N  Ns   K4KKKK)__doc__r   typingr   cyclopts.completion._baser   r   r   r   r   cycloptsr	   strr    r&   r*   r/   dictrW   listr   r1   r2   r\   ru   boolrs   rt   rL   r!   r   <module>r      s   
    *E *c *c *Z( ( (
3 3 +;  (%S/>9: 
 
#Y	Djd5c?N;Z6[ j`deh`i jZ:%S/>9::: 
: 
#Y	:zc%S/>9:cS/c c c3h	c
 c?c 
#YcL9S 9T#Y 9xT *JT#Y Jad Jimnqir Jr!   