Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /ctrl-gaurav_effGen /effgen /completion.py
| """ | |
| Tab completion scripts for effGen CLI. | |
| The completion lists (subcommands, nested subcommands, presets, tool names) are | |
| generated by introspecting the live CLI parser and registries — never | |
| hand-maintained — so they can't drift out of sync with the real CLI the way a | |
| hardcoded list does. | |
| Usage: | |
| # Bash | |
| eval "$(effgen --completion bash)" | |
| # Zsh | |
| eval "$(effgen --completion zsh)" | |
| # Fish | |
| effgen --completion fish | source | |
| """ | |
| from __future__ import annotations | |
| # Commands whose flags rarely drift; listed once and shared across shells. | |
| _RUN_FLAGS = ( | |
| "--model --provider --tools --preset --verbose --explain --stream --config " | |
| "--system-prompt --temperature --max-iterations --mode --output --session-id" | |
| ) | |
| # A conservative fallback used only if live introspection fails for some reason | |
| # (a completion script must always be emitted and must always parse). | |
| _FALLBACK_COMMANDS = [ | |
| "run", "chat", "serve", "config", "tools", "models", "presets", | |
| "examples", "health", "doctor", "cost", "eval", "compare", "debug", | |
| "batch", "workflow", "create-plugin", | |
| ] | |
| _FALLBACK_PRESETS = ["math", "research", "coding", "general", "rag", "minimal"] | |
| _FALLBACK_TOOLS = ["calculator", "python_repl", "web_search", "bash", "datetime"] | |
| def _collect_cli_metadata() -> dict[str, object]: | |
| """Introspect the real CLI parser + registries for completion data. | |
| Returns a dict with ``commands`` (top-level subcommands), ``subcommands`` | |
| (command -> nested subcommand names), ``presets`` and ``tools``. Everything | |
| degrades to a small static fallback so a parseable script is always emitted. | |
| """ | |
| import argparse | |
| commands: list[str] = list(_FALLBACK_COMMANDS) | |
| subcommands: dict[str, list[str]] = {} | |
| try: | |
| from effgen.cli._main import create_parser | |
| parser = create_parser() | |
| for action in parser._actions: | |
| if not isinstance(action, argparse._SubParsersAction): | |
| continue | |
| commands = sorted(action.choices.keys()) | |
| # Capture nested subcommands (config show/validate, tools list/...). | |
| for name, sub in action.choices.items(): | |
| nested: list[str] = [] | |
| for sub_action in sub._actions: | |
| if isinstance(sub_action, argparse._SubParsersAction): | |
| nested = sorted(sub_action.choices.keys()) | |
| break | |
| if nested: | |
| subcommands[name] = nested | |
| break | |
| except Exception: | |
| pass | |
| try: | |
| from effgen.presets import list_presets | |
| presets = sorted(list_presets().keys()) | |
| except Exception: | |
| presets = list(_FALLBACK_PRESETS) | |
| try: | |
| from effgen.tools import get_registry | |
| tools = sorted(get_registry().list_tools()) | |
| except Exception: | |
| tools = list(_FALLBACK_TOOLS) | |
| return { | |
| "commands": commands, | |
| "subcommands": subcommands, | |
| "presets": presets, | |
| "tools": tools, | |
| } | |
| def bash_completion() -> str: | |
| """Generate bash completion script from the live CLI.""" | |
| meta = _collect_cli_metadata() | |
| commands = " ".join(meta["commands"]) # type: ignore[arg-type] | |
| presets = " ".join(meta["presets"]) # type: ignore[arg-type] | |
| tools = " ".join(meta["tools"]) # type: ignore[arg-type] | |
| subcommands: dict[str, list[str]] = meta["subcommands"] # type: ignore[assignment] | |
| # Per-command nested-subcommand cases (config/tools/models/…). | |
| nested_cases = "\n".join( | |
| f' {cmd})\n' | |
| f' COMPREPLY=( $(compgen -W "{" ".join(subs)}" -- "${{cur}}") )\n' | |
| f' return 0\n' | |
| f' ;;' | |
| for cmd, subs in sorted(subcommands.items()) | |
| ) | |
| return f''' | |
| _effgen_completion() {{ | |
| local cur prev commands presets tools | |
| COMPREPLY=() | |
| cur="${{COMP_WORDS[COMP_CWORD]}}" | |
| prev="${{COMP_WORDS[COMP_CWORD-1]}}" | |
| commands="{commands}" | |
| presets="{presets}" | |
| tools="{tools}" | |
| case "${{prev}}" in | |
| effgen|effgen-agent) | |
| COMPREPLY=( $(compgen -W "${{commands}}" -- "${{cur}}") ) | |
| return 0 | |
| ;; | |
| --preset) | |
| COMPREPLY=( $(compgen -W "${{presets}}" -- "${{cur}}") ) | |
| return 0 | |
| ;; | |
| --tools|-t) | |
| COMPREPLY=( $(compgen -W "${{tools}}" -- "${{cur}}") ) | |
| return 0 | |
| ;; | |
| --mode) | |
| COMPREPLY=( $(compgen -W "auto single sub_agents" -- "${{cur}}") ) | |
| return 0 | |
| ;; | |
| run|chat) | |
| COMPREPLY=( $(compgen -W "{_RUN_FLAGS}" -- "${{cur}}") ) | |
| return 0 | |
| ;; | |
| {nested_cases} | |
| esac | |
| if [[ "${{cur}}" == -* ]]; then | |
| COMPREPLY=( $(compgen -W "--help --version --verbose --quiet --log-file" -- "${{cur}}") ) | |
| fi | |
| }} | |
| complete -F _effgen_completion effgen | |
| complete -F _effgen_completion effgen-agent | |
| ''' | |
| def zsh_completion() -> str: | |
| """Generate zsh completion script from the live CLI.""" | |
| meta = _collect_cli_metadata() | |
| commands = " ".join(meta["commands"]) # type: ignore[arg-type] | |
| presets = " ".join(meta["presets"]) # type: ignore[arg-type] | |
| tools = " ".join(meta["tools"]) # type: ignore[arg-type] | |
| return f''' | |
| #compdef effgen effgen-agent | |
| _effgen() {{ | |
| local -a commands presets tools | |
| commands=({commands}) | |
| presets=({presets}) | |
| tools=({tools}) | |
| _arguments -C \\ | |
| '--version[Show version]' \\ | |
| '--verbose[Verbose output]' \\ | |
| '--quiet[Quiet output]' \\ | |
| '--log-file[Log file path]:file:_files' \\ | |
| '1:command:->command' \\ | |
| '*::arg:->args' | |
| case $state in | |
| command) | |
| _describe 'command' commands | |
| ;; | |
| args) | |
| case $words[1] in | |
| run|chat) | |
| _arguments \\ | |
| '--model[Model to use]:model:' \\ | |
| '--provider[Provider]:provider:' \\ | |
| '--tools[Tools to enable]:tool:($tools)' \\ | |
| '--preset[Use preset config]:preset:($presets)' \\ | |
| '--verbose[Show execution stats]' \\ | |
| '--explain[Show tool reasoning]' \\ | |
| '--stream[Stream output]' \\ | |
| '--config[Config file]:file:_files' \\ | |
| '--temperature[Temperature]:temp:' \\ | |
| '--max-iterations[Max iterations]:n:' \\ | |
| '--output[Output file]:file:_files' \\ | |
| '1:task:' | |
| ;; | |
| esac | |
| ;; | |
| esac | |
| }} | |
| _effgen "$@" | |
| ''' | |
| def fish_completion() -> str: | |
| """Generate fish completion script from the live CLI.""" | |
| meta = _collect_cli_metadata() | |
| commands: list[str] = meta["commands"] # type: ignore[assignment] | |
| presets = " ".join(meta["presets"]) # type: ignore[arg-type] | |
| tools = " ".join(meta["tools"]) # type: ignore[arg-type] | |
| cmd_lines = "\n".join( | |
| f'complete -c effgen -n "__fish_use_subcommand" -a "{cmd}"' | |
| for cmd in commands | |
| ) | |
| return f''' | |
| {cmd_lines} | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l preset -xa "{presets}" | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l model -d "Model to use" | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l provider -d "Provider" | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l tools -xa "{tools}" | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l verbose -d "Show execution stats" | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l explain -d "Show tool reasoning" | |
| complete -c effgen -n "__fish_seen_subcommand_from run chat" -l stream -d "Stream output" | |
| ''' | |
| def get_completion(shell: str) -> str: | |
| """Get completion script for the given shell.""" | |
| generators = { | |
| "bash": bash_completion, | |
| "zsh": zsh_completion, | |
| "fish": fish_completion, | |
| } | |
| gen = generators.get(shell) | |
| if gen is None: | |
| raise ValueError(f"Unsupported shell '{shell}'. Supported: bash, zsh, fish") | |
| return gen() | |
Xet Storage Details
- Size:
- 8.17 kB
- Xet hash:
- 61f4086d40c3e4f9526134e31cfa69d6ed641d0b9f2095ca19339bade97a9999
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.