mirror of https://github.com/spf13/cobra.git
Compare commits
5 Commits
025dfb2af6
...
e91acb888e
Author | SHA1 | Date |
---|---|---|
Dean Eigenmann | e91acb888e | |
Vui Lam | 02326d52c0 | |
Dean Eigenmann | 5c58f7bf02 | |
Dean Eigenmann | de76854134 | |
decanus | 9b6f7e82fd |
20
command.go
20
command.go
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in New Issue