#compdef mq

autoload -U is-at-least

_mq() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'-I+[Set input format]:INPUT_FORMAT:(markdown mdx html text null raw bytes cbor csv gron json psv toml toon tsv xml yaml)' \
'--input-format=[Set input format]:INPUT_FORMAT:(markdown mdx html text null raw bytes cbor csv gron json psv toml toon tsv xml yaml)' \
'*-L+[Search modules from the directory]:MODULE_DIRECTORIES:_files' \
'*--directory=[Search modules from the directory]:MODULE_DIRECTORIES:_files' \
'*-M+[Load additional modules from specified files]:MODULE_NAMES:_default' \
'*--module-names=[Load additional modules from specified files]:MODULE_NAMES:_default' \
'*-m+[Import modules by name, making them available as \`name\:\:fn()\` in queries]:IMPORT_MODULE_NAMES:_default' \
'*--import-module-names=[Import modules by name, making them available as \`name\:\:fn()\` in queries]:IMPORT_MODULE_NAMES:_default' \
'*--args=[Sets a named string argument. NAME is accessible directly in queries, and also via ARGS."named" when --args or --argv is given]:NAME:_default:NAME:_default' \
'*--argjson=[Sets a named JSON argument. NAME is accessible directly in queries]:NAME:_default:NAME:_default' \
'*--rawfile=[Sets file contents that can be referenced at runtime]:NAME:_default:NAME:_default' \
'*--slurpfile=[Sets a named argument from a JSON file. NAME is bound to an array of every JSON value found in FILE (jq --slurpfile compatible), so a file containing a single JSON value becomes a one-element array]:NAME:_default:NAME:_default' \
'*--allowed-domain=[Allow HTTP imports from additional domain(s) beyond the default. Has no effect unless \`--allow-http-import\` (or \`--allow-all\`) is also passed. Use \`github.com/{user}/{repo}\` to allow a specific repository (expanded automatically), or a plain domain like \`example.com\` to allow any path under that host. Repeat to allow multiple extra domains]:ALLOWED_DOMAINS:_default' \
'--lockfile=[Path to the mq.lock file used for HTTP import integrity checks. Defaults to ./mq.lock (relative to the current directory)]:PATH:_files' \
'*-N+[Allow the \`http\` function to make outbound HTTPS requests. Disabled by default; requests are HTTPS-only and blocked from reaching loopback/private/link-local addresses regardless of this flag. Pass with no value to allow any domain, or \`--allow-net=DOMAIN\` (repeat the flag, or comma-separate, to add more) to restrict requests to just those domains (and any path under them). The \`=\` is required so a bare domain after the flag isn'\''t swallowed as a query/file positional instead]::DOMAIN:_default' \
'*--allow-net=[Allow the \`http\` function to make outbound HTTPS requests. Disabled by default; requests are HTTPS-only and blocked from reaching loopback/private/link-local addresses regardless of this flag. Pass with no value to allow any domain, or \`--allow-net=DOMAIN\` (repeat the flag, or comma-separate, to add more) to restrict requests to just those domains (and any path under them). The \`=\` is required so a bare domain after the flag isn'\''t swallowed as a query/file positional instead]::DOMAIN:_default' \
'*-R+[Allow the \`read_file\`/\`read_file_bytes\`/\`collection\`/\`file_exists\`/\`embed_images\` functions to read from the filesystem. Disabled by default. Pass with no value to allow reading anywhere, or \`--allow-read=PATH\` (files or directories; repeat the flag, or comma-separate, to add more) to restrict reads to just those paths and their descendants. The \`=\` is required so a bare path after the flag isn'\''t swallowed as a query/file positional instead]::PATH:_files' \
'*--allow-read=[Allow the \`read_file\`/\`read_file_bytes\`/\`collection\`/\`file_exists\`/\`embed_images\` functions to read from the filesystem. Disabled by default. Pass with no value to allow reading anywhere, or \`--allow-read=PATH\` (files or directories; repeat the flag, or comma-separate, to add more) to restrict reads to just those paths and their descendants. The \`=\` is required so a bare path after the flag isn'\''t swallowed as a query/file positional instead]::PATH:_files' \
'*-W+[Allow the \`write_file\`/\`extract_images\` functions to write to the filesystem. Disabled by default. Pass with no value to allow writing anywhere, or \`--allow-write=PATH\` (files or directories; repeat the flag, or comma-separate, to add more) to restrict writes to just those paths and their descendants. The \`=\` is required so a bare path after the flag isn'\''t swallowed as a query/file positional instead]::PATH:_files' \
'*--allow-write=[Allow the \`write_file\`/\`extract_images\` functions to write to the filesystem. Disabled by default. Pass with no value to allow writing anywhere, or \`--allow-write=PATH\` (files or directories; repeat the flag, or comma-separate, to add more) to restrict writes to just those paths and their descendants. The \`=\` is required so a bare path after the flag isn'\''t swallowed as a query/file positional instead]::PATH:_files' \
'*--allow-run=[Allow the \`system\` function to execute external commands. Disabled by default. Commands run directly (never through a shell), so shell metacharacters in arguments are never interpreted. Pass with no value to allow any command, or \`--allow-run=COMMAND\` (repeat the flag, or comma-separate, to add more) to restrict execution to just those commands. The \`=\` is required so a bare command after the flag isn'\''t swallowed as a query/file positional instead]::COMMAND:_default' \
'*-E+[Allow \`\$VAR\`/\`\${\$VAR}\` interpolation and debugger logpoints to read environment variables. Disabled by default. Pass with no value to allow reading any variable, or \`--allow-env=NAME\` (repeat the flag, or comma-separate, to add more) to restrict access to just those names. The \`=\` is required so a bare name after the flag isn'\''t swallowed as a query/file positional instead]::NAME:_default' \
'*--allow-env=[Allow \`\$VAR\`/\`\${\$VAR}\` interpolation and debugger logpoints to read environment variables. Disabled by default. Pass with no value to allow reading any variable, or \`--allow-env=NAME\` (repeat the flag, or comma-separate, to add more) to restrict access to just those names. The \`=\` is required so a bare name after the flag isn'\''t swallowed as a query/file positional instead]::NAME:_default' \
'-F+[Set output format]:OUTPUT_FORMAT:(markdown html text json table grep gron raw csv toml toon xml yaml shell none)' \
'--output-format=[Set output format]:OUTPUT_FORMAT:(markdown html text json table grep gron raw csv toml toon xml yaml shell none)' \
'--list-style=[Set the list style for markdown output]:LIST_STYLE:(dash plus star)' \
'--link-title-style=[Set the link title surround style for markdown output]:LINK_TITLE_STYLE:(double single paren)' \
'--link-url-style=[Set the link URL surround style for markdown links]:LINK_URL_STYLE:(none angle)' \
'-S+[Specify a query to insert between files as a separator]:QUERY:_default' \
'--separator=[Specify a query to insert between files as a separator]:QUERY:_default' \
'-o+[Output to the specified file]:FILE:_files' \
'--output=[Output to the specified file]:FILE:_files' \
'-B+[Show NUM nodes before each match. Only effective with -F grep]:NUM:_default' \
'--before-context=[Show NUM nodes before each match. Only effective with -F grep]:NUM:_default' \
'--after-context=[Show NUM nodes after each match. Only effective with -F grep]:NUM:_default' \
'--context=[Show NUM nodes before and after each match. Only effective with -F grep]:NUM:_default' \
'(-U --update)--skip=[Skip the first N matching results before outputting]:N:_default' \
'(-U --update)--limit=[Limit output to at most N results]:N:_default' \
'-P+[Number of files to process before switching to parallel processing]:PARALLEL_THRESHOLD:_default' \
'*--argv=[Positional string arguments, available as ARGS."positional" in queries]::ARGV:_default' \
'-O+[Optimization level for AST transformations (none = no changes, basic = constant folding and dead-branch elimination, full = all passes)]:OPTIMIZE_LEVEL:(none basic full)' \
'--optimize-level=[Optimization level for AST transformations (none = no changes, basic = constant folding and dead-branch elimination, full = all passes)]:OPTIMIZE_LEVEL:(none basic full)' \
'--timeout=[Maximum time in seconds allowed for query evaluation before aborting (e.g. 0.5, 5). No timeout by default]:SECONDS:_default' \
'-A[Aggregate all input files/content into a single array]' \
'--aggregate[Aggregate all input files/content into a single array]' \
'-f[load filter from the file]' \
'--from-file[load filter from the file]' \
'--stream[Enable streaming mode for processing large files line by line]' \
'--watch[Watch the input file(s) for changes and automatically re-run the query whenever they change. Requires at least one input file (stdin cannot be watched). With --from-file, the query file is watched too. Runs until interrupted (Ctrl-C); a query error is printed to stderr and watching continues rather than exiting]' \
'(-U --update -c --count --stream -S --separator)--eval-all[Evaluate the query once against all input files combined (like yq'\''s \`eval-all\`), instead of once per file. Enables cross-file aggregation in a single query]' \
'--allow-http-import[Allow \`import\`/\`include\` to fetch modules over HTTP(S). Disabled by default]' \
'--refresh-modules[Force re-fetch of mutable-ref (HEAD/branch) HTTP-imported modules, ignoring the local cache. Versioned (tagged) modules are never re-fetched regardless of this flag]' \
'--clear-cache[Remove all HTTP module cache including versioned (tagged) modules and lock files. Use this to fully reset the cache when something goes wrong]' \
'(--lockfile --frozen)--no-lockfile[Disable the mq.lock integrity check for HTTP imports. By default a fetched URL'\''s content is checked against mq.lock, and a mismatch is rejected unless --refresh-modules is also passed]' \
'--frozen[Fail instead of recording a new mq.lock entry. \`--frozen\`; use in CI so a new module'\''s content is only ever trusted during a reviewable local run whose mq.lock diff gets committed, not silently during CI]' \
'(-N --allow-net -R --allow-read -W --allow-write --allow-run -E --allow-env --allow-http-import)-a[Grant every sandboxed permission at once (read/write/net/run/env), and also enable HTTP module imports as if --allow-http-import were passed. Disabled by default. Cannot be combined with the individual --allow-* flags above]' \
'(-N --allow-net -R --allow-read -W --allow-write --allow-run -E --allow-env --allow-http-import)--allow-all[Grant every sandboxed permission at once (read/write/net/run/env), and also enable HTTP module imports as if --allow-http-import were passed. Disabled by default. Cannot be combined with the individual --allow-* flags above]' \
'-U[Update matching Markdown nodes and write the result to stdout]' \
'--update[Update matching Markdown nodes and write the result to stdout]' \
'--diff[With --update, print a unified diff instead of the transformed content; nothing is written. Multiple files are diffed one at a time with their path in the headers; stdin is labeled \`<stdin>\`. Exits 1 if anything would change]' \
'--unbuffered[Unbuffered output]' \
'-C[Colorize markdown output]' \
'--color-output[Colorize markdown output]' \
'-e[Exit with code 1 if the last output value is false, null, or the output is empty. Mirrors jq'\''s --exit-status / -e flag]' \
'--exit-status[Exit with code 1 if the last output value is false, null, or the output is empty. Mirrors jq'\''s --exit-status / -e flag]' \
'(-U --update --stream)-c[Output only the count of matching (non-None) results. Mirrors grep -c. With multiple files, prints "filename\: N" per file and "total\: N" at the end]' \
'(-U --update --stream)--count[Output only the count of matching (non-None) results. Mirrors grep -c. With multiple files, prints "filename\: N" per file and "total\: N" at the end]' \
'--no-position[Omit Markdown node position information from structured output (json, table, gron, csv, toml, toon, xml, yaml). Reduces output size when source line/column spans aren'\''t needed]' \
'--list[List all available subcommands (built-in and external)]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::query:_default' \
'::files:_files' \
":: :_mq_commands" \
"*::: :->mq" \
&& ret=0
    case $state in
    (mq)
        words=($line[3] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:mq-command-$line[3]:"
        case $line[3] in
            (repl)
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
'*::files -- Markdown (or other supported format) files to load as the REPL'\''s initial input:_files' \
&& ret=0
;;
(completion)
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
':shell -- Shell to generate the completion script for:(bash elvish fish nushell powershell zsh)' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
'(--markdown)--json[Print machine-readable JSON instead of formatted text]' \
'--markdown[Print Markdown instead of formatted text — e.g. queryable with mq itself\: \`mq help section --markdown | mq '\''select(.code.lang == "mq")'\''\`]' \
'-h[Print help]' \
'--help[Print help]' \
'::name -- Name of a function, selector, module, or `examples`, e.g. `map`, `.h1`, `csv_parse`, `section`, `examples`:_default' \
&& ret=0
;;
        esac
    ;;
esac
}

(( $+functions[_mq_commands] )) ||
_mq_commands() {
    local commands; commands=(
'repl:Start a REPL session for interactive query execution. Optional FILES are combined as the initial input (same file/format handling as \`mq QUERY FILES...\`)' \
'completion:Generate a shell completion script and print it to stdout' \
'help:Show documentation for a builtin function, selector, standard module, standard-module function, or the \`examples\` topic' \
    )
    _describe -t commands 'mq commands' commands "$@"
}
(( $+functions[_mq__subcmd__completion_commands] )) ||
_mq__subcmd__completion_commands() {
    local commands; commands=()
    _describe -t commands 'mq completion commands' commands "$@"
}
(( $+functions[_mq__subcmd__help_commands] )) ||
_mq__subcmd__help_commands() {
    local commands; commands=()
    _describe -t commands 'mq help commands' commands "$@"
}
(( $+functions[_mq__subcmd__repl_commands] )) ||
_mq__subcmd__repl_commands() {
    local commands; commands=()
    _describe -t commands 'mq repl commands' commands "$@"
}

if [ "$funcstack[1]" = "_mq" ]; then
    _mq "$@"
else
    compdef _mq mq
fi
