/
root.go
31 lines (26 loc) · 767 Bytes
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package cmd
import (
"log"
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "details",
Short: "This project takes student information",
Long: `A long string about description`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name := cmd.PersistentFlags().Lookup("name").Value
age := cmd.PersistentFlags().Lookup("age").Value
log.Printf("Hello %s (%s years), Welcome to the command line world", name, age)
},
}
// Execute is Cobra logic startpoint
func Execute() {
rootCmd.PersistentFlags().StringP("name", "n", "stranger", "Name of the student")
rootCmd.PersistentFlags().IntP("age", "a", 25, "Age of the student")
if err := rootCmd.Execute(); err != nil {
log.Println(err)
os.Exit(1)
}
}