#compdef fennel

autoload -U is-at-least

_fennel() {
  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
  local common=(
    '--repl[Command to launch an interactive REPL session]' \
    '-c[Command to AOT compile files, writing Lua to stdout]:FILES:' \
    '--compile[Command to AOT compile files, writing Lua to stdout]:FILES:' \
    '-e[Command to evaluate source code and print result]:SOURCE:' \
    '--eval[Command to evaluate source code and print result]:SOURCE:' \
    '--correlate[Make Lua output line numbers try to match Fennels]' \
    '-l[Load the specified FILE before executing command]:FILE:' \
    '--load[Load the specified FILE before executing command]:FILE:' \
    '--no-compiler-sandbox[Dont limit compiler environment to minimal sandbox]' \
    '--compile-binary[Compile FILE to standalone binary OUT]:FILE:' \
    '--add-package-path[Add PATH to package.path for finding Lua modules]:PATH:' \
    '--add-package-cpath[Add PATH to package.cpath for finding Lua modules]:PATH:' \
    '--add-fennel-path[Add PATH to fennel.path for finding Fennel modules]:PATH:' \
    '--add-macro-path[Add PATH to fennel.macro-path for macro modules]:PATH:' \
    '--globals[Allow these globals in addition to standard ones]:G1:G2:' \
    '--globals-only[Same as above, but exclude standard ones]:G1:G2:' \
    '--assert-as-repl[Replace assert calls with assert-repl]' \
    '--require-as-include[Inline required modules in the output]' \
    '--to-be-closed[Use to-be-closed vars in with-open (Lua 5.4+ only)]' \
    '--lambda-as-fn[Replace lambda function definitions with fn]' \
    '--skip-include[Omit certain modules from output when included]:M1:M2:' \
    '--use-bit-lib[Use LuaJITs bit library instead of operators]:' \
    '--metadata[Enable function metadata, even in compiled output]' \
    '--no-metadata[Disable function metadata, even in REPL]' \
    '--lua[Run in a child process with LUA_EXE]:LUA_EXE:' \
    '--plugin[Activate the compiler plugin in FILE]:FILE:' \
    '--raw-errors[Disable friendly compile error reporting]' \
    '--no-searcher[Skip installing package.searchers entry]' \
    '--no-fennelrc[Skip loading ~/.fennelrc when launching REPL]' \
    '--keywords[Treat these symbols as reserved Lua keywords]:K1:K2:' \
    '-h[Display this text]' \
    '--help[Display this text]' \
    '-v[Show version]' \
    '--version[Show version]' \
  )

  _arguments "${_arguments_options[@]}" $common \
    && ret=0
  case $state in
    (compile-binary)
      _arguments \
        '-h[Display further help for compiling binaries]' \
        '--help[Display further help for compiling binaries]' \
      && ret=0
      ;;
  esac
  return ret
}

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

if [ "$funcstack[1]" = "_fennel" ]; then
  _fennel "$@"
else
  compdef _fennel fennel
fi
