@aloxc
2019-04-11T10:25:38.000000Z
字数 2869
阅读 388
golang
var help = func () {
fmt.Println("Usage for calc tool.")
fmt.Println("====================================================")
fmt.Println("add 1 2, return 3")
fmt.Println("sub 1 2, return -1")
fmt.Println("mul 1 2, return 2")
fmt.Println("sqrt 2, return 1.4142135623730951")
}
func CalcByOs() error {
args := os.Args
if len(args) < 3 || args == nil {
help()
return nil
}
operate := args[1]
switch operate {
case "add":{
rt := 0
number_one, err1 := strconv.Atoi(args[2])
number_two, err2 := strconv.Atoi(args[3])
if err1 == nil && err2 == nil {
rt = number_one + number_two
fmt.Println("Result ", rt)
}
}
case "sub":
{
rt := 0
number_one, err1 := strconv.Atoi(args[2])
number_two, err2 := strconv.Atoi(args[3])
if err1 == nil && err2 == nil {
rt += number_one - number_two
fmt.Println("Result ", rt)
}
}
case "mul":
{
rt := 1
number_one, err1 := strconv.Atoi(args[2])
number_two, err2 := strconv.Atoi(args[3])
if err1 == nil && err2 == nil {
rt = number_one * number_two
fmt.Println("Result ", rt)
}
}
case "sqrt":
{
rt := float64(0)
if len(args) != 3 {
fmt.Println("Usage: sqrt 2, return 1.4142135623730951")
return nil
}
number_one, err := strconv.ParseFloat(args[2], 64)
if err == nil {
rt = math.Sqrt(number_one)
fmt.Println("Result ", rt)
}
}
default:
help()
}
return nil
}
编译后执行
./calc add 1 2
Result 3
====================
./calc sub 1 2
Result -1
====================
./calc mul 10 20
Result 200
===================
./calc sqrt 2
Result 1.4142135623730951
flag 包比 os 读取参数更方便。可以自定义传入的参数的类型:比如字符串,整型,浮点型,默认参数设置等
基本用法
var operate string
flag.StringVar(&operate,"o", "add", "operation for calc")
# 解释
绑定 operate 变量, name="o", value="add" , usage="operation for calc"
也可以这样定义为指针变量
var operate := flag.String("o", "add", "operation for calc")
同时还可以自定义 flag 类型
所有变量注册之后,调用 flag.Parse() 来解析命令行参数, 如果是绑定变量的方式,直接使用变量进行操作,
如果使用指针变量型,需要 *operate 这样使用。
flag.Args() 表示接收的所有命令行参数集, 也是一个切片
for index, value := range flag.Args {
fmt.Println(index, value)
}
示例程序
func CalcByFlag() error {
var operation string
var numberone float64
var numbertwo float64
flag.StringVar(&operation, "o", "add", "operation for this tool")
flag.Float64Var(&numberone, "n1", 0, "The first number")
flag.Float64Var(&numbertwo, "n2", 0, "The second number")
flag.Parse()
fmt.Println(numberone, numbertwo)
if operation == "add" {
rt := numberone + numbertwo
fmt.Println("Result ", rt)
} else if operation == "sub" {
rt := numberone - numbertwo
fmt.Println("Result ", rt)
} else if operation == "mul" {
rt := numberone * numbertwo
fmt.Println("Result ", rt)
} else if operation == "sqrt" {
rt := math.Sqrt(numberone)
fmt.Println("Result ", rt)
} else {
help()
}
return nil
}
最后的结果效果如下:
./calc -o add -n1 1 -n2 2
Result 3
=============================
./calc -o sub -n1 2 -n2 3
Result -1
============================
./calc -o mul -n1 10 -n2 20
Result 200
===========================
./calc -o sqrt -n1 2
Result 1.4142135623730951
const (
usage = `
add --one ONE --two TWO "加法"
`
add = "add"
)
func main() {
addCmd := flag.NewFlagSet(add, flag.ExitOnError)
one := addCmd.Float64("one", 0, "one")
two := addCmd.Float64("two", 0, "two")
switch os.Args[1] {
case add:
err := addCmd.Parse(os.Args[2:])
if err == nil{
fmt.Println(err)
}
if addCmd.Parsed() {
rt := *one + *two
fmt.Println("Result ", rt)
}
}
}
运行
go run main2.go add --one 123 --two 321