Created
January 16, 2023 06:38
-
-
Save reetasingh/8ef65336e9c56cd0962b1131616fe40e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | |
*/ | |
package cmd | |
import ( | |
"fmt" | |
"github.com/spf13/cobra" | |
) | |
type Person struct { | |
firstName string | |
lastName string | |
phoneNumbers []string | |
} | |
var firstName string | |
var lastName string | |
var phone []string | |
// addCmd represents the add command | |
var addCmd = &cobra.Command{ | |
Use: "add", | |
Short: "A brief description of your command", | |
Long: `A longer description that spans multiple lines and likely contains examples | |
and usage of using your command. For example: | |
Cobra is a CLI library for Go that empowers applications. | |
This application is a tool to generate the needed files | |
to quickly create a Cobra application.`, | |
Run: func(cmd *cobra.Command, args []string) { | |
firstName, err := cmd.Flags().GetString("first-name") | |
if err != nil { | |
panic(err) | |
} | |
lastName, err := cmd.Flags().GetString("last-name") | |
if err != nil { | |
panic(err) | |
} | |
phone, err := cmd.Flags().GetStringArray("phone-number") | |
if err != nil { | |
panic(err) | |
} | |
p := new(Person) | |
p.firstName = firstName | |
p.lastName = lastName | |
p.phoneNumbers = phone | |
fmt.Println(p) | |
}, | |
} | |
func init() { | |
rootCmd.AddCommand(addCmd) | |
addCmd.Flags().StringVar(&firstName, "first-name", "", "First name of the person") | |
addCmd.Flags().StringVar(&lastName, "last-name", "", "Last name of the person") | |
addCmd.Flags().StringArrayVar(&phone, "phone-number", []string{}, "phone number of the person") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment