package cmd import ( "fmt" "os" "strconv" u "github.com/bcicen/go-units" "github.com/spf13/cobra" ) var ( from string to string rootCmd = &cobra.Command{ Use: "converter", Short: "A brief description of your application", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application. 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) { in, err := strconv.ParseFloat(args[0], 64) if err != nil { fmt.Println(err.Error()) return } fromVal, err := u.Find(cmd.Flag("from").Value.String()) if err != nil { fmt.Println(err.Error()) return } toVal, err := u.Find(cmd.Flag("to").Value.String()) if err != nil { fmt.Println(err.Error()) return } val := u.NewValue(in, fromVal) out, err := val.Convert(toVal) if err != nil { fmt.Println(err.Error()) return } fmt.Println(out.String()) }, } ) func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } func init() { rootCmd.PersistentFlags().StringVarP(&from, "from", "f", "c", "unit system to convert from") rootCmd.PersistentFlags().StringVarP(&to, "to", "t", "f", "unit system to convert to") }