func main() { args := os.Args if args == nil || len(args) < 2 { Usge() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("USAGE1:calc add <integer1><integer2>") } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE2:calc add <integer1><integer2>", err1) return } ret := simplemath.Add(v1, v2) fmt.Println("Result1:", ret) case "sqrt": if len(args) != 3 { fmt.Println("USAGE3:calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("USAGE4:calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result2:", ret) default: Usge() } }
func main() { var args = os.Args if nil == args || len(args) <= 2 { fmt.Println("Arguments to less!") Usage() os.Exit(0) } switch args[1] { case "add": if 4 != len(args) { fmt.Println("USAGE: calc add integer integer") Usage() os.Exit(0) } a, err1 := strconv.Atoi(args[2]) b, err2 := strconv.Atoi(args[3]) if nil != err1 || nil != err2 { fmt.Println("Can not convert string to integer!") Usage() os.Exit(0) } ret := simplemath.Add(a, b) fmt.Println(a, "+", b, " = ", ret) case "sqrt": if 3 != len(args) { fmt.Println("USAGE: calc sqrt integer") Usage() os.Exit(0) } a, err := strconv.Atoi(args[2]) if nil != err { fmt.Println("Can not convert string to integer!") Usage() os.Exit(0) } ret, err := simplemath.Sqrt(a) if nil != err { fmt.Println("Can not use ", a, " in sqrt function!") Usage() os.Exit(0) } fmt.Println("Sqrt(", a, ") = ", ret) default: Usage() } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } switch args[0] { case "add": if len(args) != 3 { showAddUsage() return } //strconv.Atoi: change str into integer v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { showAddUsage() return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { showSqrtUsage() return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() } }
func main() { args := os.Args[1:] if args == nil || len(args) < 2 { Usage() return } //fmt.Println("args 0 is:", args[0], args[1], args[2]) //fmt.Println("length is:", len(args)) /*for i,v := range args { fmt.Println(i," ", v) } */ switch args[0] { case "add": if len(args) != 3 { fmt.Println("USAGE1: calc add <integer1><integer2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("USAGE2: calc add <integer1><integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() fmt.Println("why me") } }
func main() { args := os.Args if args == nil || len(args) < 3 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("Usage: cale add <integer1> <integer2>") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("Usage: cale add <integer1> <integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 3 { fmt.Println("Usage: calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("Usage: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: fmt.Println("unknown command ", args[1]) Usage() } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } fmt.Println("%s\n", args) switch args[1] { case "add": if len(args) != 4 { ErrAdd() return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { ErrAdd() return } ret := simplemath.Add(v1, v2) fmt.Println("result: %d ", ret) case "sqrt": if len(args) != 3 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: %d ", ret) default: Usage() } }
func main() { for i, v := range os.Args { fmt.Printf("%d:\t%s\n", i, v) } args := os.Args[1:] if args == nil || len(args) < 2 { Usage() return } switch args[0] { case "add": if len(args) != 3 { fmt.Println("Usage calc add <integer><interger2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("Usage calc add <integer><interger2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("Usage calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { fmt.Println("Usage calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("ResultL: ", ret) default: Usage() } }
func main() { flag.Parse() args := flag.Args() if args == nil || len(args) < 2 { Usage() return } switch args[0] { case "add": if len(args) != 3 { fmt.Println("USAGE: calc add <integer1> <integer2>") return } v1, err1 := strconv.Atoi(args[1]) v2, err2 := strconv.Atoi(args[2]) if err1 != nil || err2 != nil { fmt.Println("USAGE: calc add <integer1> <integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 2 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[1]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() } }
func main() { args := os.Args // fmt.Println(len(args)) if args == nil || len(args) < 2 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("Usage:add 3 8") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE:add 3 4") return } ret := simplemath.Add(v1, v2) fmt.Println("Result:", ret) case "sqrt": if len(args) != 3 { fmt.Println("sqrt 3-->") } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("这样输入:sqrt 3.") return } ret := simplemath.Sqrt(v) fmt.Println(ret) default: fmt.Println("default") } }
func main() { args := os.Args if args == nil || len(args) < 2 { Usage() return } switch args[1] { case "add": if len(args) != 4 { fmt.Println("USAGE: calc add <integer1><integer2>") return } v1, err1 := strconv.Atoi(args[2]) v2, err2 := strconv.Atoi(args[3]) if err1 != nil || err2 != nil { fmt.Println("USAGE: calc add <integer1><integer2>") return } ret := simplemath.Add(v1, v2) fmt.Println("Result: ", ret) case "sqrt": if len(args) != 3 { fmt.Println("USAGE: calc sqrt <integer>") return } v, err := strconv.Atoi(args[2]) if err != nil { fmt.Println("USAGE: calc sqrt <integer>") return } ret := simplemath.Sqrt(v) fmt.Println("Result: ", ret) default: Usage() //fmt.Printf("len(args): %d, args[0] : %s , args[1]: %s, args[2]: %d, args[3]: %d",len(args), args[0], args[1], args[2], args[3]) } }