Golang编程语言知识介绍


  • 首页

  • todo

  • 思考

  • life

  • food

  • OS

  • lua

  • redis

  • Golang

  • C

  • TCP/IP

  • ebpf

  • p4

  • OpenVPN

  • IPSec

  • L2TP

  • DNS

  • distributed

  • web

  • OpenWRT

  • 运维

  • Git

  • 鸟哥的私房菜

  • IT杂谈

  • 投资

  • About Me

  • 友情链接

  • FTP

  • 搜索
close

go flags包简介 go flags package introduction

时间: 2023-12-08   |   分类: go     |   阅读: 844 字 ~2分钟

flag包是Go语言标准库提供用来解析命令行参数的包,使得开发命令行工具更为简单

常用方法

1.flag.Usage 输出使用方法,如linux下ls -h的帮助输出

2.flag.Type(参数名, 默认值, 使用提示) Type为类型 如String, Int, Uint 调用相应的flag.Sring flag.Int flag.Uint方法

方法返回相应类型的指针

3.flag.Type(指针, 参数名, 默认值, 使用提示) 与flag.Type方法基本相同,不同的是多一个指针参数,将使用传入的指针,不会再创建指针返回

4.flag.Args 返回解析完命令行参数后的其他参数,如./sh -name cqh a1 a2,将返回a1 a2

5.flag.Parse 执行解析

使用示例代码

创建test.go

package main
 
import (
    "fmt"
    "flag"
)
 
func main() {
    namePtr := flag.String("name", "username", "姓名")
    agePtr := flag.Int("age", 18, "年龄")
    musclePtr := flag.Bool("muscle", true, "是否有肌肉")
 
    var email string
    flag.StringVar(&email, "email", "chenqionghe@sina.com", "邮箱")
 
 
    flag.Parse()
 
    args := flag.Args()
    fmt.Println("name:", *namePtr)
    fmt.Println("age:", *agePtr)
    fmt.Println("muscle:", *musclePtr)
    fmt.Println("email:", email)
    fmt.Println("args:", args)
}

执行go buld后,建立test执行文件

./test -h

Usage of ./test:
  -age int
        年龄 (default 18)
  -email string
        邮箱 (default "chenqionghe@sina.com")
  -muscle
        是否有肌肉 (default true)
  -name string
        姓名 (default "username")

执行

./test -name 肌肉男 -age 20 -email muscle@muscle.com 哈哈 呵呵 嘿嘿

name: 肌肉男
age: 20
muscle: true
email: muscle@muscle.com
args: [哈哈 呵呵 嘿嘿]

自定义flag 只要实现flag.Value接口即可

type Value interface {
  String() string
  Set(string) error
}

如下定义了一个Hello类型,实现了Value接口

//自定义解析参数,实现Set和String方法
type Hello string
 
func (p *Hello) Set(s string) error {
    v := fmt.Sprintf("Hello %v", s)
    *p = Hello(v)
    return nil
}
 
func (p *Hello) String() string {
    return fmt.Sprintf("%f", *p)
}

使用示例代码  

package main
 
import (
    "fmt"
    "flag"
)

//自定义解析参数,实现Set和String方法

type Hello string
 
func (p *Hello) Set(s string) error {
    v := fmt.Sprintf("Hello %v", s)
    *p = Hello(v)
    return nil
}
 
func (p *Hello) String() string {
    return fmt.Sprintf("%f", *p)
}
 
func main() {
    namePtr := flag.String("name", "username", "姓名")
    agePtr := flag.Int("age", 18, "年龄")
    musclePtr := flag.Bool("muscle", true, "是否有肌肉")
 
    var email string
    flag.StringVar(&email, "email", "chenqionghe@sina.com", "邮箱")
 
    var hello Hello
    flag.Var(&hello, "hello", "hello参数")
 
    flag.Parse()
    others := flag.Args()
    fmt.Println("name:", *namePtr)
    fmt.Println("age:", *agePtr)
    fmt.Println("vip:", *musclePtr)
    fmt.Println("hello:", hello)
    fmt.Println("email:", email)
    fmt.Println("other:", others)
}

重新build后运行

1 ./test -name 肌肉男 -age 20 -email muscle@muscle.com -hello chenqionghe 哈哈 呵呵 嘿嘿

name: 肌肉男
age: 20
vip: true
hello: Hello chenqionghe
email: muscle@muscle.com
other: [哈哈 呵呵 嘿嘿]

就是这样,Light weight baby !

以上内容转账自本链接,若需删除请联系站长

#go#
thrift 之 go 入门 Getting Started with Thrift Go
thrift 备忘录 thrift memorandum
shankusu2017@gmail.com

shankusu2017@gmail.com

日志
分类
标签
GitHub
© 2009 - 2025
粤ICP备2021068940号-1 粤公网安备44011302003059
0%