vendor/github.com/spf13/cobra/powershell_completions.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
child 265 05c40b36d3b2
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    48     # Make sure the $Command is longer then the $CursorPosition before we truncate.
    48     # Make sure the $Command is longer then the $CursorPosition before we truncate.
    49     # This happens because the $Command does not include the last space.
    49     # This happens because the $Command does not include the last space.
    50     if ($Command.Length -gt $CursorPosition) {
    50     if ($Command.Length -gt $CursorPosition) {
    51         $Command=$Command.Substring(0,$CursorPosition)
    51         $Command=$Command.Substring(0,$CursorPosition)
    52     }
    52     }
    53 	__%[1]s_debug "Truncated command: $Command"
    53     __%[1]s_debug "Truncated command: $Command"
    54 
    54 
    55     $ShellCompDirectiveError=%[3]d
    55     $ShellCompDirectiveError=%[3]d
    56     $ShellCompDirectiveNoSpace=%[4]d
    56     $ShellCompDirectiveNoSpace=%[4]d
    57     $ShellCompDirectiveNoFileComp=%[5]d
    57     $ShellCompDirectiveNoFileComp=%[5]d
    58     $ShellCompDirectiveFilterFileExt=%[6]d
    58     $ShellCompDirectiveFilterFileExt=%[6]d
    59     $ShellCompDirectiveFilterDirs=%[7]d
    59     $ShellCompDirectiveFilterDirs=%[7]d
    60 
    60 
    61 	# Prepare the command to request completions for the program.
    61     # Prepare the command to request completions for the program.
    62     # Split the command at the first space to separate the program and arguments.
    62     # Split the command at the first space to separate the program and arguments.
    63     $Program,$Arguments = $Command.Split(" ",2)
    63     $Program,$Arguments = $Command.Split(" ",2)
       
    64 
    64     $RequestComp="$Program %[2]s $Arguments"
    65     $RequestComp="$Program %[2]s $Arguments"
    65     __%[1]s_debug "RequestComp: $RequestComp"
    66     __%[1]s_debug "RequestComp: $RequestComp"
    66 
    67 
    67     # we cannot use $WordToComplete because it
    68     # we cannot use $WordToComplete because it
    68     # has the wrong values if the cursor was moved
    69     # has the wrong values if the cursor was moved
    88 `+"        # We need to use `\"`\" to pass an empty argument a \"\" or '' does not work!!!"+`
    89 `+"        # We need to use `\"`\" to pass an empty argument a \"\" or '' does not work!!!"+`
    89 `+"        $RequestComp=\"$RequestComp\" + ' `\"`\"'"+`
    90 `+"        $RequestComp=\"$RequestComp\" + ' `\"`\"'"+`
    90     }
    91     }
    91 
    92 
    92     __%[1]s_debug "Calling $RequestComp"
    93     __%[1]s_debug "Calling $RequestComp"
       
    94     # First disable ActiveHelp which is not supported for Powershell
       
    95     $env:%[8]s=0
       
    96 
    93     #call the command store the output in $out and redirect stderr and stdout to null
    97     #call the command store the output in $out and redirect stderr and stdout to null
    94     # $Out is an array contains each line per element
    98     # $Out is an array contains each line per element
    95     Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null
    99     Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null
    96 
       
    97 
   100 
    98     # get directive from last line
   101     # get directive from last line
    99     [int]$Directive = $Out[-1].TrimStart(':')
   102     [int]$Directive = $Out[-1].TrimStart(':')
   100     if ($Directive -eq "") {
   103     if ($Directive -eq "") {
   101         # There is no directive specified
   104         # There is no directive specified
   231 
   234 
   232             # TabCompleteNext and in case we get something unknown
   235             # TabCompleteNext and in case we get something unknown
   233             Default {
   236             Default {
   234                 # Like MenuComplete but we don't want to add a space here because
   237                 # Like MenuComplete but we don't want to add a space here because
   235                 # the user need to press space anyway to get the completion.
   238                 # the user need to press space anyway to get the completion.
   236                 # Description will not be shown because thats not possible with TabCompleteNext
   239                 # Description will not be shown because that's not possible with TabCompleteNext
   237                 [System.Management.Automation.CompletionResult]::new($($comp.Name | __%[1]s_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
   240                 [System.Management.Automation.CompletionResult]::new($($comp.Name | __%[1]s_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
   238             }
   241             }
   239         }
   242         }
   240 
   243 
   241     }
   244     }
   242 }
   245 }
   243 `, name, compCmd,
   246 `, name, compCmd,
   244 		ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
   247 		ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
   245 		ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))
   248 		ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))
   246 }
   249 }
   247 
   250 
   248 func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error {
   251 func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error {
   249 	buf := new(bytes.Buffer)
   252 	buf := new(bytes.Buffer)
   250 	genPowerShellComp(buf, c.Name(), includeDesc)
   253 	genPowerShellComp(buf, c.Name(), includeDesc)