|
1 // Copyright 2013-2022 The Cobra Authors |
|
2 // |
|
3 // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 // you may not use this file except in compliance with the License. |
|
5 // You may obtain a copy of the License at |
|
6 // |
|
7 // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 // |
|
9 // Unless required by applicable law or agreed to in writing, software |
|
10 // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 // See the License for the specific language governing permissions and |
|
13 // limitations under the License. |
|
14 |
1 // The generated scripts require PowerShell v5.0+ (which comes Windows 10, but |
15 // The generated scripts require PowerShell v5.0+ (which comes Windows 10, but |
2 // can be downloaded separately for windows 7 or 8.1). |
16 // can be downloaded separately for windows 7 or 8.1). |
3 |
17 |
4 package cobra |
18 package cobra |
5 |
19 |
6 import ( |
20 import ( |
7 "bytes" |
21 "bytes" |
8 "fmt" |
22 "fmt" |
9 "io" |
23 "io" |
10 "os" |
24 "os" |
|
25 "strings" |
11 ) |
26 ) |
12 |
27 |
13 func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) { |
28 func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) { |
|
29 // Variables should not contain a '-' or ':' character |
|
30 nameForVar := name |
|
31 nameForVar = strings.Replace(nameForVar, "-", "_", -1) |
|
32 nameForVar = strings.Replace(nameForVar, ":", "_", -1) |
|
33 |
14 compCmd := ShellCompRequestCmd |
34 compCmd := ShellCompRequestCmd |
15 if !includeDesc { |
35 if !includeDesc { |
16 compCmd = ShellCompNoDescRequestCmd |
36 compCmd = ShellCompNoDescRequestCmd |
17 } |
37 } |
18 WriteStringAndCheck(buf, fmt.Sprintf(`# powershell completion for %-36[1]s -*- shell-script -*- |
38 WriteStringAndCheck(buf, fmt.Sprintf(`# powershell completion for %-36[1]s -*- shell-script -*- |
50 if ($Command.Length -gt $CursorPosition) { |
70 if ($Command.Length -gt $CursorPosition) { |
51 $Command=$Command.Substring(0,$CursorPosition) |
71 $Command=$Command.Substring(0,$CursorPosition) |
52 } |
72 } |
53 __%[1]s_debug "Truncated command: $Command" |
73 __%[1]s_debug "Truncated command: $Command" |
54 |
74 |
55 $ShellCompDirectiveError=%[3]d |
75 $ShellCompDirectiveError=%[4]d |
56 $ShellCompDirectiveNoSpace=%[4]d |
76 $ShellCompDirectiveNoSpace=%[5]d |
57 $ShellCompDirectiveNoFileComp=%[5]d |
77 $ShellCompDirectiveNoFileComp=%[6]d |
58 $ShellCompDirectiveFilterFileExt=%[6]d |
78 $ShellCompDirectiveFilterFileExt=%[7]d |
59 $ShellCompDirectiveFilterDirs=%[7]d |
79 $ShellCompDirectiveFilterDirs=%[8]d |
60 |
80 |
61 # Prepare the command to request completions for the program. |
81 # Prepare the command to request completions for the program. |
62 # Split the command at the first space to separate the program and arguments. |
82 # Split the command at the first space to separate the program and arguments. |
63 $Program,$Arguments = $Command.Split(" ",2) |
83 $Program,$Arguments = $Command.Split(" ",2) |
64 |
84 |
65 $RequestComp="$Program %[2]s $Arguments" |
85 $RequestComp="$Program %[3]s $Arguments" |
66 __%[1]s_debug "RequestComp: $RequestComp" |
86 __%[1]s_debug "RequestComp: $RequestComp" |
67 |
87 |
68 # we cannot use $WordToComplete because it |
88 # we cannot use $WordToComplete because it |
69 # has the wrong values if the cursor was moved |
89 # has the wrong values if the cursor was moved |
70 # so use the last argument |
90 # so use the last argument |
90 `+" $RequestComp=\"$RequestComp\" + ' `\"`\"'"+` |
110 `+" $RequestComp=\"$RequestComp\" + ' `\"`\"'"+` |
91 } |
111 } |
92 |
112 |
93 __%[1]s_debug "Calling $RequestComp" |
113 __%[1]s_debug "Calling $RequestComp" |
94 # First disable ActiveHelp which is not supported for Powershell |
114 # First disable ActiveHelp which is not supported for Powershell |
95 $env:%[8]s=0 |
115 $env:%[9]s=0 |
96 |
116 |
97 #call the command store the output in $out and redirect stderr and stdout to null |
117 #call the command store the output in $out and redirect stderr and stdout to null |
98 # $Out is an array contains each line per element |
118 # $Out is an array contains each line per element |
99 Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null |
119 Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null |
100 |
120 |
241 } |
261 } |
242 } |
262 } |
243 |
263 |
244 } |
264 } |
245 } |
265 } |
246 `, name, compCmd, |
266 |
|
267 Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock |
|
268 `, name, nameForVar, compCmd, |
247 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, |
269 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, |
248 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) |
270 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) |
249 } |
271 } |
250 |
272 |
251 func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error { |
273 func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error { |