Tools: completion: get vehicule type from sim_vehicle help

This commit is contained in:
Pierre Kancir 2019-11-14 17:21:44 +01:00 committed by Peter Barker
parent 989653bf69
commit 1affdbf275
2 changed files with 66 additions and 141 deletions

View File

@ -60,77 +60,35 @@ _sim_vehicle() {
fi fi
done done
# TODO: generate for waf help
case $prev in case $prev in
-v | --vehicle) -v | --vehicle)
opts="ArduCopter AntennaTracker APMrover2 ArduSub ArduPlane" # get the calling program, remove anything after the space == all commands arguments
local caller; caller=$(echo $@ | sed 's/ .*//g');
# get options between "vehicle type " and closing ")", remove line return, remove spaces, anything before the opening (, remove the "|" and closing ")"
opts=$($caller --help | sed -n '/vehicle type (/,/)/p' | tr '\n' ' ' | sed -e 's/ //g' -e 's/.*(//g' -e 's/[|)]/ /g')
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0 return 0
;; ;;
-f | --frame) -f | --frame)
opts="" # get the calling program, remove anything after the space == all commands arguments
if [[ ${COMP_WORDS[@]} == *"ArduCopter"* ]]; then local caller; caller=$(echo $@ | sed 's/ .*//g');
opts+=" +" # Get everything between "frame type" and "-C", remove first and last line, get line starting with caracters and ": " sequence, remove spaces and :
opts+=" X" local supported_vehicle_list=$($caller --help | sed -n '/frame type/,/-C/p' | sed -e '1d' -e '$d' | sed -n 's/\(.*: \).*/\1/p' | sed -e 's/ //g' -e 's/://g')
opts+=" airsim-copter" local frames;
opts+=" coaxcopter" local count=1;
opts+=" djix" for v in $supported_vehicle_list
opts+=" dodeca-hexa" do
opts+=" gazebo-iris" if [[ ${COMP_WORDS[@]} == *"$v"* ]]
opts+=" heli" then
opts+=" heli-compound" # Get everything between "frame type" and "-C", remove first and last line and change starting space by " :", change newlines to space, remove spaces then change vehicle type enclose by ":" by newline and change "|" by space, remove first line, get line "count" that match supported_vehicle_list[count]
opts+=" heli-dual" frames=$($caller --help | sed -n '/frame type/,/-C/p' | sed -e '1d' -e '$d' -e 's/^\s*A/ :A/g' | tr '\n' ' ' | sed -e 's/ //g' -e 's/:\w*:/\n/g' -e 's/[|]/ /g' | sed '1d' | sed "${count}q;d")
opts+=" hexa" break
opts+=" octa"
opts+=" octa-quad"
opts+=" quad"
opts+=" scrimmage-copter"
opts+=" singlecopter"
opts+=" tri"
opts+=" y6"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi fi
if [[ ${COMP_WORDS[@]} == *"AntennaTracker"* ]]; then count=$(( $count + 1 ))
opts+=" tracker" done
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) COMPREPLY=($(compgen -W "${frames}" -- ${cur}))
return 0 return 0
fi
if [[ ${COMP_WORDS[@]} == *"APMrover2"* ]]; then
opts+=" balancebot"
opts+=" gazebo-rover"
opts+=" rover"
opts+=" rover-skid"
opts+=" sailboat"
opts+=" sailboat-motor"
opts+=" balancebot"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
if [[ ${COMP_WORDS[@]} == *"ArduSub"* ]]; then
opts+=" gazebo-bluerov2"
opts+=" vectored"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
if [[ ${COMP_WORDS[@]} == *"ArduPlane"* ]]; then
opts+=" CRRCSim"
opts+=" gazebo-zephyr"
opts+=" jsbsim"
opts+=" plane"
opts+=" plane-dspoilers"
opts+=" plane-elevon"
opts+=" plane-jet"
opts+=" plane-tailsitter"
opts+=" plane-vtailjet"
opts+=" quadplane"
opts+=" quadplane-cl84"
opts+=" quadplane-tilttrivec"
opts+=" quadplane-tri"
opts+=" scrimmage-plane"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
;; ;;
esac esac

View File

@ -3,6 +3,8 @@
local temp_word local temp_word
local cur cword prev local cur cword prev
local -a vehicles
_sim_vehicle() { _sim_vehicle() {
typeset -A opt_args typeset -A opt_args
local context state state_descr line curcontext="$curcontext" local context state state_descr line curcontext="$curcontext"
@ -58,96 +60,61 @@ _sim_vehicle() {
} }
# TODO : generate with regex from sim_vehicle help _get_vehicle_type() {
# get the calling program, remove anything after the space == all commands arguments
local caller; caller=$(echo ${words[@]} | sed 's/ .*//g');
# get options between "vehicle type " and closing ")", remove line return, remove spaces, anything before the opening (, remove the "|" and closing ")"
vehicles=( $($caller --help | sed -n '/vehicle type (/,/)/p' | tr '\n' ' ' | sed -e 's/ //g' -e 's/.*(//g' -e 's/[|)]/ /g') )
}
(( $+functions[_sim_vehicle_vehicles] )) || (( $+functions[_sim_vehicle_vehicles] )) ||
_sim_vehicle_vehicles() { _sim_vehicle_vehicles() {
local vehicles; vehicles=( _get_vehicle_type
'ArduCopter:Copter type' \
'AntennaTracker:Tracker type' \
'APMrover2:Rover type' \
'ArduSub:Sub type' \
'ArduPlane:Plane type'
)
_describe -t vehicles 'vehicle' vehicles "$@" && ret=0 _describe -t vehicles 'vehicle' vehicles "$@" && ret=0
} }
# TODO : generate with regex from sim_vehicle help
(( $+functions[_copter_frames] )) || (( $+functions[_copter_frames] )) ||
_copter_frames() { _copter_frames() {
search_vehicle=true search_vehicle=true
local_current=$CURRENT local_current=$CURRENT
vehicle='' vehicle=''
_get_vehicle_type
# search the vehicle type used with -v on previous arguements
# Todo: handle failure
while ( $search_vehicle || $local_current -eq 0 ) while ( $search_vehicle || $local_current -eq 0 )
do do
case $words[$local_current] in for w in $vehicles
("ArduCopter" | "AntennaTracker" | "APMrover2" | "ArduSub" | "ArduPlane") do
search_vehicle=false if [ "$w" = "$words[$local_current]" ]
then
vehicle=$words[$local_current] vehicle=$words[$local_current]
;; search_vehicle=false
esac break
fi
done;
local_current=$(( $local_current - 1 )) local_current=$(( $local_current - 1 ))
done done
local frames;
case $vehicle in
("ArduCopter")
frames=(
'+:Copter type +' \
'X:Copter type X' \
'airsim-copter:Copter for AirSim' \
'coaxcopter:Copter type Coaxial' \
'djix:Copter type DJI X' \
'dodeca-hexa:Copter type Dodeca-hexa' \
'gazebo-iris:Copter type iris for Gazebo' \
'heli:Heli' \
'heli-compound:Heli with compound' \
'heli-dual:Dual Heli' \
'hexa:Copter type hexa' \
'octa:Copter type octa' \
'octa-quad:Copter type octa-quad' \
'quad:Copter type quad' \
'scrimmage-copter:Copter for scrimmage' \
'singlecopter:Copter type singlecopter' \
'tri:Copter type tri' \
'y6:Copter type y6' \
);; # get the calling program, remove anything after the space == all commands arguments
("APMrover2") local caller; caller=$(echo ${words[@]} | sed 's/ .*//g');
frames=(
'balancebot:Balance Bot' \ # to get the vehicle list :
'gazebo-rover:Rover for Gazebo' \ # Get everything between "frame type" and "-C", remove first and last line, get line starting with caracters and ": " sequence, remove spaces and :
'rover:Rover type ackerman' \ local supported_vehicle_list=($($caller --help | sed -n '/frame type/,/-C/p' | sed -e '1d' -e '$d' | sed -n 's/\(.*: \).*/\1/p' | sed -e 's/ //g' -e 's/://g'))
'rover-skid:Rover type skidsteering' \
'sailboat:Boat type sailboat' \ local frames;
'sailboat-motor:Boat type sailboat with motor' \ local count=1;
'balancebot:Balance Bot' \ for v in $supported_vehicle_list
);; do
("ArduSub") if [ "$v" = "$vehicle" ]
frames=( then
'gazebo-bluerov2:Sub for Gazebo' \ # Get everything between "frame type" and "-C", remove first and last line and change starting space by " :", change newlines to space, remove spaces then change vehicle type enclose by ":" by newline and change "|" by space, remove first line, get line "count" that match supported_vehicle_list[count]
'vectored:Sub vectored' \ frames=($($caller --help | sed -n '/frame type/,/-C/p' | sed -e '1d' -e '$d' -e 's/^\s*A/ :A/g' | tr '\n' ' ' | sed -e 's/ //g' -e 's/:\w*:/\n/g' -e 's/[|]/ /g' | sed '1d' | sed "${count}q;d"))
);; break
("AntennaTracker") fi
frames=( count=$(( $count + 1 ))
'tracker:Antenna Tracker' \ done
);;
("ArduPlane")
frames=(
'CRRCSim:Plane for CRRCSim' \
'gazebo-zephyr:Plane for Gazebo' \
'jsbsim:Plane for jsbsim' \
'plane:Plane type plane' \
'plane-dspoilers:Plane with dspoilers' \
'plane-elevon:Plane with elevon' \
'plane-jet:Plane type jet' \
'plane-tailsitter:Plane type tailsitter' \
'plane-vtailjet:Plane type vtail' \
'quadplane:Plane type quadplane' \
'quadplane-cl84:Plane type quadplane-cl84' \
'quadplane-tilttrivec:Plane type quadplane-tilttrivec' \
'quadplane-tri:Plane type quadplane-tri' \
'scrimmage-plane:Plane for scrimmage' \
);;
esac
_describe -t frames 'frame' frames "$@" && ret=0 _describe -t frames 'frame' frames "$@" && ret=0
} }