goctl completion

概述

goctl 采用了 Cobra 框架开发,因此其继承了 Cobra 的自动补全功能,在 goctl 中,我们提供了 bashfishzshpowershell 的自动补全功能。

goctl completion 指令

  1. $ goctl completion --help
  2. Generate the autocompletion script for goctl for the specified shell.
  3. See each sub-command's help for details on how to use the generated script.
  4. Usage:
  5. goctl completion [command]
  6. Available Commands:
  7. bash Generate the autocompletion script for bash
  8. fish Generate the autocompletion script for fish
  9. powershell Generate the autocompletion script for powershell
  10. zsh Generate the autocompletion script for zsh
  11. Flags:
  12. -h, --help help for completion
  13. Use "goctl completion [command] --help" for more information about a command.

目前 goctl 支持 bashfishzshpowershell 的自动补全功能,如果你是 Windows 操作系统且使用的是 powershell,你可以选择 goctl completion powershell 来生成脚本,如果你是 Linux 或者 Mac 操作系统,你可以选择除 goctl completion powershell 外的另外 3个来生成脚本,但要根据你当前的 shell 来选择。

goctl completion - 图1查看当前 shell

在 Linux 或者 Mac 操作系统中,可以通过 echo $SHELL 来查看当前 shell,如果是 zsh,则选择 goctl completion zsh 来生成脚本,如果是 bash,则选择 goctl completion bash 来生成脚本,如果是 fish,则选择 goctl completion fish 来生成脚本。

goctl completion bash 指令

  1. $ goctl completion bash --help
  2. Generate the autocompletion script for the bash shell.
  3. This script depends on the 'bash-completion' package.
  4. If it is not installed already, you can install it via your OS's package manager.
  5. To load completions in your current shell session:
  6. source <(goctl completion bash)
  7. To load completions for every new session, execute once:
  8. #### Linux:
  9. goctl completion bash > /etc/bash_completion.d/goctl
  10. #### macOS:
  11. goctl completion bash > $(brew --prefix)/etc/bash_completion.d/goctl
  12. You will need to start a new shell for this setup to take effect.
  13. Usage:
  14. goctl completion bash
  15. Flags:
  16. -h, --help help for bash
  17. --no-descriptions disable completion descriptions

在 Linux 或者 Mac 操作系统中,如果你使用的是 bash,你可以通过 goctl completion bash 来生成脚本,需要注意的是,goctl completion bash 依赖 bash-completion 包的支持,如果你的操作系统没有安装此包的话,建议自行安装一下。

  1. 临时生效

    临时生效仅对当前终端 session 有用,是一次性的。

    1. $ source <(goctl completion bash)
  2. 永久生效

永久生效设置按照操作系统的不同,其设置方法也不同,设置后需要重新启动终端才生效,目前针对 Linux 和 MacOS 的设置方法如下:

  • Linux
  • MacOS
  1. $ goctl completion bash > /etc/bash_completion.d/goctl
  1. $ goctl completion bash > $(brew --prefix)/etc/bash_completion.d/goctl

goctl completion fish 指令

  1. $ goctl completion fish --help
  2. Generate the autocompletion script for the fish shell.
  3. To load completions in your current shell session:
  4. goctl completion fish | source
  5. To load completions for every new session, execute once:
  6. goctl completion fish > ~/.config/fish/completions/goctl.fish
  7. You will need to start a new shell for this setup to take effect.
  8. Usage:
  9. goctl completion fish [flags]
  10. Flags:
  11. -h, --help help for fish
  12. --no-descriptions disable completion descriptions
  1. 临时生效

    临时生效仅对当前终端 session 有用,是一次性的。

    1. $ goctl completion fish | source
  2. 永久生效

  1. $ goctl completion fish > ~/.config/fish/completions/goctl.fish

goctl completion powershell 指令

  1. $ goctl completion powershell --help
  2. Generate the autocompletion script for powershell.
  3. To load completions in your current shell session:
  4. goctl completion powershell | Out-String | Invoke-Expression
  5. To load completions for every new session, add the output of the above command
  6. to your powershell profile.
  7. Usage:
  8. goctl completion powershell [flags]
  9. Flags:
  10. -h, --help help for powershell
  11. --no-descriptions disable completion descriptions
  1. 临时生效

    临时生效仅对当前终端 session 有用,是一次性的。

    1. $ goctl completion powershell | Out-String | Invoke-Expression
  2. 永久生效

需要将如下输出添加到 powershell 的配置文件中。

  1. $ goctl completion powershell | Out-String | Invoke-Expression

goctl completion zsh 指令

  1. $ goctl completion zsh --help
  2. Generate the autocompletion script for the zsh shell.
  3. If shell completion is not already enabled in your environment you will need
  4. to enable it. You can execute the following once:
  5. echo "autoload -U compinit; compinit" >> ~/.zshrc
  6. To load completions in your current shell session:
  7. source <(goctl completion zsh); compdef _goctl goctl
  8. To load completions for every new session, execute once:
  9. #### Linux:
  10. goctl completion zsh > "${fpath[1]}/_goctl"
  11. #### macOS:
  12. goctl completion zsh > $(brew --prefix)/share/zsh/site-functions/_goctl
  13. You will need to start a new shell for this setup to take effect.
  14. Usage:
  15. goctl completion zsh [flags]
  16. Flags:
  17. -h, --help help for zsh
  18. --no-descriptions disable completion descriptions
goctl completion - 图2温馨提示

如果你使用的是 zsh,且 shell completion 没有启用,那么你可以通过如下指令开启:

  1. echo "autoload -U compinit; compinit" >> ~/.zshrc
  1. 临时生效

    临时生效仅对当前终端 session 有用,是一次性的。

    1. $ source <(goctl completion zsh); compdef _goctl goctl
  2. 永久生效

永久生效设置按照操作系统的不同,其设置方法也不同,设置后需要重新启动终端才生效,目前针对 Linux 和 MacOS 的设置方法如下:

  • Linux
  • MacOS
  1. $ goctl completion zsh > "${fpath[1]}/_goctl"
  1. $ goctl completion zsh > $(brew --prefix)/share/zsh/site-functions/_goctl