forked from astaxie/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## func (f *FlagSet) Lookup(name string) *Flag | ||
|
||
参数列表 | ||
- name string flag名称 | ||
|
||
返回值 | ||
- *Flag Flag指针 | ||
|
||
功能说明 | ||
- 获取flag集合f中名称为name值的flag指针,如果对应的flag不存在,返回nil | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var myFlagSet = flag.NewFlagSet("testFlagSet", flag.ExitOnError) | ||
var testFlag = myFlagSet.String("test", "default value", "help message.") | ||
|
||
func print(f *flag.Flag) { | ||
if f != nil { | ||
fmt.Println(f.Value) | ||
} else { | ||
fmt.Println(nil) | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Print("test:") | ||
print(myFlagSet.Lookup("test")) | ||
fmt.Print("test1:") | ||
print(myFlagSet.Lookup("test1")) | ||
myFlagSet.Parse([]string{"-test=12345"}) | ||
fmt.Print("test:") | ||
print(myFlagSet.Lookup("test")) | ||
fmt.Print("test1:") | ||
print(myFlagSet.Lookup("test1")) | ||
} | ||
|
||
运行结果 | ||
|
||
// ./testflagsetlookup -test "12345" | ||
test:default value | ||
test1:<nil> | ||
test:12345 | ||
test1:<nil> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
## func (f *FlagSet) NArg() int | ||
|
||
参数列表 | ||
|
||
返回值 | ||
- int 返回解析后剩余的参数的数量 | ||
|
||
功能说明 | ||
- 获取输入参数解析后剩余的参数的数量,即fmt.Args()返回的值的元素个数 | ||
|
||
示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
uintFlag = myFlagSet.Uint("uintFlag", 1000, defaultUsage) | ||
uintVar uint | ||
) | ||
|
||
func init() { | ||
myFlagSet.UintVar(&uintVar, "uintVar", 30, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"-uintFlag", "24", | ||
"--uintVar", "25", | ||
"arg1", | ||
"arg2", | ||
"arg3", | ||
} | ||
myFlagSet.Parse(args) | ||
length := myFlagSet.NArg() | ||
for i := 0; i < length; i++ { | ||
fmt.Println(i, myFlagSet.Arg(i)) | ||
} | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetnarg | ||
0 arg1 | ||
1 arg2 | ||
2 arg3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## func (f *FlagSet) NFlag() int | ||
|
||
参数列表 | ||
|
||
返回值 | ||
- int 返回解析成功的flag个数 | ||
|
||
功能说明 | ||
- 获取解析成功的flag个数 | ||
|
||
示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
uintFlag = myFlagSet.Uint("uintFlag", 1000, defaultUsage) | ||
testFlag = myFlagSet.String("testFlag", "default value", defaultUsage) | ||
uintVar uint | ||
) | ||
|
||
func init() { | ||
myFlagSet.UintVar(&uintVar, "uintVar", 30, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"-uintFlag", "24", | ||
"arg1", | ||
} | ||
fmt.Println("before:", myFlagSet.NFlag()) | ||
myFlagSet.Parse(args) | ||
fmt.Println("after:", myFlagSet.NFlag()) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testflagsetnflag | ||
before: 0 | ||
after: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
## func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value uint64 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *uint64 返回一个uint64类型的变量的地址 | ||
|
||
功能说明 | ||
- 为flag集合f增加爱一个带默认值和提示语句的uint64类型flag,返回flag对应变量值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
uint64Flag = myFlagSet.Uint64("uint64Flag", 3310000, defaultUsage) | ||
uint64Var uint64 | ||
) | ||
|
||
func init() { | ||
myFlagSet.Uint64Var(&uint64Var, "uint64Var", 719200, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"--uint64Var", "25", | ||
"-uint64Flag", "1231", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("uint64Flag", *uint64Flag) | ||
fmt.Println("uint64Var", uint64Var) | ||
} | ||
|
||
|
||
代码输出 | ||
|
||
// ./testflagsetuint64 | ||
uint64Flag 1231 | ||
uint64Var 25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
## func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) | ||
|
||
参数列表 | ||
- p *uint64 需要与flag参数值绑定的变量地址 | ||
- name string flag名称 | ||
- value uint64 变量默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将f中指定flag参数值绑定到uint64变量 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
defaultUsage = "help message" | ||
) | ||
|
||
var ( | ||
myFlagSet = flag.NewFlagSet("newflagset", flag.ExitOnError) | ||
uint64Flag = myFlagSet.Uint64("uint64Flag", 3310000, defaultUsage) | ||
uint64Var uint64 | ||
) | ||
|
||
func init() { | ||
myFlagSet.Uint64Var(&uint64Var, "uint64Var", 719200, defaultUsage) | ||
} | ||
|
||
func main() { | ||
args := []string{ | ||
"--uint64Var", "25", | ||
"-uint64Flag", "1231", | ||
"arg2", | ||
} | ||
myFlagSet.Parse(args) | ||
fmt.Println("uint64Flag", *uint64Flag) | ||
fmt.Println("uint64Var", uint64Var) | ||
} | ||
|
||
|
||
代码输出 | ||
|
||
// ./testflagsetuint64 | ||
uint64Flag 1231 | ||
uint64Var 25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## func Lookup(name string) *Flag | ||
|
||
参数列表 | ||
- name string flag名称 | ||
|
||
返回值 | ||
- *Flag Flag指针 | ||
|
||
功能说明 | ||
- 获取flag集合中名称为name值的flag指针,如果对应的flag不存在,返回nil | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var testFlag = flag.String("test", "default value", "help message.") | ||
|
||
func print(f *flag.Flag) { | ||
if f != nil { | ||
fmt.Println(f.Value) | ||
} else { | ||
fmt.Println(nil) | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Print("test:") | ||
print(flag.Lookup("test")) | ||
fmt.Print("test1:") | ||
print(flag.Lookup("test1")) | ||
flag.Parse() | ||
fmt.Print("test:") | ||
print(flag.Lookup("test")) | ||
fmt.Print("test1:") | ||
print(flag.Lookup("test1")) | ||
} | ||
|
||
运行结果 | ||
|
||
// ./testlookup -test "12345" | ||
test:default value | ||
test1:<nil> | ||
test:12345 | ||
test1:<nil> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters