Compare commits

...

5 Commits

Author SHA1 Message Date
Dean Eigenmann e91acb888e
Merge 5c58f7bf02 into 02326d52c0 2024-11-11 10:22:27 -05:00
Vui Lam 02326d52c0
Fix broken links in active_help.md (#2202)
Small change to fix a couple of broken links in active_help.md

Signed-off-by: Vui Lam <vui.lam@broadcom.com>
2024-11-05 06:19:24 -05:00
Dean Eigenmann 5c58f7bf02
Update command.go 2024-06-20 09:37:19 +02:00
Dean Eigenmann de76854134
Merge branch 'spf13:main' into main 2023-10-13 14:47:58 -05:00
decanus 9b6f7e82fd
implemented OnKillRun 2023-02-16 16:44:14 +01:00
2 changed files with 23 additions and 3 deletions

View File

@ -23,9 +23,11 @@ import (
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"syscall"
flag "github.com/spf13/pflag"
)
@ -141,6 +143,8 @@ type Command struct {
PersistentPostRun func(cmd *Command, args []string)
// PersistentPostRunE: PersistentPostRun but returns an error.
PersistentPostRunE func(cmd *Command, args []string) error
// OnKillRun: run if a commands execution is exited
OnKillRun func(cmd *Command, args []string, os.Signal)
// groups for subcommands
commandgroups []*Group
@ -936,6 +940,22 @@ func (c *Command) execute(a []string) (err error) {
argWoFlags = a
}
if c.OnKillRun != nil {
sigchan := make(chan os.Signal)
signal.Notify(
sigchan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
go func() {
s := <-sigchan
c.OnKillRun(c, argWoFlags, s)
}()
}
if err := c.ValidateArgs(argWoFlags); err != nil {
return err
}

View File

@ -20,12 +20,12 @@ bin/ internal/ scripts/ pkg/ testdata/
## Supported shells
Active Help is currently only supported for the following shells:
- Bash (using [bash completion V2](shell_completions.md#bash-completion-v2) only). Note that bash 4.4 or higher is required for the prompt to appear when an Active Help message is printed.
- Bash (using [bash completion V2](completions/_index.md#bash-completion-v2) only). Note that bash 4.4 or higher is required for the prompt to appear when an Active Help message is printed.
- Zsh
## Adding Active Help messages
As Active Help uses the shell completion system, the implementation of Active Help messages is done by enhancing custom dynamic completions. If you are not familiar with dynamic completions, please refer to [Shell Completions](shell_completions.md).
As Active Help uses the shell completion system, the implementation of Active Help messages is done by enhancing custom dynamic completions. If you are not familiar with dynamic completions, please refer to [Shell Completions](completions/_index.md).
Adding Active Help is done through the use of the `cobra.AppendActiveHelp(...)` function, where the program repeatedly adds Active Help messages to the list of completions. Keep reading for details.
@ -148,7 +148,7 @@ details for your users.
## Debugging Active Help
Debugging your Active Help code is done in the same way as debugging your dynamic completion code, which is with Cobra's hidden `__complete` command. Please refer to [debugging shell completion](shell_completions.md#debugging) for details.
Debugging your Active Help code is done in the same way as debugging your dynamic completion code, which is with Cobra's hidden `__complete` command. Please refer to [debugging shell completion](completions/_index.md#debugging) for details.
When debugging with the `__complete` command, if you want to specify different Active Help configurations, you should use the active help environment variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where any non-ASCII-alphanumeric characters are replaced by an `_`. For example, we can test deactivating some Active Help as shown below: