+
    Q(iM                         R t ^ RIt^ RIHt ^ RIHt ^ RIHtHtH	t	H
t
HtHt ]'       d   ^ RIHt R R ltR R	 ltR
 R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR# )zBash completion script generator.

Generates static bash completion scripts using COMPREPLY and compgen.
Targets bash 3.2+ with no external dependencies.
N)TYPE_CHECKING)is_iterable_type)CompletionActionCompletionDataclean_choice_textescape_for_shell_patternextract_completion_dataget_completion_action)Appc                4    V ^8  d   QhRRR\         R\         /# )   appr
   	prog_namereturnstr)formats   "^/Users/agent/.openclaw/workspace/venv/lib/python3.14/site-packages/cyclopts/completion/bash.py__annotate__r      s!     * *E *c *c *    c                   V'       d   \         P                  ! RV4      '       g   \        RV: R24      hVP                  RR4      p\	        V 4      pRV 2RRRV R	2R
R.pVP                  \        W1V 4      4       VP                  R.4       VP                  R4       VP                  RV RV 24       VP                  R4       RP                  V4      # )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   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r   c                0    V ^8  d   QhR\         R\         /# )r   choicer   r   )r   s   "r   r   r   E   s     ( ( ( (r   c                &    V P                  RR4      # )z&Escape single quotes for bash strings.'z'\''r    )r*   s   &r   _escape_bash_choicer.   E   s    >>#w''r   c                0    V ^8  d   QhR\         R\         /# )r   textr   r   )r   s   "r   r   r   J   s      3 3 r   c                N    V P                  RR4      p V P                  RR4      p V # )z*Escape description text for bash comments.r   r   r-   )r0   s   &r   _escape_bash_descriptionr3   J   s'    <<c"D<<c"DKr   c                0    V ^8  d   QhR\         R\        /# )r   actionr   )r   r   )r   s   "r   r   r   Q   s      +;  r   c                b    V \         P                  8X  d   R# V \         P                  8X  d   R# R# )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)r5   s   &r   _map_completion_action_to_bashr9   Q   s+     !'''	#//	/r   c          	          V ^8  d   QhR\         \        \        R3,          \        3,          R\        RRR\        \        ,          /# r   r&   .r   r   r
   r   dicttupler   r   list)r   s   "r   r   r   e   sF      %S/>9: 
 
#Y	r   c                    . pVP                  R4       VP                  R4       VP                  R4       VP                  \        V 4      4       VP                  R4       VP                  \        WV4      4       V# )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"   e   sf    * E	LL45	LL78	LL	LL1/BC	LL	LL+OLMLr   c                    V ^8  d   QhR\         \        \        R3,          \        3,          R\        \        ,          /# )r   r&   .r   r<   )r   s   "r   r   r      s7     j jd5c?N;Z6[ j`deh`i jr   c                
   \        4       p\        4       pV P                  4        F  pVP                   F  pVP                  4       '       d   K  VP                  P
                  '       g   K9  VP                  P
                   F-  pVP                  R4      '       g   K  VP                  V4       K/  	  K  	  VP                   F@  pVP                   F-  pVP                  R4      '       d   K  VP                  V4       K/  	  KB  	  K  	  . pVP                  R4       V'       dK   \        V4       U	u. uF  p	\        V	4      NK  	  p
p	RP                  V
4      pVP                  RV R24       MVP                  R4       VP                  R4       VP                  R4       V'       dK   \        V4       Uu. uF  p\        V4      NK  	  ppRP                  V4      pVP                  R	V R24       MVP                  R
4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R 4       VP                  R!4       VP                  R"4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R4       VP                  R#4       VP                  R$4       VP                  R%4       VP                  R&4       VP                  R'4       VP                  R(4       VP                  R)4       VP                  R4       VP                  R4       VP                  R4       V# u up	i u upi )*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argumentrJ   registered_commandcmd_namer'   optescaped_optsopts_strcmdescaped_cmdscmds_strs   &              r   rA   rA      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   4U
9Uc          	          V ^8  d   QhR\         \        \        R3,          \        3,          R\        RRR\        \        ,          /# r;   r<   )r   s   "r   r   r      sF     < <%S/>9:<< 
< 
#Y	<r   c                t   . pVP                   '       d   \        VP                   4      MRpVP                  '       d   \        VP                  4      MRpVP                  R4       VP                  R4       \	        R V P                  4        4       4      p\        V^,           4       EFM  pV P                  4        Uu. uF  p\        V4      V8X  g   K  VNK  	  p	pV	'       g   K?  VP                  RV R24       V^ 8X  d   VP                  \        V RRWE4      4       MVP                  R4       \        V	4       Fp  pV U
u. uF  p
\        V
4      NK  	  pp
RP                  V4      pVP                  R	V R
24       VP                  \        WRWE4      4       VP                  R4       Kr  	  VP                  R4       VP                  R4       VP                  R4       VP                  R4       EKP  	  VP                  R4       VP                  R4       VP                  R4       V# u upi u up
i )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   "   T F  p\        V4      x  K  	  R # 5iN)len).0paths   & r   	<genexpr>-_generate_completion_logic.<locals>.<genexpr>  s     A*@$CII*@s       )z      z      case "${cmd_path[@]}" inr   z	        "z")z
          z          ;;z
        *)z
      esacz      ;;z    *)z  esac )
help_flagsr>   version_flagsr#   maxkeysranger`   r!   _generate_completions_for_pathrO   r   r$   )r&   r   r   r'   rh   ri   	max_depthdepthrb   relevant_pathsrY   escaped_pathpath_strs   &&&          r   rB   rB      s   * E*-...s~~&bJ030A0A0AE#++,rM	LLGH	LL./A/*>*>*@AAIy1}%+:+?+?+AX+A4SYRWEW$$+AXtE7!_%A:LL7XWaqrLL9:~.IMN# 8 =N88L1y
"562?,Xbr ^, / LL&LL(LL&Z 1 &4 
LL	LL	LLL; Y  Os   ?H0H0H5c                    V ^8  d   QhR\         \        \        R3,          \        3,          R\        \        R3,          R\        R\        \        R3,          R\        \        R3,          R\        \        ,          /# )r   r&   .command_pathindentrh   ri   r   r<   )r   s   "r   r   r   3  sz     c c%S/>9:cS/c c c3h	c
 c?c 
#Ycr   c                   W9  d   V R2.# W,          p. p. pVP                    Uu. uF0  qP                  4       '       d   K  VP                  '       g   K.  VNK2  	  p	pV	 F  p
V
P                  P                  ;'       g    .  F-  pVP                  R4      '       g   K  VP                  V4       K/  	  V
P                   F-  pVP                  R4      '       g   K  VP                  V4       K/  	  K  	  . pVP                   F@  pVP                   F-  pVP                  R4      '       g   K  VP                  V4       K/  	  KB  	  V F=  pVP                  R4      '       g   K  W9  g   K$  W9  g   K,  VP                  V4       K?  	  V F=  pVP                  R4      '       g   K  W9  g   K$  W9  g   K,  VP                  V4       K?  	  VP                  V4       . pVP                   F@  pVP                   F-  pVP                  R4      '       d   K  VP                  V4       K/  	  KB  	  VP                    Uu. uF(  qP                  f   K  VP                  '       g   K&  VNK*  	  ppVP                  R R7       VP                  V R24       V'       dD   V Uu. uF  p\        V4      NK  	  ppRP                  V4      pVP                  V RV R24       MVP                  V R	24       VP                  V R
24       \        VP                   4      pV'       d.   \!        VP                   VVV R24      pVP                  V4       MV'       dD   V Uu. uF  p\        V4      NK  	  ppRP                  V4      pVP                  V RV R24       M;V'       d    VP                  \#        VV R24      4       MVP                  V R	24       VP                  V R24       V# u upi u upi u upi u upi )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                 :    V P                   e   V P                   # ^ # r_   )index)as   &r   <lambda>0_generate_completions_for_path.<locals>.<lambda>w  s    !''2Eqww'L1'Lr   )keyzif [[ ${cur} == -* ]]; thenr   z  COMPREPLY=( $(compgen -W '' -- "${cur}") )z  COMPREPLY=()else  fi)rG   is_positional_onlyshowrI   rJ   rK   r#   	negativesrM   rN   r!   ry   sortr.   r$   _check_if_prev_needs_value#_generate_value_completion_for_prev_generate_positional_completion)r&   rt   ru   rh   ri   rR   r'   optionsargkeyword_argsrS   rJ   flag_commandsrT   flagrM   rU   positional_argsrV   escaped_optionsoptions_strneeds_value_completionvalue_completion_linesrY   escaped_commandscommands_strs   &&&&&                     r   rm   rm   3  sf   6 *(,'(((DEG#'>>_>C9O9O9QCVYV^V^CC>L_ &&++1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		sRURZRZssnO[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/?O4O4'O4"O9O>c                F    V ^8  d   QhR\         R\        \         ,          /# )r   ru   r   )r   r?   )r   s   "r   r   r     s"     I IS IT#Y Ir   c           	     H   . p\        V 4      ^8X  d   V ^ ,          P                  RR7      p\        V ^ ,          P                  4      pV'       dN   V Uu. uF  p\	        \        V4      4      NK  	  ppRP                  V4      pVP                  V RV R24       V# \        V4      pV'       d   VP                  V RV R24       V# VP                  V R24        V# VP                  V R	24       \        V 4       F  w  rV
P                  RR7      p\        V
P                  4      pVP                  V R
V	 R24       V'       dM   V Uu. uF  p\	        \        V4      4      NK  	  ppRP                  V4      pVP                  V RV R24       M?\        V4      pV'       d   VP                  V RV R24       MVP                  V R24       VP                  V R24       K  	  \        R V  4       R4      pVP                  V R24       V'       d   VP                  RR7      p\        VP                  4      pV'       dM   V Uu. uF  p\	        \        V4      4      NK  	  ppRP                  V4      pVP                  V RV R24       MT\        V4      pV'       d   VP                  V RV R24       M)VP                  V R24       MVP                  V R24       VP                  V R24       VP                  V R24       V# u upi u upi u upi )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.
Tforcer   COMPREPLY=( $(compgen -W 'r~   zCOMPREPLY=( $(compgen  -- "${cur}") )rw   zcase ${positional_count} inr   rf       COMPREPLY=( $(compgen -W '    COMPREPLY=( $(compgen     COMPREPLY=()    ;;c              3   `   "   T F$  p\        VP                  4      '       g   K   Vx  K&  	  R # 5ir_   )r   hint)ra   r   s   & r   rc   2_generate_positional_completion.<locals>.<genexpr>  s!     ZOS?OPSPXPX?YSSOs   .
.N  *)esac)r`   get_choicesr	   r   r.   r   r$   r#   r9   	enumeratenext)r   ru   r'   choicesr5   cescaped_choiceschoices_strcompgen_flagidxrS   iterable_args   &&          r   r   r     s    E
?q !!$00t0<&q'9'>'>?RYZRYQ23DQ3GHRYOZ((?3KLLF8#=k]J^_`b L_ :&ALx'=l^K\]^Z LW x|45V LQ 	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$ ZOZ\`axt_%"..T.:G*<+<+<=FV]"^V]QR#67H7K#LV]"^!hh7x'Ek]Rfgh=fELLF8+El^Sd!efLLF8+;!<=LLF8#345xv&'xt_%Lg [$ #_& #_s   LLLc                $    V ^8  d   QhR\         /# )r   r   )bool)r   s   "r   r   r     s      T r   c                    V  F]  pVP                  4       '       d   K  VP                  P                  ;'       g    .  F  pVP                  R4      '       g   K    R# 	  K_  	  R# )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)rH   rI   rJ   rK   )rG   rS   rJ   s   &  r   r   r     sU     !! **//5525??3'' 6 
 r   c                h    V ^8  d   QhR\         \        ,          R\        R\         \        ,          /# )r   rM   ru   r   )r?   r   )r   s   "r   r   r     s2     J JT#Y Jad Jimnqir Jr   c           
        . pVP                  V R24       RpV  EF]  pVP                  4       '       d   K  VP                  P                  ;'       g    .  Uu. uF  qwP	                  R4      '       g   K  VNK  	  ppV'       g   Km  RpVP                  RR7      p	\        VP                  4      p
V F  pVP                  V RV R24       V	'       dM   V	 Uu. uF  p\        \        V4      4      NK  	  ppRP                  V4      pVP                  V R	V R
24       M?\        V
4      pV'       d   VP                  V RV R24       MVP                  V R24       VP                  V R24       K  	  EK`  	  V'       d   VP                  V R24       V'       dD   V Uu. uF  p\        V4      NK  	  ppRP                  V4      pVP                  V R	V R
24       M:V'       d   VP                  \        W# R24      4       MVP                  V R24       VP                  V R24       VP                  V R24       V# . pV'       dE   V Uu. uF  p\        V4      NK  	  ppRP                  V4      pVP                  V RV R
24       V# V'       d   VP                  \        W#4      4       V# VP                  V R24       V# u upi u upi u upi u upi )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   r   rf   r   r   r~   r   r   r   r   r   re   r   r   rw   )r#   rH   rI   rJ   rK   r   r	   r   r.   r   r$   r9   r!   r   )rG   rM   r   ru   r'   	has_casesrS   rJ   rN   r   r5   r   r   r   r   rY   r   r   s   &&&&              r   r   r     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6KK%KK)__doc__r   typingr   cyclopts.annotationsr   cyclopts.completion._baser   r   r   r   r   r	   cycloptsr
   r(   r.   r3   r9   r"   rA   rB   rm   r   r   r   rg   r   r   <module>r      sh    
   1  *Z(
(DjZ<~cLIX*Jr   