Skip to content

Commit

Permalink
Merge pull request ocaml#453 from chambart/master
Browse files Browse the repository at this point in the history
Update bash completion
  • Loading branch information
samoht committed Feb 12, 2013
2 parents 3e11532 + 971e3e9 commit 5dbc9dc
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions shell/opam_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,82 @@ _opam_add()
_opam_global_options()
{
local res
res="$( opam --help 2>/dev/null | grep '^ -' | sed 's/ *//;s/ .*//' )"
res="$( opam --help=plain 2>/dev/null | grep '^ -' | sed 's/ *//;s/[ ,\[\=].*//' )"
_opam_add "$res"
}

_opam_commands()
{
local res
res="$( opam --help 2>/dev/null | grep '^ [^ ]' | sed 's/ *//;s/ .*//' | grep -v '^-' )"
res="$( opam help topics )"
_opam_add "$res"
}

_opam_flags()
{
local res cmd
cmd="$1"
res="$( opam $cmd --help 2>/dev/null | grep '^ -' | sed 's/ *//;s/ .*//' | grep -v '^--' )"
res="$( opam $cmd --help=plain 2>/dev/null | grep '^ -' | sed 's/ *//;s/[ ,\[\=].*//' )"
_opam_add "$res"
}

_opam_packages()
{
local res
res="$( opam list -short )"
res="$( opam list -s )"
_opam_add "$res"
}

_opam_installed_packages()
{
local res
res="$( opam list -i -s )"
_opam_add "$res"
}

_opam_compilers()
{
local res count
count="$( opam switch -list 2>/dev/null | grep -n '\--- Compilers available' | sed 's/\([:digit:]*\)\:.*/\1/' )"
res="$( opam switch -list 2>/dev/null | tail -n $(($count+1)) | sed 's/^[ ~] *//' )"
local res
res="$( opam switch -s )"
_opam_add "$res"
}

_opam_config_subcommands()
{
_opam_add "env var list subst includes bytecomp asmcomp bytelink asmlink"
}

_opam_config_vars()
{
local res
res="$( opam config -list-vars 2>/dev/null | sed 's/ *//;s/ .*//' )"
res="$( opam config list 2>/dev/null | sed 's/ *//;s/ .*//' )"
_opam_reply="$res"
}

_opam_repository_subcommands()
{
_opam_add "add remove list priority"
}

_opam_repositories()
{
local res
res="$( opam remote -s )"
_opam_add "$res"
}

_opam_repositories_only()
{
_opam_reply="$( opam remote -s )"
}

_opam()
{
local cmd cur prev

COMPREPLY=()
cmd=${COMP_WORDS[1]}
subcmd=${COMP_WORDS[2]}
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
reprev=${COMP_WORDS[COMP_CWORD-2]}
Expand All @@ -68,15 +97,26 @@ _opam()
install|info)
_opam_packages
;;
reinstall|remove)
_opam_installed_packages
;;
switch)
_opam_compilers
;;
config)
if [ "$prev" = "-var" ]; then _opam_config_vars; fi
_opam_config_subcommands
if [ "$prev" = "var" ]; then _opam_config_vars; fi
;;
repository)
_opam_repository_subcommands
case "$subcmd" in
remove)
_opam_repositories_only
;;
esac
;;
remote)
if [ "$prev" = "-add" ]; then return 0; fi
if [ "$reprev" = "-add" ]; then _filedir -d; return 0; fi
update)
_opam_repositories
;;
esac

Expand Down

0 comments on commit 5dbc9dc

Please sign in to comment.