YU 2dZddlZddlZddlmZmZmZdedefdZdej deeedfeeffd Z dej deefd Z dej d eedfd eeedfeeffd Z deeedfeefdeedfdeefdZ ddej dedefdZ ddej dedefdZ ddej dedefdZeeedZeeZ ddej dededefdZdS)z Shell auto-completion script generators for the CLI. Introspects an argparse parser to enumerate all commands, subcommands, and flags, then emits completion scripts for bash, zsh, and fish. N)DictListTupleprogreturnc.tjdd|S)zDConvert a prog name to a safe shell identifier (letters, digits, _).z [^a-zA-Z0-9]_)resub)rs V/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/completions.py_safe_identifierr s 6/3 - --parser.c,i}t|d||S)z@Walk the parser tree and return {command_path: [flags]} mapping.) _walk_parser)rresults r _collect_commandsrs 02FV$$$ Mrcg}|jD]W}t|tjrt|tjr8|jD]}||Xt|S)z:Extract all optional flags from a parser (excluding help).)_actions isinstanceargparse _HelpAction_SubParsersActionoption_stringsappendsorted)rflagsactionopts r _get_flagsr!s E/ fh2 3 3   fh8 9 9  (  C LL      %==rpathrct|}|||<|jD]P}t|tjr4|jD]\}}t|||fz|QdS)z>Recursively walk subparsers and collect command paths + flags.N)r!rrrrchoicesitemsr)rr"rrrname subparsers r rr(s v  EF4L/@@ fh8 9 9 @#)>#7#7#9#9 @ @iYw????@@rcommandsprefixct}|D][}t|t|dzkr6|dt||kr||d\t|S)z,Get immediate subcommands of a given prefix.N)setlenaddr)r(r)subsr"s r _get_subcommandsr17ss 55D t99F a ' 'D3v;;,?6,I,I HHT"X    $<<rimunify360-agentc,t|}g}|d||d|d|d|dt|d|d|d|d |d |d |d |d |d|d|d|d|d|d|d|d|d|d|d|d|d|d|dt|d}|D]}|st ||}||}d||z}d|} |d| d|d |d!|d"t |d#} |d#g} d| | z} |d$|d | d!|d"|d%|d&|d|d't|d(||dd)|S)*z"Generate a bash completion script.z# bash completion for # Auto-generated by z completions bashr z_completions() {z local cur prev words cwordz. if type _init_completion &>/dev/null; thenz" _init_completion || returnz elsez COMPREPLY=()z' cur="${COMP_WORDS[COMP_CWORD]}"z* prev="${COMP_WORDS[COMP_CWORD-1]}"z" words=("${COMP_WORDS[@]}")z cword=$COMP_CWORDz fiz' # Build the command path from wordsz local cmd_path=""z local iz% for (( i=1; i < cword; i++ )); doz case "${words[i]}" inz -*) continue ;;zB *) cmd_path="${cmd_path:+${cmd_path} }${words[i]}" ;;z esac donez case "$cmd_path" inc&t| |fSNr.ps r zgenerate_bash..gAwlrkey "")z% COMPREPLY=($(compgen -W "z " -- "$cur"))z return ;;r "") esac}z complete -F _z _completions  )rrr rkeysr1joinget) rrr(lines all_pathsr"r0r completionspattern root_subs root_flagsroot_completionss r generate_bashrQCs!((H E LL0$00111 LL????@@@ LL LL>%d++>>>??? LL1222 LLABBB LL5666 LL LL'((( LL:;;; LL=>>> LL5666 LL,--- LL LL LL:;;; LL())) LL LL8999 LL0111 LL./// LLL LL    LL LL LL*+++x}},B,BCCCI . .  $//hhte|,, ((4.. ,,,,--- NK N N N     ,----!2..Ib"%%Jxx J 677 LL LLO0@OOO LL())) LL LL LL LLL!1$!7!7LLdLLMMM LL 99U  rc2t|}dt|}g}|d||d||d|d|d||d|d|d |d|d |d |d |d |d|d|d|d|dt|d}|D]}|st ||}||}d|} |d| d|r8dd|D} |d| d|r8dd|D} |d| d||rdndd|d|dt |d } |d g} dd!| D}|d"|d|d| r8dd#| D} |d| d|d|d|d$|d%|d|||dd&|S)'z!Generate a zsh completion script.r z #compdef z# zsh completion for r4z completions zshr5z() {z local -a commands flagsz local cmd_pathz# # Build command path from wordsz cmd_path=()z" for word in ${words[2,-1]}; doz% [[ $word == -* ]] && continuez5 [[ $word == "$words[$CURRENT]" ]] && continuez cmd_path+=($word)r6z case "${cmd_path[*]}" inc&t| |fSr8r9r:s r r<zgenerate_zsh..r=rr>r@rArBc3"K|] }d|dV dS"Nr.0ss r zgenerate_zsh..s* 8 8aQ 8 8 8 8 8 8rz commands=()c3"K|] }d|dV dSrUrrXfs r rZzgenerate_zsh..s* 9 9aQ 9 9 9 9 9 9rz flags=(z; _describe 'command' commands -- flags && returnz compadd -- z && returnz ;;rc3"K|] }d|dV dSrUrrWs r rZzgenerate_zsh..s*55aQ555555rrCc3"K|] }d|dV dSrUrr]s r rZzgenerate_zsh..s*::!XXXX::::::rrDrErF)rr rrrGr1rHrI)rrr( func_namerJrKr"r0rrM desc_list flag_listrNrO root_descs r generate_zshres;!((H,$T**,,I E LL#T##$$$ LL///000 LL>>>>??? LL LLI$$$%%% LL./// LL%&&& LL LL6777 LL"### LL5666 LL8999 LLHIII LL,--- LL LL LL/000x}},B,BCCCI''  $//((4.. ,,,,---  @ 8 84 8 8 888I LL>)>>> ? ? ?  = 9 95 9 9 999I LL;y;;; < < <  G I IF388E??FFF    %&&&&!2..Ib"%%J55955555I LL LL6)6667779HH::z:::::  79777888 LLNOOO LL!""" LL LL LL LLI    LL 99U  rc t|}g}|d||d|d|dt|dD]}t ||}||}|sddt |d }|D]!}|d |d |d |d "|D]{} | dr(|d |d |d| ddd ?| dr'|d |d |d| ddd |g} |D]} | d| d| }|} | r|dd| z }|D]!}|d |d |d |d "|D]{} | dr(|d |d |d| ddd ?| dr'|d |d |d| ddd ||dd|S)z"Generate a fish completion script.z# fish completion for r4z completions fishr5c$t||fSr8r9r:s r r<zgenerate_fish..ss1vvqkrr>z not __fish_seen_subcommand_from r@rz complete -c z -n 'z ' -f -a ''z--z' -l 'N-z' -s 'r+z__fish_seen_subcommand_from z && z$ && not __fish_seen_subcommand_from rF)rrrrGr1rH startswith) rrr(rJr"r0r conditionr flag seen_partsr; child_subss r generate_fishrps!((H E LL0$00111 LL????@@@ LLx}},A,ABBB11$//- ?HH-h;;<<??    H4HHiHH#HHH  ??4((LLNtNN)NN48NNN__S))LLNtNN)NN48NNN  J F F!!"D"D"DEEEE J//IJ /,,//    H4HHiHH#HHH  ??4((LLNtNN)NN48NNN__S))LLNtNN)NN48NNN   LL 99U  r)bashzshfishshellct|}|-td|ddt|||S)zfGenerate completion script for the given shell. Raises ValueError if shell is not supported. NzUnsupported shell: z. Supported shells: z, ) GENERATORSrI ValueErrorrHSUPPORTED_SHELLS)rrtr generators r generate_completionsrzslu%%I ?% ? ?!%+;!rs $$$$$$$$$$.3.3....   # %S/49 $% x. 49     @  # @ S/ @ sCx$s)+ , @ @ @ @ 5c?DI-.  #s(O  #Y    2D@@  #@+.@@@@@H2D>>  #>+.>>>>>D2D@@  #@+.@@@@@H    6*//++,, ###  ## # # ######r