@@ -23,68 +23,40 @@ _@go.set_command_path_and_argv() {
2323 return 1
2424 fi
2525
26- local cmd_args=(" $@ " )
27- local cmd_name=" ${cmd_args[0]} "
28- local cmd_paths=()
26+ local path_suffix
2927 local try_path
3028
31- unset ' cmd_args[0]'
32-
33- for try_path in " ${_GO_SEARCH_PATHS[@]} " ; do
34- try_path=" $try_path /$cmd_name "
35-
36- if [[ -f " $try_path " && -x " $try_path " ]]; then
37- cmd_paths+=(" $try_path " )
38- fi
39- done
40-
41- if [[ " ${# cmd_paths[*]} " -eq 0 ]]; then
42- printf " Unknown command: ${cmd_name} \n\n" >&2
43- _@go.list_available_commands " ${_GO_SEARCH_PATHS[@]} " >&2
44- return 1
45- fi
46-
47- local -i cmd_arg_index=1
48- cmd_name=(" $cmd_name " )
49-
50- local -i i=0
51- local -i longest_path=()
5229 __go_cmd_name=()
53- for try_path in " ${cmd_paths[@]} " ; do
54- for arg in " ${cmd_args[@]} " ; do
55- # This is most likely to happen during argument completion.
56- if [[ -z " $arg " ]]; then
57- break
30+ __go_argv=()
31+ __go_cmd_path=()
32+
33+ for (( i = "$# "; i > 0 ; i-- )) ; do
34+ for try_path in " ${_GO_SEARCH_PATHS[@]} " ; do
35+ path_suffix=" $( printf ' %s.d/' " ${@: 1: i} " ) "
36+ path_suffix=" ${path_suffix% .d/ } "
37+ try_path=" $try_path /$path_suffix "
38+
39+ if [[ -f " $try_path " && -x " $try_path " ]]; then
40+ __go_cmd_path+=(" $try_path " )
41+ if [[ " ${# __go_argv[@]} " -eq 0 ]]; then
42+ __go_cmd_name=(" ${@: 1: i} " )
43+ __go_argv=(" ${@: i+1} " )
44+ fi
5845 fi
59-
60- try_path=" ${cmd_paths[$i]} .d/$arg "
61-
62- if [[ ! -e " $try_path " ]]; then
63- break
64- elif [[ ! (-f " $try_path " && -x " $try_path " ) ]]; then
65- break
66- fi
67-
68- cmd_paths[$i ]=" $try_path "
69-
70- cmd_name+=(" $arg " )
71- cmd_arg_index=cmd_arg_index+1
7246 done
7347
74- if [[ " ${# cmd_name[@]} " -gt " ${# __go_cmd_name[@]} " ]]; then
75- longest_path=" $i "
76- __go_cmd_name=(" ${cmd_name[@]} " )
77- __go_argv=(" ${cmd_args[@]: cmd_arg_index} " )
48+ if [[ " ${# __go_cmd_path[@]} " -ne 0 ]]; then
49+ break
7850 fi
79-
80- i=i+1
8151 done
8252
8353 # The command that is the most nested one takes precedence. Eg
8454 # `scripts/foobar/aaa/bbb/ccc arg1 arg2` takes precedence over
8555 # `scripts/foobar/aaa bbb ccc arg1 arg2`.
86- __go_cmd_path=(' ' " ${cmd_paths[@]} " )
87- __go_cmd_path[0]=" ${cmd_paths[longest_path]} "
88- unset " __go_cmd_path[longest_path+1]"
89- __go_cmd_path=(" ${__go_cmd_path[@]} " )
56+
57+ if [[ " ${# __go_cmd_name[*]} " -eq 0 ]]; then
58+ printf " Unknown command: $1 \n\n" >&2
59+ _@go.list_available_commands " ${_GO_SEARCH_PATHS[@]} " >&2
60+ return 1
61+ fi
9062}
0 commit comments