Welcome! Please see the About page for a little more info on how this works.

+4 votes
in Clojure CLI by
edited by

It would be nice if I didn't have to remember all the tools I have installed and their options. They could be auto completed on command line.

  1. I would like to have available tools listed when typing: clojure -T<tab><tab>
  2. I would like to have specific tool functions listed when typing: clojure -Ttools <tab><tab>
  3. I would like to have possible keys listed for given tool when typing clojure -Ttools show <tab><tab>

I was playing with bash completion a bit and sort of managed to do first 2:

$ clojure -T<tab><tab>
-Tantq      -Tclj-new   -Toutdated  -Ttools

$ clojure -Ttools <tab><tab>
install         install-latest  list            remove          show

using following script:

  #!/usr/bin/env bash

_clojure() {
  if [[ "${COMP_CWORD}" -eq 1 ]]; then
    local tools=($(clojure -Ttools list | tail --lines +2 | awk 'BEGIN {ORS=" "}{print $1}'))
    local options=()
    for tool in "${tools[@]}"; do
      options+=("-T${tool}")
    done
    COMPREPLY=($(compgen -W "${options[*]}" -- "${COMP_WORDS[COMP_CWORD]}"))
  elif [[ "${COMP_CWORD}" -eq 2 ]]; then
    local aliases=($(clojure -A:deps "${COMP_WORDS[1]}" help/dir))
    COMPREPLY=($(compgen -W "${aliases[*]}" -- "${COMP_WORDS[COMP_CWORD]}"))
  fi
}

complete -F _clojure clojure

I'm not saying that clojure necessarily needs to come with some completion script (although that would be nice), but It could come with features allowing to write such script.

It could e.g. read env variable GEN_COMPLETION that if set to 1 - clojure would print list of completions so one could request list of tools like this:

$ GEN_COMPLETION=1 clojure -T
-Tantq      -Tclj-new   -Toutdated  -Ttools

I believe there might be an issue knowing if "-T" is an argument or part of the argument, so there could be another env GEN_COMPLETION_CURRENT_ARG=0 indicating that -T is part we're completing (akin to COMP_CWORD in bash) which would be set to empty string or not present if we want to complete next argument in the command line.

As for completion for specific tools, they could provide some completion function that would be called when completing options of a specific tool.

1 Answer

+1 vote
by
selected by
 
Best answer

Would love to have completion support but it may require some planning wrt performance as it will likely require edn parsing, which implies Clojure, which implies a JVM.

...