Example code (example.py):
importfireclassFoo:
defbar(self):
return'bar'defdo_stuff(self, arg1):
return'stuff'if__name__=='__main__':
fire.Fire(Foo)
Run:
python3 example.py -- --completion
Completion Output (take note of --self in output):
# bash completion support for example.py# DO NOT EDIT.# This script is autogenerated by fire/completion.py._complete-examplepy()
{
local cur prev opts lastcommand
COMPREPLY=()
prev="${COMP_WORDS[COMP_CWORD-1]}"
cur="${COMP_WORDS[COMP_CWORD]}"
lastcommand=$(get_lastcommand)
opts=""
GLOBAL_OPTIONS=""case"${lastcommand}"in
bar)
if is_prev_global;then
opts="${GLOBAL_OPTIONS}"else
opts="--self ${GLOBAL_OPTIONS}"fi
opts=$(filter_options $opts)
;;
do-stuff)
if is_prev_global;then
opts="${GLOBAL_OPTIONS}"else
opts="--arg1 --self ${GLOBAL_OPTIONS}"fi
opts=$(filter_options $opts)
;;
example.py)
opts="bar do-stuff ${GLOBAL_OPTIONS}"
opts=$(filter_options $opts)
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
get_lastcommand()
{
local lastcommand i
lastcommand=
for((i=0; i <${#COMP_WORDS[@]}; ++i));doif [[ ${COMP_WORDS[i]}!= -* ]] && [[ -n${COMP_WORDS[i]} ]] && [[
${COMP_WORDS[i]}!=$cur ]];then
lastcommand=${COMP_WORDS[i]}fidoneecho$lastcommand
}
filter_options()
{
local opts
opts=""foroptin"$@"doif! option_already_entered $opt;then
opts="$opts$opt"fidoneecho$opts
}
option_already_entered()
{
local opt
foroptin${COMP_WORDS[@]:0:COMP_CWORD}doif [ $1==$opt ];thenreturn 0
fidonereturn 1
}
is_prev_global()
{
local opt
foroptin$GLOBAL_OPTIONSdoif [ $opt==$prev ];thenreturn 0
fidonereturn 1
}
complete -F _complete-examplepy example.py
Example code (example.py):
Run:
python3 example.py -- --completionCompletion Output (take note of
--selfin output):