#compdef pcp pcp2elasticsearch pcp2graphite pcp2influxdb pcp2json pcp2spark pcp2xlsx pcp2xml pcp2zabbix pmafm pmchart pmclient pmclient_fg=pmclient pmdbg pmdiff pmdumplog pmdumptext pmerr pmfind pmie pmie2col pminfo pmiostat pmlogcheck pmlogextract pmlogger pmloglabel pmlogsummary pmprobe pmrep pmseries pmstat pmstore pmval
#
# PCP <https://pcp.io> completions for zsh <http://zsh.sf.net>.
#
# Started by Marko Myllynen <myllynen@redhat.com> 2016.
#
# Configuration:
#
#  * PCP offers a *lot* of metrics, hence something like 'pminfo <tab>'
#    returns a lot of possible completions. If you wish to complete
#    metrics piecewise instead of using their complete names (meaning
#    that 'pminfo me<tab>' will offer only 'mem.' not all the metrics
#    under 'mem'), this can be enabled with:
#
#      % zstyle ':completion:*:*:pcp:metrics' full-names off
#
#    In both modes partial word completion is enabled (meaning that when
#    completing 'pminfo m.u.f<tab>' it expands to 'pminfo mem.util.free'.
#
#  * Container completion offers by default the names of running
#    containers while IDs are also recognized if being completed. To use
#    IDs instead of names (but still recognize names, too), set:
#
#      % zstyle ':completion:*:*:pcp:containers' use-ids on
#
#

_pcp () {
  local curcontext="$curcontext" state line expl ret=1
  local common_help common_most common_pmda arch_req=0
  declare -A opt_args

  local -a py_tools
  py_tools=(pcp2elasticsearch pcp2graphite pcp2influxdb pcp2json pcp2spark pcp2xlsx pcp2xml pcp2zabbix pmrep)

  local exargs="-? --help -V --version"
  common_help=(
    '(- *)'{-\?,--help}'[display help message]'
    '(- *)'{-V,--version}'[display version number]'
  )
  common_most=(
    "(--container -h --host -L --local-PMDA -K --spec-local $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives'
    "(-h --host -a --archive -L --local-PMDA -K --spec-local $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts'
    "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]'
    "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone'
    "(-s --samples $exargs)"{-s+,--samples=}'[specify number of samples]:samples:'
    "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:'
    "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:'
    "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:'
    "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:'
    "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:'
  )
  common_pmda=(
    "(-L --local-PMDA -a --archive -h --host $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]'
    "(-a --archive -h --host $exargs)"\*{-K+,--spec-local=}'[define PMDA spec for local DSO PMDAs]:spec:(add del clear)'
  )
  common_python=(
    "(--archive-folio --container -h --host -L --local-PMDA -K --spec-local $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives'
    "(--archive-folio -a --archive --container -h --host -L --local-pmda -K --spec-local $exargs)"--archive-folio=+'[specify metrics source folio]:folio:_files'
    "(--container -a --archive --archive-folio -d --delay -u --no-interpol $exargs)"--container=+'[specify container to query]:container:->containers'
    "(-h --host -a --archive --archive-folio -L --local-PMDA -K --spec-local $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts'
    "(--daemonize $exargs)"--daemonize'[daemonize on startup]'
    "(-s --samples $exargs)"{-s+,--samples=}'[specify number of samples]:samples:'
    "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:'
    "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:'
    "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:'
    "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:'
    "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:'
    "(-c --config $exargs)"{-c+,--config=}'[specify config file]:file:_files'
    "(-C --check $exargs)"{-C,--check}'[check config only]'
    "($exargs)"\*{-e+,--derived=}'[derived metrics definitions]:file or formula:_files'
    "(-H --no-header $exargs)"{-H,--no-header}'[omit headers]'
    "(-G --no-globals $exargs)"{-G,--no-globals}'[omit global metrics]'
    "(-r --raw -R --raw-prefer $exargs)"{-r,--raw}'[force reporting raw values]'
    "(-R --raw-prefer -r --raw $exargs)"{-R,--raw-prefer}'[prefer reporting raw values]'
    "(-I --ignore-incompat $exargs)"{-I,--ignore-incompat}'[ignore incompatible metrics]'
    "(-5 --ignore-unknown $exargs)"{-5,--ignore-unknown}'[ignore unknown metrics]'
    "(-4 --names-change $exargs)"{-4+,--names-change=}'[update/ignore/abort on metric name changes]:action:(abort ignore update)'
    "($exargs)"\*{-i+,--instances=}'[specify instances to report]:instances:->instances'
    "(-v --omit-flat $exargs)"{-v,--omit-flat}'[omit single-valued metrics]'
    "(-P --precision $exargs)"{-P+,--precision=}'[set default floating point precision]:precision:'
    "(-0 --precision-force $exargs)"{-0+,--precision-force=}'[set forced floating point precision]:precision:'
    "(-q --count-scale -Q --count-scale-force $exargs)"{-q+,--count-scale=}'[set default count unit]:scale:->scales_count'
    "(-Q --count-scale-force -q --count-scale $exargs)"{-Q+,--count-scale-force=}'[set forced count unit]:scale:->scales_count'
    "(-b --space-scale -B --space-scale-force $exargs)"{-b+,--space-scale=}'[set default space unit]:scale:->scales_space'
    "(-B --space-scale-force -b --space-scale $exargs)"{-B+,--space-scale-force=}'[set forced space unit]:scale:->scales_space'
    "(-y --time-scale -Y --time-scale-force $exargs)"{-y+,--time-scale=}'[set default time unit]:scale:->scales_time'
    "(-Y --time-scale-force -y --time-scale $exargs)"{-Y+,--time-scale-force=}'[set forced time unit]:scale:->scales_time'
  )

  case $service in
  pcp)
    if (( ! $+_cache_pcp_cmds )); then
      _cache_pcp_cmds=( ${="$(pcp --help 2>&1)"#*Available Commands:} )
    fi
    if (( ! $+_cache_pcp_cmdopts )); then
      typeset -gA _cache_pcp_cmdopts
    fi
    _arguments -A "-*" -C -S -s \
      "$common_help[@]" \
      "$common_most[@]" \
      "(-g --guimode $exargs)"{-g,--guimode}'[GUI mode with new time control]' \
      "(-L --local-PMDA -a --archive -h --host -H --hostsfile $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-p --guiport $exargs)"{-p+,--guiport=}'[specify existing time control port]:port:' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      - set1 \
      '1:command:->pcp_commands' \
      - set2 \
      "(-P --pmie $exargs)"{-P,--pmie}'[display pmie evaluation statistics]' \
      && return 0
      # We accept only PCP command options after the first non-option argument
      # (i.e., the PCP command itself), this makes it so with the -A "-*" above
      [[ -z $state ]] && state=pcp_cmdopts
  ;;
  pcp2elasticsearch)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-g --es-host $exargs)"{-g+,--es-host=}'[elasticsearch server]:host:_hosts' \
      "(-x --es-index $exargs)"{-x+,--es-index=}'[elasticsearch index]:index:' \
      "(-X --es-hostid $exargs)"{-X+,--es-hostid=}'[elasticsearch host-id]:host-id:' \
      "(-p --es-search-type $exargs)"{-p+,--es-search-type=}'[elasticsearch search type]:type:' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2graphite)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-E --text-port -p --pickle-port -X --pickle-protocol $exargs)"{-E+,--text-port=}'[graphite plaintext port]:port:' \
      "(-g --graphite-host $exargs)"{-g+,--graphite-host=}'[graphite server]:host:_hosts' \
      "(-p --pickle-port -E --text-port $exargs)"{-p+,--pickle-port=}'[graphite pickle port]:port:' \
      "(-x --prefix $exargs)"{-x+,--prefix=}'[prefix for metric names]:port:' \
      "(-X --pickle-protocol -E --text-port $exargs)"{-X+,--pickle-protocol=}'[pickle protocol version]:protocol:' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2influxdb)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-E --db-pass $exargs)"{-E+,--db-pass=}'[password for database]:password:' \
      "(-g --db-server $exargs)"{-g+,--db-server=}'[server URL]:url:_urls' \
      "(-U --db-user $exargs)"{-U+,--db-user=}'[username for database]:username:_users' \
      "(-x --db-name $exargs)"{-x+,--db-name=}'[metrics database name]:database:' \
      "(-X --db-tags $exargs)"{-X+,--db-tags=}'[string of tags to add to the metrics]:tags:' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2json)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-f --timestamp-format $exargs)"{-f+,--timestamp-format=}'[set time format string]:format:' \
      "(-F --output-file $exargs)"{-F+,--output-file=}'[specify output file]:file:_files' \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-x --extended -X --everything $exargs)"{-x,--extended}'[write extended information]' \
      "(-X --everything -x --extended $exargs)"{-X,--everything}'[write everything, incl. internals]' \
      "(-E --exact-types $exargs)"{-E,--exact-types}'[write numbers as number data types]' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2spark)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-g --spark-server $exargs)"{-g+,--spark-server=}'[spark server]:host:_hosts' \
      "(-p --spark-port $exargs)"{-p+,--spark-port=}'[spark port]:port:' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2xlsx)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-f --timestamp-format $exargs)"{-f+,--timestamp-format=}'[set time format string]:format:' \
      "(-F --output-file $exargs)"{-F+,--output-file=}'[specify output file]:file:_files' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2xml)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-f --timestamp-format $exargs)"{-f+,--timestamp-format=}'[set time format string]:format:' \
      "(-F --output-file $exargs)"{-F+,--output-file=}'[specify output file]:file:_files' \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-x --extended -X --everything $exargs)"{-x,--extended}'[write extended information]' \
      "(-X --everything -x --extended $exargs)"{-X,--everything}'[write everything, incl. internals]' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pcp2zabbix)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-E --zabbix-interval $exargs)"{-E+,--zabbix-interval=}'[send interval]:interval:' \
      "(-g --zabbix-server $exargs)"{-g+,--zabbix-server=}'[zabbix server]:host:_hosts' \
      "(-p --zabbix-port $exargs)"{-p+,--zabbix-port=}'[zabbix port]:port:' \
      "(-x --zabbix-prefix $exargs)"{-x+,--zabbix-prefix=}'[prefix for metric names]:prefix:' \
      "(-X --zabbix-host $exargs)"{-X+,--zabbix-host=}'[zabbix host-id]:host-id:' \
      "(-l --zabbix-lld $exargs)"{-l,--zabbix-lld}'[enable low-level discovery]' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pmafm)
    _arguments -A "-*" \
      '(- *)'{-\?,--help}'[display help message]' \
      '1:folio:_files' \
      '2:command:(archives check help hosts list quit remove repeat replay run selections)' \
      && return 0
  ;;
  pmchart)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:' \
      "(-h --host -H --hostsfile -L --local-PMDA -K --spec-local $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-a --archive -L --local-PMDA -K --spec-local $exargs)"\*{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-a --archive -L --local-PMDA -K --spec-local $exargs)"\*{-H+,--hostsfile=}'[specify metrics source hosts file]:file:_files' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-a --archive -h --host -H --hostsfile $exargs)"\*{-K+,--spec-local=}'[define PMDA spec for local DSO PMDAs]:spec:(add del clear)' \
      "(-L --local-PMDA -a --archive -h --host -H --hostsfile $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-p --guiport $exargs)"{-p+,--guiport=}'[specify existing time control port]:port:' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-c --view $exargs)"\*{-c+,--view=}'[specify chart views to load]:file:_files' \
      "(-C --check $exargs)"{-C,--check}'[check views only]' \
      "(-F --font-size $exargs)"{-F+,--font-size=}'[specify font size]:size:' \
      "(-f --font-family $exargs)"{-f+,--font-family=}'[specify font family]:family:_xft_fonts' \
      "(-g --geometry $exargs)"{-g+,--geometry=}'[specify image geometry (WxH)]:geometry:' \
      "(-o --output $exargs)"{-o+,--output=}'[specify output file]:file:_files' \
      "(-s --samples $exargs)"{-s+,--samples=}'[buffer up N points of sample history]:samples:' \
      "(-v --visible $exargs)"{-v+,--visible=}'[set points to display]:points:' \
      "(-W --white $exargs)"{-W,--white}'[export using opaque (white) bg]' \
      '*:source:_hosts' \
      && return 0
  ;;
  pmclient)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_most[@]" \
      "(-g --guimode $exargs)"{-g,--guimode}'[GUI mode with new time control]' \
      "(-p --guiport $exargs)"{-p+,--guiport=}'[specify existing time control port]:port:' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-P --pause -h --host $exargs)"{-P,--pause}'[pause between updates in archive mode]' \
      && return 0
  ;;
  pmdbg)
    _arguments -A "-*" \
      '(- *)'{-\?,--help}'[display help message]' \
      '(- *)'{-o,--old}'[old and deprecated (bit-field) encodings]' \
      '(- *)'{-l,--list}'[display values and text for all PCP debug options]' \
      '(- *)'-D'[parse name]:name:( $(pmdbg -l -o | awk "/^DBG_/ {print \$1}" | sed -e "s,DBG_TRACE_,,g") )' \
      '*:code:( $(pmdbg -l -o | awk "/^DBG_/ {print \$2}") )' \
      && return 0
  ;;
  pmdiff)
    _arguments -A "-*" -C -S -s \
      "$common_help[@]" \
      "(-d --keep $exargs)"{-d,--keep}'[debug, keep temp files]' \
      "(-p --precision $exargs)"{-p+,--precision=}'[set floating point precision]:precision:' \
      "(-q --threshold $exargs)"{-q+,--threshold=}'[specify reporting threshold]:threshold:' \
      "(--skip-excluded $exargs)"--skip-excluded'[omit excluded metrics info]' \
      "(--skip-missing $exargs)"--skip-missing'[omit missing metrics info]' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-B --begin $exargs)"{-B+,--begin=}'[set start of second time window]:timespec:' \
      "(-E --end $exargs)"{-E+,--end=}'[set end of time second window]:timespec:' \
      "(-x $exargs)"-x+'[define exclude pattern]:regex:' \
      "(-X $exargs)"-X+'[specify exclude pattern file]:file:_files' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      '1:archive:->archives' \
      '2:archive:->archives' \
      && return 0
  ;;
  pmdumplog)
    arch_req=1
    _arguments -C -S -s \
      "$common_help[@]" \
      - set1 \
      "(-a --all $exargs)"{-a,--all}'[dump everything]' \
      "(-d --descs $exargs)"{-d,--descs}'[dump metric descriptions]' \
      "(-e --labelsets $exargs)"{-e,--labelsets}'[dump all metric label metadata]' \
      "(-h --helptext $exargs)"{-h,--helptext}'[dump all metric help text]' \
      "(-i --insts $exargs)"{-i,--insts}'[dump instance domain descriptions]' \
      "(-L $exargs)"-L'[verbose label dump]' \
      "(-l --label $exargs)"{-l,--label}'[dump archive log label]' \
      "(-M --markrecs $exargs)"{-M,--markrecs}'[report <mark> records]' \
      "(-m --metrics $exargs)"{-m,--metrics}'[dump values of metrics]' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-r --reverse $exargs)"{-r,--reverse}'[reverse chronological order]' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-s --sizes $exargs)"{-s,--sizes}'[report data record sizes]' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-t $exargs)"-t'[dump temporal index]' \
      "(-x $exargs)"-x'[include date in timestamps]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      '1:archive:->archives' \
      '*:metric:->metrics' \
      - set2 \
      "(-v $exargs)"-v+'[specify log to dump as raw hex]:file:_files' \
      && return 0
  ;;
  pmdumptext)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:' \
      "(-h --host $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-h --host -a --archive $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-s --samples $exargs)"{-s+,--samples=}'[specify number of samples]:samples:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-c --config $exargs)"{-c+,--config=}'[specify metrics list file]:file:_files' \
      "(-C --check $exargs)"{-C,--check}'[check metrics only]' \
      "(-d --delimiter $exargs)"{-d+,--delimiter=}'[set column delimiter]:delimiter:' \
      "(-f --time-format $exargs)"{-f+,--time-format=}'[set time format string]:format:' \
      "(-F --fixed $exargs)"{-F,--fixed}'[use fixed width values]' \
      "(-G --scientific $exargs)"{-G,--scientific}'[use scientific format (if shorter)]' \
      "(-H --headers $exargs)"{-H,--headers}'[show all headers]' \
      "(-i --interactive $exargs)"{-i,--interactive}'[format output for interactive use]' \
      "(-l --source $exargs)"{-l,--source}'[show metrics source]' \
      "(-m --metrics $exargs)"{-m,--metrics}'[show metric names]' \
      "(-M $exargs)"-M'[show complete metric names]' \
      "(-N $exargs)"-N'[show normalizing factor]' \
      "(-o --offset $exargs)"{-o,--offset}'[prefix timestamp with offset]' \
      "(-P --precision $exargs)"{-P+,--precision=}'[set floating point precision]:precision:' \
      "(-R --repeat $exargs)"{-R+,--repeat=}'[set header repeat frequency]:samples:' \
      "(-r --raw $exargs)"{-r,--raw}'[report raw values]' \
      "(-U --unavailable $exargs)"{-U+,--unavailable=}'[define string for unavailable values]:string:' \
      "(-u --units $exargs)"{-u,--units}'[show metric units]' \
      "(-w --width $exargs)"{-w+,--width=}'[set column width]:width:' \
      "(-X --extended $exargs)"{-X,--extended}'[show extended metric names]' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pmerr)
    _arguments -A "-*" \
      '(- *)'{-\?,--help}'[display help message]' \
      '(- *)'{-l,--list}'[list known debug flags]' \
      '*:code:( $(pmerr -l | awk "/ PM_ERR_/ {print \$1}") )' \
      && return 0
  ;;
  pmfind)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-m --mechanism $exargs)"{-m+,--mechanism=}'[specify service discovery mechanism]:mechanism:->pmfind_mechs' \
      "(-r --resolve $exargs)"{-r,--resolve}'[resolve addresses]' \
      "(-s --service $exargs)"{-s+,--service=}'[specify service to find]:service:(all pmcd pmproxy)' \
      "(-t --timeout $exargs)"{-t+,--timeout=}'[set discovery timeout in seconds]:timeout:' \
      "(-q --quiet $exargs)"{-q,--quiet}'[quiet mode]' \
      && return 0
  ;;
  pmie)
    exargs="-? --help"
    _arguments -C -S -s \
      '(- *)'{-\?,--help}'[display help message]' \
      "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:' \
      "(-h --host $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-a --archive $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-C --check $exargs)"{-C,--check}'[check config only]' \
      "(-c --config $exargs)"{-c+,--config=}'[specify config file]:file:_files' \
      "(-d --interact $exargs)"{-d,--interact}'[interactive debugging mode]' \
      "(-f --foreground $exargs)"{-f,--foreground}'[run in foreground]' \
      "(-P --primary $exargs)"{-P,--primary}'[run as primary pmie instance]' \
      "(-j $exargs)"-j+'[specify stomp protocol file]:file:_files' \
      "(-l --logfile $exargs)"{-l+,--logfile=}'[specify log file]:file:_files' \
      "(-U --username $exargs)"{-U+,--username=}'[specify username to run as]:user:_users' \
      "(-b --buffer $exargs)"{-b,--buffer}'[use line buffered output]' \
      "(-e --timestamp $exargs)"{-e,--timestamp}'[print timestamps in verbose mode]' \
      "(-q --quiet $exargs)"{-q,--quiet}'[quiet mode]' \
      "(-v $exargs)"-v'[verbose mode]' \
      "(-V --verbose $exargs)"{-V,--verbose}'[more verbose mode]' \
      "(-W $exargs)"-W'[most verbose mode]' \
      "(-X --secret-applet -x --secret-agent $exargs)"{-X,--secret-applet}'[run in thin client mode]' \
      "(-x --secret-agent -X --secret-applet $exargs)"{-x,--secret-agent}'[run in domain agent mode]' \
      '*:config:_files' \
      && return 0
  ;;
  pmie2col)
    exargs="-? --help"
    _arguments -A "-*" -C -S -s \
      '(- *)'{-\?,--help}'[display help message]' \
      "(-d --delimiter $exargs)"{-d+,--delimiter=}'[specify output delimiter character]:delimiter:' \
      "(-p --precision $exargs)"{-p+,--precision=}'[specify output floating point precision]:precision:' \
      "(-w --width $exargs)"{-w+,--width=}'[specify output column width]:width:' \
      && return 0
  ;;
  pminfo)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(--container -h --host -L --local-PMDA -K --spec-local $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-h --host -a --archive -L --local-PMDA -K --spec-local $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(--container -a --archive $exargs)"--container=+'[specify container to query]:container:->containers' \
      "(-L --local-PMDA -a --archive -h --host $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-a --archive -h --host $exargs)"\*{-K+,--spec-local=}'[define PMDA spec for local DSO PMDAs]:spec:(add del clear)' \
      "(-n --namespace -N --uniqnames $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-N --uniqnames -n --namespace $exargs)"{-N+,--uniqnames=}'[specify alt. PMNS with unique PMID names]:pmnsfile:_files' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-b --batch $exargs)"{-b+,--batch=}'[set fetch batch size]:size:' \
      "(-d --desc $exargs)"{-d,--desc}'[print metric description]' \
      "(-f --fetch $exargs)"{-f,--fetch}'[fetch and print metric values]' \
      "(-F --fetchall $exargs)"{-F,--fetchall}'[fetch and print all metric values]' \
      "(-I --fullindom $exargs)"{-I,--fullindom}'[print InDom in verbose format]' \
      "(-l --labels $exargs)"{-l,--labels}'[print metric value labels]' \
      "(-m --pmid $exargs)"{-m,--pmid}'[print PMID]' \
      "(-M --fullpmid $exargs)"{-M,--fullpmid}'[print PMID verbosely]' \
      "(-s --series $exargs)"{-s,--series}'[print source, metric and instance series identifiers]' \
      "(-t --oneline $exargs)"{-t,--oneline}'[print terse help text]' \
      "(-T --helptext $exargs)"{-T,--helptext}'[print verbose help text]' \
      "(-c --derived $exargs)"{-c+,--derived=}'[specify derived metrics config]:file:_files' \
      "(-x --events $exargs)"{-x,--events}'[unpack and report events]' \
      "(-v --verify $exargs)"{-v,--verify}'[verify mode, report errors only]' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pmiostat)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:' \
      "(-h --host $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-G --aggregate $exargs)"{-G+,--aggregate=}'[set method to aggregate device values]:method:(sum avg min max)' \
      "(-h --host -a --archive -u --no-interpolation $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-P --precision $exargs)"{-P+,--precision=}'[set floating point precision]:precision:' \
      "(-R --regex $exargs)"{-R+,--regex=}'[define device matching regex]:regex:' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-s --samples $exargs)"{-s+,--samples=}'[specify number of samples]:samples:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:' \
      "(-u --no-interpolation -h --host $exargs)"{-u,--no-interpolation}'[disable interpolation]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-x $exargs)"-x+'[set comma-separated extended options]:options:->pmiostat_extopts' \
      && return 0
  ;;
  pmlogcheck)
    exargs="-? --help"
    _arguments -C -S -s \
      '(- *)'{-\?,--help}'[display help message]' \
      "(-l --label $exargs)"{-l,--label}'[print archive label]' \
      "(-m,--metadataonly $exargs)"{-m,--metadataonly}'[skip checking log data volumes]' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-v --verbose $exargs)"{-v,--verbose}'[verbose output]' \
      "(-w --nowrap $exargs)"{-w,--nowrap}'[suppress counter wrap warnings]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      '1:archive:->archives' \
      && return 0
  ;;
  pmlogextract)
    exargs="-? --help"
    _arguments -C -S -s \
      '(- *)'{-\?,--help}'[display help message]' \
      "(-c --config $exargs)"{-c+,--config=}'[specify config file]:file:_files' \
      "(-d --desperate $exargs)"{-d,--desperate}'[save output after fatal error]' \
      "(-f --first $exargs)"{-f,--first}'[use timezone from the first (not last) archive]' \
      "(-m --mark $exargs)"{-m,--mark}'[ignore prologue/epilogue and <mark> records between archives]' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-s --samples $exargs)"{-s+,--samples=}'[specify number of log records to write]:samples:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-v $exargs)"-v+'[switch log volumes after this many samples]' \
      "(-w $exargs)"-w'[ignore day/month/year]' \
      "(-x $exargs)"-x'[skip metrics with mismatched metadata]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      '*:archive:->archives' \
      && return 0
  ;;
  pmlogger)
    exargs="-? --help"
    _arguments -C -S -s \
      '(- *)'{-\?,--help}'[display help message]' \
      "(-c --config $exargs)"{-c+,--config=}'[specify config file]:file:_files' \
      "(-C --check $exargs)"{-C,--check}'[check config only]' \
      "(-h --host -o --local-PMDA -K --spec-local $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-H --labelhost $exargs)"{-H+,--labelhost=}'[specify hostname label]:label:' \
      "(-l --log $exargs)"{-l+,--log=}'[specify log file]:file:_files' \
      "(-L --linger $exargs)"{-L,--linger}'[linger even if not logging]' \
      "(-m --note $exargs)"{-m+,--note=}'[define map file note]:note:' \
      "(-h --host $exargs)"\*{-K+,--spec-local=}'[define PMDA spec for local DSO PMDAs]:spec:(add del clear)' \
      "(-o --local-PMDA -h --host $exargs)"{-o,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-p --PID $exargs)"{-p+,--PID=}'[specify PID to exit after]:PID:_pids' \
      "(-P --primary $exargs)"{-P,--primary}'[run as primary logger instance]' \
      "(-r --report $exargs)"{-r,--report}'[report record sizes]' \
      "(-s --size $exargs)"{-s+,--size=}'[set endsize of archive]:size:' \
      "(-t --interval $exargs)"{-t+,--interval=}'[set logging interval]:interval:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-U --username $exargs)"{-U+,--username=}'[specify username to run as]:user:_users' \
      "(-v --volsize $exargs)"{-v+,--volsize=}'[set log volume size]:size:' \
      "(-x $exargs)"-x+'[set control file descriptor]:fd:_file_descriptors' \
      "(-y $exargs)"-y'[use local time not PMCD timezone]' \
      '1:archive:_files' \
      && return 0
  ;;
  pmloglabel)
    exargs="-? --help"
    _arguments -C -S -s \
      '(- *)'-\?'[display help message]' \
      "(-h --host $exargs)"{-h+,--host=}'[set hostname for all files in archive]:hostname:_hosts' \
      "(-l --label $exargs)"{-l,--label}'[print archive label]' \
      "(-L $exargs)"-L'[print archive label verbosely]' \
      "(-p --pid $exargs)"{-p+,--pid=}'[set logger PID for all files in archive]:pid:' \
      "(-s $exargs)"-s'[write label sentinel values for all files in archive]' \
      "(-v --verbose $exargs)"{-v,--verbose}'[verbose output]' \
      "(-V --version $exargs)"{-V+,--version=}'[set version number for all files in archive]' \
      "(-Z --timezone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      '1:archive:->archives' \
      && return 0
  ;;
  pmlogsummary)
    arch_req=1
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-a --all $exargs)"{-a,--all}'[print all information]' \
      "(-b -x $exargs)"-b'[print both type time averages]' \
      "(-B --bins $exargs)"{-B+,--bins=}'[set number of bins]:bins:' \
      "(-f -F $exargs)"-f'[spreadsheet format with tabs]' \
      "(-F -f $exargs)"-F'[spreadsheet format with commas]' \
      "(-H --header $exargs)"{-H,--header}'[print header]' \
      "(-i --mintime $exargs)"{-i,--mintime}'[print time of min value]' \
      "(-I --maxtime $exargs)"{-I,--maxtime}'[print time of max value]' \
      "(-l --label $exargs)"{-l,--label}'[print archive label]' \
      "(-m --minimum $exargs)"{-m,--minimum}'[print minimum value]' \
      "(-M --maximum $exargs)"{-M,--maximum}'[print maximum value]' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-N -v --verbose --$exargs)"-N'[suppress warnings]' \
      "(-p --precision $exargs)"{-p+,--precision=}'[set floating point precision]:precision:' \
      "(-s --sum $exargs)"{-s,--sum}'[only print value sums]' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-v --verbose -N $exargs)"{-v,--verbose}'[verbose mode]' \
      "(-x -b $exargs)"-x'[print stochastic averages only]' \
      "(-y --samples $exargs)"-y'[print sample counts]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      '1:archive:->archives' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pmprobe)
    exargs="-? --help -d --version"
    _arguments -C -S -s \
      '(- *)'{-\?,--help}'[display help message]' \
      '(- *)'{-d,--version}'[display version number]' \
      "(--container -h --host -L --local-PMDA -K --spec-local $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-h --host -a --archive -L --local-PMDA -K --spec-local $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(--container -a --archive $exargs)"--container=+'[specify container to query]:container:->containers' \
      "(--derived $exargs)"--derived=+'[specify derived metrics config]:file:_files' \
      "(-L --local-PMDA -a --archice -h --host $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-a --archive -h --host $exargs)"\*{-K+,--spec-local=}'[define PMDA spec for local DSO PMDAs]:spec:(add del clear)' \
      "(-b --batch $exargs)"{-b+,--batch=}'[set fetch batch size]:size:' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-f --force $exargs)"{-f,--force}'[report all instances]' \
      "(-F --faster $exargs)"{-F,--faster}'[assume metrics are PMNS leaf nodes]' \
      "(-I --external -v --values $exargs)"{-I,--external}'[list external instance names]' \
      "(-i --internal -v --values $exargs)"{-i,--internal}'[list internal instance numbers]' \
      "(-V --verbose $exargs)"{-V,--verbose}'[report PDU operations]' \
      "(-v --values -I --external -i --internal $exargs)"{-v,--values}'[report metric values]' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pmrep)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_most[@]" \
      "$common_pmda[@]" \
      "$common_python[@]" \
      "(-o --output $exargs)"{-o+,--output=}'[set output target]:target:(archive csv stdout)' \
      "(-F --output-file $exargs)"{-F+,--output-file=}'[specify output file]:file:_files' \
      "(-U --no-unit-info $exargs)"{-U,--no-unit-info}'[omit unit info]' \
      "(-p --timestamps $exargs)"{-p,--timestamps}'[print timestamps]' \
      "(-d --delay --container -h --host -L --local-PMDA -K --spec-local -u --no-interpol $exargs)"{-d,--delay}'[delay between updates in archive mode]' \
      "(--include-texts $exargs)"--include-texts'[include metric help texts in archive output]' \
      "(-X --colxrow $exargs)"{-X+,--colxrow=}'[swap stdout columns and rows using header label]:label:' \
      "(-w --width -W --width-force $exargs)"{-w+,--width=}'[set default column width]:width:' \
      "(-W --width-force -w --width $exargs)"{-W+,--width-force=}'[forced column width]:width:' \
      "(-l --delimiter $exargs)"{-l+,--delimiter=}'[set column delimiter]:delimiter:' \
      "(-j --live-filter $exargs)"{-j,--live-filter}'[perform instance live filtering]' \
      "(-J --rank $exargs)"{-J+,--rank=}'[set limit for results of valued instances]' \
      "(-2 --overall-rank -3 --overall-rank-alt -N --predicate $exargs)"{-2,--overall-rank}'[perform overall ranking from archive]' \
      "(-3 --overall-rank-alt -2 --overall-rank -N --predicate $exargs)"{-3,--overall-rank-alt}'[perform overall ranking using pmrep format]' \
      "(-6 --sort-metric $exargs)"{-6+,--sort-metric}'[specify sort metric]' \
      "(-8 --limit-filter -9 --limit-filter-force $exargs)"{-8+,--limit-filter=}'[set default limit filter]' \
      "(-9 --limit-filter-force -8 --limit-filter $exargs)"{-9+,--limit-filter-force=}'[set forced limit filter]' \
      "(-n --invert-filter $exargs)"{-n,--invert-filter}'[perform ranking before live filtering]' \
      "(-N --predicate -2 --overall-rank -3 --overall-rank-alt $exargs)"{-N+,--predicate=}'[specify predicate metric]:metric:->metrics' \
      "(-k --extended-csv $exargs)"{-k,--extended-csv}'[write extended CSV]' \
      "(-x --extended-header $exargs)"{-x,--extended-header}'[print extended header]' \
      "(-7 --fixed-header -1 --dynamic-header -4 --names-change $exargs)"{-7,--fixed-header}'[use fixed header with colxrow]' \
      "(-E --repeat-header $exargs)"{-E+,--repeat-header=}'[set header repeat frequency]:samples:' \
      "(-1 --dynamic-header -7 --fixed-header $exargs)"{-1,--dynamic-header}'[update header dynamically]' \
      "(-g --separate-header $exargs)"{-g,--separate-header}'[print separate header]' \
      "(-f --timestamp-format $exargs)"{-f+,--timestamp-format=}'[set time format string]:format:' \
      "(-u --no-interpol --container -d --delay -h --host -L --local-PMDA -K --spec-local -t --interval $exargs)"{-u,--no-interpol}'[disable interpolation]' \
      '*:metric:->metrics' \
      && return 0
  ;;
  pmseries)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-c --config $exargs)"{-c+,--config=}'[specify config file]:file:_files' \
      "(-h --host $exargs)"{-h+,--host=}'[specify Redis host]:host:_hosts' \
      "(-p --port $exargs)"{-p+,--port=}'[specify Redis port]:port:' \
      "(-L --load $exargs)"{-L,--load}'[load metadata and data into Redis]:path:_files' \
      "(-q --query $exargs)"{-q,--query}'[perform timeseries query]' \
      "(-a --all $exargs)"{-a,--all}'[report all metadata, same as -dilms]' \
      "(-d --desc $exargs)"{-d,--desc}'[report metric descriptions]' \
      "(-I --fullindom $exargs)"{-I,--fullindom}'[print verbose InDom]' \
      "(-i --instances $exargs)"{-i,--instances}'[report instance names]' \
      "(-F --fast $exargs)"{-F,--fast}'[request metadata only]' \
      "(-g --glob $exargs)"{-g+,--glob}'[glob pattern to restrict matches]' \
      "(-l --labels $exargs)"{-l,--labels}'[report label values]' \
      "(-M --fullpmid $exargs)"{-M,--fullpmid}'[print verbose PMID]' \
      "(-m --metrics $exargs)"{-m,--metrics}'[report metric names]' \
      "(-n --names $exargs)"{-n,--names}'[report label names]' \
      "(-S --sources $exargs)"{-S,--sources}'[report context for timeseries sources]' \
      "(-s --series $exargs)"{-s,--series}'[report timeseries identifiers]' \
      "(-t --times $exargs)"{-t,--times}'[report numeric timestamps]' \
      "(-v --values $exargs)"{-v,--values}'[report known values for labels]' \
      "(-Z --timezone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      '1:series:->ts_metrics' \
      && return 0
  ;;
  pmstat)
    _arguments -C -S -s \
      "$common_help[@]" \
      "(-A --align $exargs)"{-A+,--align=}'[set initial sample time alignment]:timespec:' \
      "(-h --host -H --hostsfile -L --local-PMDA $exargs)"\*{-a+,--archive=}'[specify metrics source archive]:archive:->archives' \
      "(-g --guimode $exargs)"{-g,--guimode}'[GUI mode with new time control]' \
      "(-a --archive -L --local-PMDA $exargs)"\*{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-O --origin $exargs)"{-O+,--origin=}'[set initial sample time origin]:timespec:' \
      "(-p --guiport $exargs)"{-p+,--guiport=}'[specify existing time control port]:port:' \
      "(-S --start $exargs)"{-S+,--start=}'[set start of time window]:timespec:' \
      "(-s --samples $exargs)"{-s+,--samples=}'[specify number of samples]:samples:' \
      "(-T --finish $exargs)"{-T+,--finish=}'[set end of time window]:timespec:' \
      "(-t --interval $exargs)"{-t+,--interval=}'[specify sampling interval]:interval:' \
      "(-Z --timezone -z --hostzone $exargs)"{-Z+,--timezone=}'[set reporting timezone]:timezone:_time_zone' \
      "(-z --hostzone -Z --timezone $exargs)"{-z,--hostzone}'[use metrics source timezone]' \
      "(-a --archive -L --local-PMDA $exargs)"\*{-H+,--hostsfile=}'[specify metrics source hosts file]:file:_files' \
      "(-L --local-PMDA -a --archive -h --host -H --hostsfile $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-l --suffix $exargs)"{-l,--suffix}'[print hostname suffix]' \
      "(-P --pause -h --host -L --local-pmda $exargs)"{-P,--pause}'[pause between updates in archive mode]' \
      "(-x --xcpu $exargs)"{-x,--xcpu}'[report extended CPU statistics]' \
      && return 0
  ;;
  pmstore)
    _arguments -A "-*" -C -S -s \
      "$common_help[@]" \
      "(-h --host -L --local-PMDA -K --spec-local $exargs)"{-h+,--host=}'[specify metrics source host]:host:_hosts' \
      "(-F --fetch $exargs)"{-F,--fetch}'[perform pmFetch after pmStore to confirm value]' \
      "(-L --local-PMDA -h --host $exargs)"{-L,--local-PMDA}'[use local DSO PMDAs as metrics source]' \
      "(-h --host $exargs)"\*{-K+,--spec-local=}'[define PMDA spec for local DSO PMDAs]:spec:(add del clear)' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(-F --fetch $exargs)"{-F,--fetch}'[perform pmFetch after pmStore to confirm value]' \
      "(-f --force $exargs)"{-f,--force}'[store value even if unset]' \
      "($exargs)"\*{-i+,--insts=}'[specify instances to store to]:instances:->instances' \
      '1:metric:->metrics' \
      && return 0
  ;;
  pmval)
    _arguments -C -S -s \
      "$common_help[@]" \
      "$common_most[@]" \
      "$common_pmda[@]" \
      "(-g --guimode $exargs)"{-g,--guimode}'[GUI mode with new time control]' \
      "(-p --guiport $exargs)"{-p+,--guiport=}'[specify existing time control port]:port:' \
      "(-n --namespace $exargs)"{-n+,--namespace=}'[specify alternative PMNS]:pmnsfile:_files' \
      "(--container -a --archive -U --nointerp $exargs)"--container=+'[specify container to query]:container:->containers' \
      "(--derived $exargs)"--derived=+'[specify derived metrics config]:file:_files' \
      "(-d --delay --container -h --host -L --local-PMDA -K --spec-local $exargs)"{-d,--delay}'[delay between updates in archive mode]' \
      "(-f --precision $exargs)"{-f+,--precision=}'[set fixed output format precision]:precision:' \
      "($exargs)"\*{-i+,--instances=}'[specify instances to report]:instances:->instances' \
      "(-r --raw $exargs)"{-r,--raw}'[report raw values]' \
      "(-a --archive --container -h --host -L --local-PMDA -K --spec-local $exargs)"{-U+,--nointerp=}'[specify uninterpolated metrics source archive]:archive:->archives' \
      "(-v --verbose $exargs)"{-v,--verbose}'[verbose mode]' \
      "(-w --width $exargs)"{-w+,--width=}'[set column width]:width:' \
      "(-x --filter $exargs)"{-x+,--filter=}'[define event filter pattern]:filter:' \
      '1:metric:->metrics' \
      && return 0
  ;;
  esac

  case $state in
  archives)
    local fpat t
    local -a archives
    [[ -n $words[CURRENT] ]] && t=${words[CURRENT]##*,}
    archives=( ${t}*(-FM) ${t}*.meta* )
    if [[ $#archives -gt 0 && $archives == *.meta* ]]; then
      _values -s , -w archive ${archives/.meta*} && ret=0
    else
      compset -P '*,'
      zstyle -s ":completion:${curcontext}:" file-patterns fpat
      if [[ -z "$fpat" ]]; then
        zstyle ":completion:${curcontext}:" file-patterns '%p:globbed-files'
      fi
      _wanted archives expl archive _files -g '*(-F)' && ret=0
    fi
  ;;
  containers)
    local -a conts names found
    conts=( ${(f)"$(pminfo -f containers.state.running)"} )
    names=( ${(f)"$(pminfo -f containers.name)"} )
    for cont in $conts; do
      [[ $cont == *value?1* ]] || continue
      for c in $names; do
        if zstyle -t ":completion:*:${curcontext}:pcp:containers" use-ids; then
          [[ ${${=cont}[4]} == ${${=c}[4]} ]] && found+=( ${(Q)${${=cont}[4]}/\]} )
          [[ ${${=cont}[4]} == ${${=c}[4]} && -n $words[CURRENT] ]] && found+=( ${(Q)${=c}[6]} )
        else
          [[ ${${=cont}[4]} == ${${=c}[4]} ]] && found+=( ${(Q)${=c}[6]} )
          [[ ${${=cont}[4]} == ${${=c}[4]} && -n $words[CURRENT] ]] && found+=( ${(Q)${${=cont}[4]}/\]} )
        fi
      done
    done
    _wanted containers expl container compadd -a found && ret=0
  ;;
  instances)
    local arch=${${~opt_args[-a]:-${~opt_args[--archive]}}/:/,}
    local a=${arch:+-a}
    local cont=${~opt_args[--container]}
    local c=${cont:+--container}
    local host=${~opt_args[-h]:-${~opt_args[--host]}}
    local h=${host:+-h}
    local -a metrics insts found
    local word
    if [[ $arch_req -eq 1 ]]; then
      a=-a
      arch=$words[2]
      for word in ${words:1}; do
        [[ $word != -* && (-f $word.meta || -f $word.meta.xz) ]] && arch=$word
      done
    fi
    for word in ${words:1}; do
      # Find metrics arguments, mishits will be filtered out w/ $2>0
      [[ $word == [a-z]* ]] && metrics+=( $word )
    done
    # TODO: Check if feasible to handle quotes ('") / multiple spaces in insts
    insts=( ${(f)"$(pmprobe $a ${(e)arch} $c ${(e)cont} $h ${(e)host} -I $metrics 2>/dev/null | awk '!/1 PM_IN_NULL$/ && $1~"^[a-z]" && $2>0 {for(i=3;i<=NF;i++)print $i}')"} )
    insts=( ${(q-)${${(ps:" ":)insts}//\"}} )
    # TODO: don't duplicate " when doing pmval -i "<TAB>
    _wanted instances expl instance compadd -Q -P '"' -S '"' -a insts && ret=0
  ;;
  metrics)
    local arch=${${~opt_args[-a]:-${~opt_args[--archive]}}/:/,}
    local a=${arch:+-a}
    local cont=${~opt_args[--container]}
    local c=${cont:+--container}
    local host=${~opt_args[-h]:-${~opt_args[--host]}}
    local h=${host:+-h}
    local word
    if [[ $arch_req -eq 1 ]]; then
      a=-a
      arch=$words[2]
      for word in ${words:1}; do
        [[ $word != -* && (-f $word.meta || -f $word.meta.xz) ]] && arch=$word
      done
    fi
    # Derived metrics from command line
    local drvc=${~opt_args[--derived]}
    local -a drvm
    [[ $service == pminfo ]] && drvc=${drvc:-${~opt_args[-c]}}
    (( ${(k)py_tools[(I)$service]} > 0 )) && drvc=${drvc:-${~opt_args[-e]}}
    if [[ -n $drvc && -f ${(e)drvc} ]]; then
      drvm=( ${=${${(f)"$(<${(e)drvc})"}:#\#*}%%=*} )
    fi
    if [[ -n $drvc && (( ${(k)py_tools[(I)$service]} > 0 )) && ! -f $drvc ]]; then
      drvm=( ${=${(ps:,:)${(Qe)drvc}}%%=*} )
    fi
    if [[ $words[CURRENT] != :* ]]; then
      if zstyle -T ":completion:*:${curcontext}:pcp:metrics" full-names; then
        _wanted metrics expl metric compadd -M 'r:|.=* r:|=*' $drvm \
          $(pminfo -d $a ${(e)arch} $c ${(e)cont} $h ${(e)host} 2>/dev/null | grep -Ev '( |^$|^event)') && ret=0
      else
        local -a res
        res=( $(pminfo -d $a ${(e)arch} $c ${(e)cont} $h ${(e)host} 2>/dev/null | grep -Ev '( |^$|^event)') )
        res+=( $drvm )
        _wanted metrics expl metric _multi_parts -i . res
      fi
    else
      # pmrep(1) style metricset
      (( ${(k)py_tools[(I)$service]} > 0 )) || return 1
      local conf=${~opt_args[-c]:-${~opt_args[--config]}}
      if [[ -z $conf ]]; then
        for f in ./$service.conf $HOME/.$service.conf $HOME/.pcp/$service.conf ${${(M)${(f)"$(</etc/pcp.conf)"}:#PCP_SYSCONF_DIR=*}##PCP_SYSCONF_DIR=}/$service/$service.conf; do
          [[ -f $f ]] && conf=$f && break
        done
      fi
      [[ -z $conf ]] && return 1
      local -a sets
      for s in ${(f)"$(<${(e)conf})"}; do
        [[ $s == \[*\] && $s != \[(global|options)\] ]] && sets+=:${${s/\[}/\]}
      done
      _wanted metricsets expl metricset compadd -a sets && ret=0
    fi
  ;;
  ts_metrics)
    local host=${~opt_args[-h]:-${~opt_args[--host]}}
    local h=${host:+-h}
    local port=${~opt_args[-p]:-${~opt_args[--port]}}
    local p=${port:+-p}
    _wanted ts_metrics expl metric compadd -M 'r:|.=* r:|=*' \
      $(pmseries $h ${(e)host} $p ${(e)port} -m 2>/dev/null) && ret=0
  ;;
  pcp_commands)
    _wanted commands expl 'pcp command' compadd -a _cache_pcp_cmds && ret=0
  ;;
  pcp_cmdopts)
    local cmd word
    for word in ${words:1}; do
      [[ -n "${_cache_pcp_cmds[(r)$word]}" ]] && cmd=$word && break
    done
    [[ -z $cmd ]] && return 1
    [[ $cmd == python ]] && _default && return 0
    if [[ -z $_cache_pcp_cmdopts[$cmd] ]]; then
      _cache_pcp_cmdopts[$cmd]=${${${${(M)${=${(f)"$(pcp $cmd --help 2>&1)"}}:#-[-0-9A-Za-z?]*}/,}/=*}/-USR1}
    fi
    [[ -n $_cache_pcp_cmdopts[$cmd] ]] && _values -w option ${(u)=_cache_pcp_cmdopts[$cmd]} && ret=0
  ;;
  pmfind_mechs)
    if compset -P '*,'; then
      case "$IPREFIX" in
      *avahi,)
        _wanted timeout expl timeout compadd -S = timeout && ret=0
      ;;
      *probe=*)
        local -a opts
        [[ $IPREFIX != *timeout* ]] && opts+=( timeout )
        [[ $IPREFIX != *maxThreads* ]] && opts+=( maxThreads )
        [[ $#opts -eq 0 ]] && ret=0
        [[ $#opts -gt 0 ]] && _wanted options expl option compadd -S = -a opts && ret=0
      ;;
      esac
    else
      _wanted mechanisms expl mechanism compadd -S '' avahi probe= && ret=0
    fi
  ;;
  pmiostat_extopts)
    _values -s , 'extended option' dm t h noidle && ret=0
  ;;
  scales_count)
    _values count-scale \
      "count x 10^-8" "count x 10^-7" "count x 10^-6" "count x 10^-5" \
      "count x 10^-4" "count x 10^-3" "count x 10^-2" "count x 10^-2" \
      "count x 10^-1" "count" "count x 10" "count x 10^2" "count x 10^3" \
      "count x 10^4" "count x 10^5" "count x 10^6" "count x 10^7" && ret=0
  ;;
  scales_space)
    _wanted space-scales expl 'space scale' compadd \
      -M 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' \
      byte Kbyte KB Mbyte MB Gbyte GB Tbyte TB Pbyte PB Ebyte EB Zbyte ZB Ybyte YB && ret=0
  ;;
  scales_time)
    _values time-scale \
      nanosec ns microsec us millisec ms sec s minute min hour hr && ret=0
  ;;
  esac

  return ret
}

_pcp "$@"
