Skip to content

Instantly share code, notes, and snippets.

@javier-lopez
Created February 18, 2011 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javier-lopez/834476 to your computer and use it in GitHub Desktop.
Save javier-lopez/834476 to your computer and use it in GitHub Desktop.
#basic bash completion for android utilities
have android && {
_android()
{
#When the function or command is invoked, the first argument is the name of
#the command whose arguments are being completed, the second argument is the
#word being completed, and the third argument is the word preceding the word
#being completed on the current command line.
#program_name=$1
#cur=$2
#prev=$3
#$4<---?
#defining local vars
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
prev_prev="${COMP_WORDS[COMP_CWORD-2]}"
#=======================================================
# General options
#=======================================================
COMMANDS="list create move delete update"
#COMMANDS=`android -h | grep '^-' | sed -r 's/: .*//' | awk '{print $2}' | sort | uniq 2> /dev/null`
OPTS="-h --help -v --verbose -s --silent"
#=======================================================
# Nested options <1st layer>
#=======================================================
list_opts="avd target"
create_opts="avd project test-project lib-project"
move_opts="avd"
delete_opts="avd"
update_opts="avd project test-project lib-project adb sdk"
#=======================================================
# Nested options <2nd layer>
#=======================================================
create_avd_opts="-c --sdcard -t --target -n --name -a --snapshot -p\
--path -f -s --skin"
create_project_opts="-n --name -t --target -p --path -k --package -a\
--activity"
create_test-project_opts="-p --path -m --main -n --name"
create_lib-project_opts="-n --name -p --path -t --target -k --package"
move_avd_opts="-p --path -n --name -r --rename"
delete_avd_opts="-n --name"
update_avd_opts="-n --name"
update_project_opts="-p --path -l --library -n --name -t --target -s \
--subprojects"
update_test-project_opts="-m --main -p --path"
update_lib-project_opts="-p --path -t --target"
update_adb_opts=""
update_sdk_opts="--proxy-port -f --force -u --no-ui -o --obsolete \
--proxy-host -t --filter -s --no-https -n --dry-mode"
########################################################
# -W wordlist
# -A action (see bash 'complete' build-in command)
# -G globalpath
# -C command
# -F function
# -X filterpath
# -P preffix
# -S suffix
case "${prev}" in
##1st layer
list)
COMPREPLY=( $( compgen -W "$list_opts" -- $cur ))
return 0
;;
create)
COMPREPLY=( $( compgen -W "$create_opts" -- $cur ))
return 0
;;
move)
COMPREPLY=( $( compgen -W "$move_opts" -- $cur ))
return 0
;;
delete)
COMPREPLY=( $( compgen -W "$delete_opts" -- $cur ))
return 0
;;
update)
COMPREPLY=( $( compgen -W "$update_opts" -- $cur ))
return 0
;;
##2nd layer
#avd)
#case "${prev_prev}" in
#create)
#COMPREPLY=( $( compgen -W "$create_avd_opts" -- $cur ))
#return
#;;
#esac
#;;
esac
case "${cur}" in
-*)
COMPREPLY=( $( compgen -W "$OPTS" -- $cur ))
;;
*)
COMPREPLY=( $( compgen -W "$COMMANDS" -- $cur ))
;;
esac
}
complete -F _android android
}
#_foo()
#{
#}
#complete -F _foo foo
####################################################################################
#complete -o [bashdefault|dirnames|filename|nospace|plusdirs]
#bashdefault
#Perform the rest of the default bash completions if the compspec generates
#no matches. default Use readline's default filename completion if the
#compspec generates no matches.
#dirnames
#Perform directory name completion if the compspec generates no matches.
#filenames
#Tell readline that the compspec generates filenames, so it can perform any
#filename-specific processing (like adding a slash to directory names,
#quoting special characters, or suppressing trailing spaces). Intended to be
#used with shell functions.
#nospace
#Tell readline not to append a space (the default) to words completed at
#the end of the line.
#plusdirs
#After any matches defined by the compspec are generated, directory
#name completion is attempted and any matches are added to the results of
#the other actions.
#$ man bash => "Programmable Completion"
####################################################################################
# vim: set ts=8 sw=4 tw=0 ft=sh :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment