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.
Merge pull request astaxie#206 from faberliu/master
增加flag包使用示例
- Loading branch information
Showing
60 changed files
with
2,918 additions
and
7 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,54 @@ | ||
## func Arg(i int) string | ||
|
||
参数列表 | ||
- i int 非flag命令行参数集合的索引 | ||
|
||
返回值 | ||
- string 第i+1个非flag命令行参数的值 | ||
|
||
功能说明 | ||
- 获取第i+1个非flag命令行参数的值,使用前需要先调用`flag.Parse()`解析flag,底层实际调用了全局变量CommandLine的Arg函数 | ||
|
||
代码示例1 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println(flag.Arg(0)) | ||
} | ||
|
||
执行结果 | ||
|
||
// ./test_arg abc | ||
abc | ||
|
||
代码示例2 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var ip = flag.String("flag", "1234", "test for flags") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println(flag.Arg(0)) | ||
} | ||
|
||
执行结果 | ||
|
||
// ./test_arg abc | ||
abc | ||
|
||
// ./test_arg -flag abc def | ||
def | ||
|
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 Args() []string | ||
|
||
参数列表 | ||
|
||
返回值 | ||
- []string 非flag命令行参数集合 | ||
|
||
功能说明 | ||
- 获取非flag命令行参数集合,使用前需要先调用`flag.Parse`解析flag,底层实际调用了全局变量CommandLine的Args函数 | ||
|
||
代码示例1 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println(flag.Args()) | ||
} | ||
|
||
|
||
执行结果 | ||
|
||
// ./test_args abc | ||
[abc] | ||
|
||
代码示例2 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var ip = flag.String("flag", "1234", "test for flags") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println(flag.Args()) | ||
} | ||
|
||
执行结果 | ||
|
||
// ./test_args abc def | ||
[abc def] | ||
|
||
// ./test_args -flag abc def ghi | ||
[def ghi] | ||
|
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,39 @@ | ||
## func Bool(name string, value bool, usage string) *bool | ||
|
||
参数列表 | ||
- name string 命令行flag名称 | ||
- value bool 默认值 | ||
- usage string 帮助信息 | ||
|
||
返回值 | ||
- *bool 返回一个bool类型的flag值的地址 | ||
|
||
功能说明 | ||
- 定义一个带默认值和提示语句的bool类型flag,返回flag对应值的地址,底层实际调用了全局变量CommandLine的Bool方法 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var boolFlag = flag.Bool("flag", false, "help message for flag") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("boolFlag: ", *boolFlag) | ||
} | ||
|
||
执行结果 | ||
|
||
// ./test_bool | ||
boolFlag: false | ||
|
||
// ./test_bool -flag | ||
boolFlag: true | ||
|
||
// ./test_bool -flag=false | ||
boolFlag: false |
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,43 @@ | ||
## func BoolVar(p *bool, name string, value bool, usage string) | ||
|
||
参数列表 | ||
- p *bool bool型变量指针 | ||
- name string flag名称 | ||
- value bool 默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将指定flag值绑定到一个bool变量,底层实际调用了全局变量CommandLine的BoolVar函数 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var boolFlag bool | ||
|
||
func init() { | ||
flag.BoolVar(&boolFlag, "flag", false, "help message for flag") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println("boolFlag: ", boolFlag) | ||
} | ||
|
||
执行结果 | ||
|
||
// ./test_boolval | ||
boolFlag: false | ||
|
||
// ./test_boolval -flag | ||
boolFlag: true | ||
|
||
// ./test_boolval -flag=true | ||
boolFlag: true |
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,44 @@ | ||
## func Duration(name string, value time.Duration, usage string) *time.Duration | ||
|
||
参数列表 | ||
- name string flag名称 | ||
- value time.Duration 默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
- *time.Duration 返回一个Duration类型的flag值的地址 | ||
|
||
功能说明 | ||
- 定义一个带默认值和提示语句的Duration类型flag,返回flag对应值的地址 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var durationFlag = flag.Duration("duraT", 12, "help message for duraT") | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println(*durationFlag) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testduration | ||
12ns | ||
|
||
// ./testduration -duraT | ||
flag needs an argument: -duraT | ||
Usage of ./testduration: | ||
-duraT=12ns: help message for duraT | ||
|
||
// ./testduration -duraT=200ms | ||
200ms | ||
|
||
// ./testduration -duraT 3000h | ||
3000h0m0s |
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 DurationVar(p *time.Duration, name string, value time.Duration, usage string) | ||
|
||
参数列表 | ||
- p *time.Duration 需要与flag值绑定的变量的指针 | ||
- name string flag名称 | ||
- value time.Duration 默认值 | ||
- usage string 提示信息 | ||
|
||
返回值 | ||
|
||
功能说明 | ||
- 将一个time.Duration类型的flag值绑定到一个time.Duration的变量 | ||
|
||
代码示例 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
var durationFlag time.Duration | ||
|
||
func init() { | ||
flag.DurationVar(&durationFlag, "duraT", 120, "help message for duraT") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Println(durationFlag) | ||
} | ||
|
||
代码输出 | ||
|
||
// ./testdurationvar | ||
120ns | ||
|
||
// ./testdurationvar -duraT | ||
flag needs an argument: -duraT | ||
Usage of ./testdurationvar: | ||
-duraT=120ns: help message for duraT | ||
|
||
// ./testdurationvar -duraT=200ms | ||
200ms | ||
|
||
// ./testdurationvar -duraT 3000h | ||
3000h0m0s |
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,36 @@ | ||
## func (f *FlagSet) Arg(i int) string | ||
|
||
参数列表 | ||
- i int 非flag参数集合的索引 | ||
|
||
返回值 | ||
- string 第i+1个非flag参数的值 | ||
|
||
功能说明 | ||
- 获取Flag集合f中第i+1个非flag参数的值 | ||
|
||
代码示例1 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var myFlagSet = flag.NewFlagSet("myflagset", flag.ExitOnError) | ||
var stringFlag = myFlagSet.String("abc", "default value", "help mesage") | ||
|
||
func main() { | ||
myFlagSet.Parse([]string{"-abc", "def", "ghi", "123"}) | ||
args := myFlagSet.Args() | ||
for i := range args { | ||
fmt.Println(i, myFlagSet.Arg(i)) | ||
} | ||
} | ||
|
||
执行结果 | ||
|
||
// ./testflagsetarg | ||
0 ghi | ||
1 123 |
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,35 @@ | ||
## func (f *FlagSet) Args() []string | ||
|
||
参数列表 | ||
|
||
返回值 | ||
- []string 所有非flag参数的集合 | ||
|
||
功能说明 | ||
- 获取Flag集合f中所有非flag参数的集合 | ||
|
||
代码示例1 | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var myFlagSet = flag.NewFlagSet("myflagset", flag.ExitOnError) | ||
var stringFlag = myFlagSet.String("abc", "default value", "help mesage") | ||
|
||
func main() { | ||
myFlagSet.Parse([]string{"-abc", "def", "ghi", "123"}) | ||
args := myFlagSet.Args() | ||
for i := range args { | ||
fmt.Println(i, myFlagSet.Arg(i)) | ||
} | ||
} | ||
|
||
执行结果 | ||
|
||
// ./testflagsetarg | ||
0 ghi | ||
1 123 |
Oops, something went wrong.