1
1
# bash completion for nim -*- shell-script -*-
2
2
3
+ # (NOTE) The following functions SHOULD be identical to those
4
+ # in nim source tree: __is_short_or_long, __ask_for_subcmd_or_subopts,
5
+ # __ask_for_subcmd, __ask_for_subcmd_opts
6
+
3
7
__is_short_or_long ()
4
8
{
5
9
local actual short long
@@ -11,82 +15,48 @@ __is_short_or_long()
11
15
return 1
12
16
}
13
17
14
- __ask_for_subcmd ()
18
+ __ask_for_subcmd_or_subopts ()
15
19
{
16
- local args cmd words len ilast ilastlast word_first word_last sub_words sub_len
17
- local i ele
18
- args=(" $@ " )
19
- cmd=" ${args[0]} "
20
- ilast=" ${args[1]} "
21
- words=(" ${args[@]: 2} " )
22
- len=${# words[@]}
23
- ilastlast=$(( ilast - 1 ))
24
- word_first=${words[0]}
25
- word_last=${words[ilast]}
26
- sub_words=(" ${words[@]: 1: ilastlast} " ) # start at 1 hence lastlast
27
- sub_len=${# words[@]}
28
-
29
- # printf "\n[DBUG] word_first:${word_first}|word_last:${word_last}|sub_words(${#sub_words[*]}):${sub_words[*]}\n"
30
-
31
- if [[ $word_first != $cmd || $word_last =~ ^- ]]
32
- then
33
- return 1
34
- fi
20
+ local args cmd subcmd words sub_words word_first word_last word_lastlast
21
+ local len ilast ilastlast i ele sub_len n_nopts
35
22
36
- i=0
37
- while [[ $i -lt $sub_len ]]
38
- do
39
- ele=${sub_words[i]}
40
- if [[ ! -z $ele && ! $ele =~ ^[-:] ]]
41
- then
42
- return 1
43
- fi
44
- if [[ $ele =~ ^: ]]
45
- then
46
- (( i+= 1 ))
47
- fi
48
- (( i+= 1 ))
49
- done
50
-
51
- if [[ $word_last == : ]]
52
- then
53
- return 1
54
- fi
55
-
56
- return 0
57
- }
58
-
59
- __ask_for_subcmd_opts ()
60
- {
61
- local args cmd subcmd words len ilast sub_words sub_len word_first n_nopts
62
- local i ele
63
23
args=(" $@ " )
64
- cmd=" ${args[0]} "
65
- subcmd=" ${args[1]} "
66
- ilast=" ${args[2]} "
67
- words=(" ${args[@]: 3} " )
24
+ ask_for_what=" ${args[0]} "
25
+ cmd=" ${args[1]} "
26
+ subcmd=" ${args[2]} "
27
+ ilast=" ${args[3]} "
28
+ words=(" ${args[@]: 4} " )
68
29
len=${# words[@]}
30
+ ilastlast=$(( ilast - 1 ))
69
31
sub_words=(" ${words[@]: 0: ilast} " )
70
32
sub_len=${# sub_words[@]}
71
33
word_first=${words[0]}
34
+ word_last=${words[ilast]}
35
+ word_lastlast=${words[ilastlast]}
72
36
n_nopts=0
73
37
74
- # printf "\n[DBUG] word_first:${word_first}|words(${len}):${words[*]}|sub_words(${sub_len}):${sub_words[*]}\n"
38
+ # printf "\n[DBUG] word_first:${word_first}|ilast:${ilast}| words(${len}):${words[*]}|sub_words(${sub_len}):${sub_words[*]}\n"
75
39
76
40
if [[ $word_first != $cmd ]]
77
41
then
78
42
return 1
79
43
fi
80
44
81
45
i=0
82
- while [[ $i -lt $sub_len ]]
46
+ while [[ $i -lt $len ]]
83
47
do
84
- ele=${sub_words [i]}
48
+ ele=${words [i]}
85
49
if [[ ! $ele =~ ^- ]]
86
50
then
87
51
if [[ $ele == $cmd || $ele == $subcmd ]]
88
52
then
89
53
(( n_nopts+= 1 ))
54
+ elif [[ $i -eq $ilast && $ele =~ ^[a-zA-Z] ]]
55
+ then
56
+ (( i= i))
57
+ elif [[ -z $ele ]]
58
+ then
59
+ (( i= i))
90
60
elif [[ $ele =~ ^: ]]
91
61
then
92
62
(( i+= 1 ))
@@ -97,12 +67,37 @@ __ask_for_subcmd_opts()
97
67
(( i+= 1 ))
98
68
done
99
69
100
- if [[ n_nopts -ne 2 ]]
101
- then
102
- return 1
103
- fi
70
+ case $ask_for_what in
71
+ 1)
72
+ if [[ n_nopts -eq 1 ]]
73
+ then
74
+ if [[ -z $word_last || $word_last =~ ^[a-zA-Z] ]] && [[ $word_lastlast != : ]]
75
+ then
76
+ return 0
77
+ fi
78
+ fi
79
+ ;;
80
+ 2)
81
+ if [[ n_nopts -eq 2 ]]
82
+ then
83
+ if [[ -z $word_last ]] || [[ $word_last =~ ^[-:] ]]
84
+ then
85
+ return 0
86
+ fi
87
+ fi
88
+ esac
104
89
105
- return 0
90
+ return 1
91
+ }
92
+
93
+ __ask_for_subcmd ()
94
+ {
95
+ __ask_for_subcmd_or_subopts 1 " $@ "
96
+ }
97
+
98
+ __ask_for_subcmd_opts ()
99
+ {
100
+ __ask_for_subcmd_or_subopts 2 " $@ "
106
101
}
107
102
108
103
_nimble ()
@@ -122,7 +117,7 @@ _nimble()
122
117
# printf "\n[DBUG] i_curr:$i_curr|curr:$curr|prev:$prev|words(${#words[*]}):${words[*]}\n"
123
118
124
119
# Asking for a subcommand
125
- if __ask_for_subcmd nimble $i_curr " ${words[@]} "
120
+ if __ask_for_subcmd nimble nimble $i_curr " ${words[@]} "
126
121
then
127
122
subcmds=" "
128
123
subcmds=" ${subcmds} install"
0 commit comments