#compdef ipsort

autoload -U is-at-least

_ipsort() {
    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[@]}" : \
'--generate-completions=[Generate shell completions for the given shell and print to stdout]:SHELL:(bash elvish fish powershell zsh)' \
'-r[Reverse the sort order]' \
'--reverse[Reverse the sort order]' \
'--ipv6-first[Sort IPv6 addresses before IPv4]' \
'-u[Remove duplicate addresses (compared by normalized CIDR)]' \
'--unique[Remove duplicate addresses (compared by normalized CIDR)]' \
'-i[Reorder all IP tokens freely across the entire input]' \
'--inline[Reorder all IP tokens freely across the entire input]' \
'-n[Emit canonical network strings (clears host bits, adds /32 or /128)]' \
'--normalize[Emit canonical network strings (clears host bits, adds /32 or /128)]' \
'(--ips-only-with-structure)--ips-only[Strip all non-IP content and emit one bare address per line]' \
'(--ips-only)--ips-only-with-structure[Strip decoration but preserve non-IP lines as block separators]' \
'-a[Merge adjacent CIDRs into their minimal supernet representation]' \
'--aggregate[Merge adjacent CIDRs into their minimal supernet representation]' \
'-c[Check whether input is already sorted; exit 0 if so, 1 if not. No output is printed. Reports the first out-of-order line to stderr on failure]' \
'--check[Check whether input is already sorted; exit 0 if so, 1 if not. No output is printed. Reports the first out-of-order line to stderr on failure]' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
'*::addresses -- Input CIDRs. Pass one or more addresses, a comma/space-separated list, or '\''-'\'' to read from stdin:_default' \
&& ret=0
}

(( $+functions[_ipsort_commands] )) ||
_ipsort_commands() {
    local commands; commands=()
    _describe -t commands 'ipsort commands' commands "$@"
}

if [ "$funcstack[1]" = "_ipsort" ]; then
    _ipsort "$@"
else
    compdef _ipsort ipsort
fi
