Cobra指挥官:如何从另一个命令调用一个命令?

huangapple go评论141阅读模式
英文:

Cobra Commander: How to call a command from another command?

问题

在 cobra 中,我创建了一个命令的命令:

  1. myapp zip -directory "xzy" -output="zipname"
  2. myapp upload -filename="abc"

我想创建一个 zipAndUpload 命令,并重用现有的命令,即:

  1. myapp zipup -directory "xzy" -output="zipname"

在这里,zipup 首先调用 "zip" 命令,然后使用 "zip" 命令的输出名称作为 "upload" 命令的 "filename" 标志。

如何在不重复大量代码的情况下执行这个操作?

英文:

In cobra I've create a command of commands:

  1. myapp zip -directory "xzy" -output="zipname"
  2. myapp upload -filename="abc"

I want to make a zipAndUpload command and reuse the existing commands, i.e.,

  1. myapp zipup -directory "xzy" -outout="zipname"

Here the zipup would first call the "zip" command and then use the output name from the "zip" command as the "filename" flag for the "upload" command.

How can I execute this without a lot of code duplication?

答案1

得分: 4

"shared"开关变为全局。

子命令的"run"部分转换为函数。

为此,您必须手动定义命令:

  1. var (
  2. cfgDirectory string
  3. cfgFilename string
  4. cfgOutput string
  5. )
  6. var rootCmd = &cobra.Command{
  7. Use: "root",
  8. Short: "root",
  9. Long: "root",
  10. Run: func(cmd *cobra.Command, args []string) {
  11. // something
  12. },
  13. }
  14. var uploadCmd = &cobra.Command{
  15. Use: "upload",
  16. Short: "upload",
  17. Long: "upload",
  18. Run: func(cmd *cobra.Command, args []string) {
  19. Upload()
  20. },
  21. }
  22. var zipCmd = &cobra.Command{
  23. Use: "zip",
  24. Short: "zip",
  25. Long: "zip",
  26. Run: func(cmd *cobra.Command, args []string) {
  27. Zip()
  28. },
  29. }
  30. var zipupCmd = &cobra.Command{
  31. Use: "zipup",
  32. Short: "zipup",
  33. Long: "zipup",
  34. Run: func(cmd *cobra.Command, args []string) {
  35. Zip()
  36. Upload()
  37. },
  38. }
  39. func setFlags() {
  40. rootCmd.PersistentFlags().StringVar(&cfgDirectory, "directory", "", "explanation")
  41. rootCmd.PersistentFlags().StringVar(&cfgFilename, "filename", "", "explanation")
  42. rootCmd.PersistentFlags().StringVar(&cfgOutput, "output", "", "explanation")
  43. }
  44. func Upload() {
  45. // you know what to do
  46. }
  47. func Zip() {
  48. // you know what to do
  49. }
  50. ...
  51. // 添加子命令
  52. rootCmd.AddCommand(zipCmd)
  53. rootCmd.AddCommand(uploadCmd)
  54. rootCmd.AddCommand(zipupCmd)

希望这可以帮到您,这是我在没有任何示例代码的情况下能做到的最好的翻译。

英文:

The "shared" switches make as global.

The "run" sections of the sub-commands, convert to functions.

To do this you must define the commands manually:

  1. var (
  2. cfgDirectory string
  3. cfgFilename string
  4. cfgOutput string
  5. )
  6. var rootCmd = &cobra.Command{
  7. Use: "root",
  8. Short: "root",
  9. Long: "root",
  10. Run: func(cmd *cobra.Command, args []string) {
  11. // something
  12. },
  13. }
  14. var uploadCmd = &cobra.Command{
  15. Use: 'upload',
  16. Short: 'upload',
  17. Long: `upload`,
  18. Run: func(cmd *cobra.Command, args []string) {
  19. Upload()
  20. },
  21. }
  22. var zipCmd = &cobra.Command{
  23. Use: "zip",
  24. Short: "zip",
  25. Long: "zip",
  26. Run: func(cmd *cobra.Command, args []string) {
  27. Zip()
  28. },
  29. }
  30. var zipupCmd = &cobra.Command{
  31. Use: "zipup",
  32. Short: "zipup",
  33. Long: "zipup",
  34. Run: func(cmd *cobra.Command, args []string) {
  35. Zip()
  36. Upload()
  37. },
  38. }
  39. func setFlags() {
  40. rootCmd.PersistentFlags().StringVar(&cfgDirectory, "directory", "", "explanation")
  41. rootCmd.PersistentFlags().StringVar(&cfgFilename, "filename", "", "explanation")
  42. rootCmd.PersistentFlags().StringVar(&cfgOutput, "output", "", "explanation")
  43. }
  44. func Upload() {
  45. // you know what to do
  46. }
  47. func Zip() {
  48. // you know what to do
  49. }
  50. ...
  51. // Add subcommands
  52. rootCmd.AddCommand(zipCmd)
  53. rootCmd.AddCommand(uploadCmd)
  54. rootCmd.AddCommand(zipupCmd)

Hope this helps, this is the best I could do without any example code.

答案2

得分: 2

当您从cobra CLI创建新命令时,通常会将它们放入单独的文件中。如果您想从不同的命令文件中运行命令,代码会类似于这样:

  1. package cmd
  2. import "github.com/spf13/cobra"
  3. // DirectoryFlag 指定目录
  4. var DirectoryFlag string
  5. // OutputFlag 指定输出
  6. var OutputFlag string
  7. // zipupCmd 表示"zipup"命令
  8. var zipupCmd = &cobra.Command{
  9. Use: "zipup",
  10. Short: "Zip & Upload",
  11. Long: `Zip & Upload`,
  12. Run: func(cmd *cobra.Command, args []string) {
  13. anyArgs := "whatever"
  14. zipCmd.Run(cmd, []string{anyArgs})
  15. uploadCmd.Run(cmd, []string{anyArgs})
  16. },
  17. }
  18. func init() {
  19. rootCmd.AddCommand(zipupCmd)
  20. zipupCmd.Flags().StringVarP(&DirectoryFlag, "directory", "d", "", "设置目录")
  21. zipupCmd.Flags().StringVarP(&OutputFlag, "output", "o", "", "设置输出")
  22. }
英文:

When you create new commands from the cobra CLI, it usually puts them into separate files. If you want to run a command from a different command file, it would look something like this:

  1. package cmd
  2. import "github.com/spf13/cobra"
  3. // DirectoryFlag specifies the directory
  4. var DirectoryFlag string
  5. // OutputFlag specifies the output
  6. var OutputFlag string
  7. // zipupCmd represents the "zipup" command
  8. var zipupCmd = &cobra.Command{
  9. Use: "zipup",
  10. Short: "Zip & Upload",
  11. Long: `Zip & Upload`,
  12. Run: func(cmd *cobra.Command, args []string) {
  13. anyArgs := "whatever"
  14. zipCmd.Run(cmd, []string{anyArgs})
  15. uploadCmd.Run(cmd, []string{anyArgs})
  16. },
  17. }
  18. func init() {
  19. rootCmd.AddCommand(zipupCmd)
  20. zipupCmd.Flags().StringVarP(&DirectoryFlag, "directory", "d", "", "set the directory")
  21. zipupCmd.Flags().StringVarP(&OutputFlag, "output", "o", "", "set the output")
  22. }

huangapple
  • 本文由 发表于 2017年5月3日 04:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/43747075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定