import "flag"

flag The package implements the parsing of command line parameters .

requirement :

use flag.String(), Bool(), Int() Equal function registration flag, The following example declares an integer flag, The parsing results are saved in *int Pointer ip in :
import "flag" var ip = flag.Int("flagname", 1234, "help message for flagname")
If you like it , You can also flag Bind to a variable , use Var Series function :
var flagvar int func init() { flag.IntVar(&flagvar, "flagname", 1234, "help
message for flagname") }
Or you can customize one for flag Type of ( satisfy Value Interface ) And use this type for flag analysis , as follows :
flag.Var(&flagVal, "name", "help message for flagname")
Right flag, The default value is the initial value of the variable .

At all flag After registration , call :
To parse command-line parameters and write registered flag in .

After parsing ,flag Can be used directly . If you use flag oneself , They are pointers ; If you bind to a variable , They are values .
fmt.Println("ip has value ", *ip) fmt.Println("flagvar has value ", flagvar)
After analysis ,flag The following parameters can be downloaded from flag.Args() To obtain or use in flag.Arg(i) Separate access . These parameters are indexed from 0 reach flag.NArg()-1.

command line flag grammar :
-flag -flag=x -flag x // Only non bool Type flag sure
have access to 1 Or 2 individual '-' Number , The effect is the same . The last format cannot be used for bool Type flag, Because if there is a file named 0,false Isochronous , The order is as follows :
cmd -x *
Its meaning will change . You have to use it -flag=false Format to close a bool type flag.

Flag Parsing in the first non flag parameter ( single "-" no flag parameter ) Stop before , Or in the terminator "--" Then stop .

integer flag accept 1234,0664,0x1234 Etc , It can also be negative .bool type flag It can be :
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
time slot flag Accept any legal offer time.ParseDuration Input for .

Default command line flag Function control of set and package level .FlagSet Types allow programmers to define independent flag collection , For example, the implementation of the command line interface under the sub command .FlagSet The method is very similar to the package level function .

©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )