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

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

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

问题

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

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

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

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

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

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

英文:

In cobra I've create a command of commands:

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

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

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"部分转换为函数。

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

var (
    cfgDirectory string
    cfgFilename  string
    cfgOutput    string
)

var rootCmd = &cobra.Command{
    Use:   "root",
    Short: "root",
    Long:  "root",
    Run: func(cmd *cobra.Command, args []string) {
        // something
    },
}

var uploadCmd = &cobra.Command{
    Use:   "upload",
    Short: "upload",
    Long:  "upload",
    Run: func(cmd *cobra.Command, args []string) {
        Upload()
    },
}

var zipCmd = &cobra.Command{
    Use:   "zip",
    Short: "zip",
    Long:  "zip",
    Run: func(cmd *cobra.Command, args []string) {
        Zip()
    },
}

var zipupCmd = &cobra.Command{
    Use:   "zipup",
    Short: "zipup",
    Long:  "zipup",
    Run: func(cmd *cobra.Command, args []string) {
        Zip()
        Upload()
    },
}

func setFlags() {
    rootCmd.PersistentFlags().StringVar(&cfgDirectory, "directory", "", "explanation")
    rootCmd.PersistentFlags().StringVar(&cfgFilename, "filename", "", "explanation")
    rootCmd.PersistentFlags().StringVar(&cfgOutput, "output", "", "explanation")
}

func Upload() {
    // you know what to do
}

func Zip() {
    // you know what to do
}
...

// 添加子命令
rootCmd.AddCommand(zipCmd)
rootCmd.AddCommand(uploadCmd)
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:

var (
cfgDirectory  string
cfgFilename  string
cfgOutput string
)
var rootCmd = &cobra.Command{
Use:   "root",
Short: "root",
Long:  "root",
Run: func(cmd *cobra.Command, args []string) {
// something
},
}
var uploadCmd = &cobra.Command{
Use:   'upload',
Short: 'upload',
Long:  `upload`,
Run: func(cmd *cobra.Command, args []string) {
Upload()
},
}
var zipCmd = &cobra.Command{
Use:   "zip",
Short: "zip",
Long:  "zip",
Run: func(cmd *cobra.Command, args []string) {
Zip()
},
}
var zipupCmd = &cobra.Command{
Use:   "zipup",
Short: "zipup",
Long:  "zipup",
Run: func(cmd *cobra.Command, args []string) {
Zip()
Upload()
},
}
func setFlags() {
rootCmd.PersistentFlags().StringVar(&cfgDirectory, "directory", "", "explanation")
rootCmd.PersistentFlags().StringVar(&cfgFilename, "filename", "", "explanation")
rootCmd.PersistentFlags().StringVar(&cfgOutput, "output", "", "explanation")
}
func Upload() {
// you know what to do
}
func Zip() {
// you know what to do
}
...
// Add subcommands
rootCmd.AddCommand(zipCmd)
rootCmd.AddCommand(uploadCmd)
rootCmd.AddCommand(zipupCmd)

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

答案2

得分: 2

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

package cmd

import "github.com/spf13/cobra"

// DirectoryFlag 指定目录
var DirectoryFlag string

// OutputFlag 指定输出
var OutputFlag string

// zipupCmd 表示"zipup"命令
var zipupCmd = &cobra.Command{
	Use:   "zipup",
	Short: "Zip & Upload",
	Long:  `Zip & Upload`,
	Run: func(cmd *cobra.Command, args []string) {
		anyArgs := "whatever"
		zipCmd.Run(cmd, []string{anyArgs})
		uploadCmd.Run(cmd, []string{anyArgs})
	},
}

func init() {
	rootCmd.AddCommand(zipupCmd)
	zipupCmd.Flags().StringVarP(&DirectoryFlag, "directory", "d", "", "设置目录")
	zipupCmd.Flags().StringVarP(&OutputFlag, "output", "o", "", "设置输出")
}
英文:

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:

package cmd
import "github.com/spf13/cobra"
// DirectoryFlag specifies the directory
var DirectoryFlag string
// OutputFlag specifies the output
var OutputFlag string
// zipupCmd represents the "zipup" command
var zipupCmd = &cobra.Command{
Use:   "zipup",
Short: "Zip & Upload",
Long: `Zip & Upload`,
Run: func(cmd *cobra.Command, args []string) {
anyArgs := "whatever"
zipCmd.Run(cmd, []string{anyArgs})
uploadCmd.Run(cmd, []string{anyArgs})
},
}
func init() {
rootCmd.AddCommand(zipupCmd)
zipupCmd.Flags().StringVarP(&DirectoryFlag, "directory", "d", "", "set the directory")
zipupCmd.Flags().StringVarP(&OutputFlag, "output", "o", "", "set the output")
}

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:

确定