Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang cobra subcommands without arguments matching

Tags:

go

go-cobra

cobra

i want to create golang CLI with cobra. For now, it runs well. I have created something like foobar create --username johndoe. But I need subcommands like foobar create user --username johndoe. There is user subcommand.

I have created with arguments matching,

var applicationCmd = &cobra.Command{
   Use:   "application",
   Short: "Create/Read/Update application",
   Long: `You can create any user or partner.`,
   Run: (cmd *cobra.Command, args []string) {
        if len(args) < 1 {
            fmt.Printf("%sPlease choose any object that you want to change e.g (user, partner)%s\n", chalk.Red, chalk.Reset)
        }
        if stringUtil.Contains(args, "user") {
            createUser()
        }
        if stringUtil.Contains(args, "partner") {
            createPartner()
        }
   },
}

but I think there is a better way to do it using cobra rather than using arguments matching like this. Any suggestion?

like image 779
fzlrhmn Avatar asked Nov 06 '17 05:11

fzlrhmn


2 Answers

I went slightly overboard; but yes, appCmd.Addcommand(newCmd) is what you were looking for.

(I had started working on this before you posted your answer, but I didn't want to scrap the code)

package main

import (
"github.com/spf13/cobra"
"fmt"
)

var rootCmd = &cobra.Command{
    Use:   "app",
    Run: func (cmd *cobra.Command, args []string) {
    },
}

var makeU = &cobra.Command{
    Use:   "makeU",
    Short: "makes a user",
    Long: "Prints stuff about the user. You could also use the flags in your addPartner() function",
    Run: func (cmd *cobra.Command, args []string) {
        fmt.Println("User's name: " + uName)
        fmt.Println("User's number: " + uNumber)
        fmt.Println("User's other stuff: " + uOtherStuff)
    },
}

var makeP = &cobra.Command{
    Use:   "makeP",
    Short: "makes a partner",
    Long: "Prints stuff about the partner. You could also use the flags in your addPartner() function",
    Run: func (cmd *cobra.Command, args []string) {
        fmt.Println("Partner's name: " + uName)
        fmt.Println("Partner's number: " + uNumber)
        fmt.Println("Partner's other stuff: " + uOtherStuff)
    },
}

// adds commands
func addCommands() {
    rootCmd.AddCommand(makeU)
    rootCmd.AddCommand(makeP)
}

// adds flags
var pName, pNumber, pOtherStuff string
var uName, uNumber, uOtherStuff string
func init() {
    makeU.Flags().StringVar(&uName, "username", "default", "--username=<user's name>")
    makeU.Flags().StringVar(&uNumber, "usernumber", "default", "--usernumber=<user's number>")
    makeU.Flags().StringVar(&uOtherStuff, "userother", "default", "--userother=<user's other stuff>")
    makeP.Flags().StringVar(&pName, "partnername", "default", "--partnername=<partner's name>")
    makeP.Flags().StringVar(&pNumber, "partnernumber", "default", "--partnernumber=<partner's number>")
    makeP.Flags().StringVar(&pOtherStuff, "partnerother", "default", "--partnerother=<partner's other stuff>")
}


func main() {
    addCommands()
    rootCmd.Execute()
}
like image 124
Aor Avatar answered Sep 21 '22 21:09

Aor


I've solved this. Just add new cobra command to applicationCmd like so

applicationCmd.AddCommand(yourNewCommand)
like image 30
fzlrhmn Avatar answered Sep 18 '22 21:09

fzlrhmn