“This is the 23rd day of my participation in the First Challenge 2022. For details: First Challenge 2022”

The standard library flag

The implementation of the standard library only applies to the libraries in the standard library, and flag is used to parse command-line arguments.

Analysis of the

  • Almost a third of the code defines a value type interface, realize the interface has a bool/int/uint/int64 / uint64 float64 / string/duration
  • This value type interface defines reading and writing to values, as well as string reading
  • Interfaces that implement value types have their own constructors
  • The rest of the story is all about FlagSet
  • FlagSet: A structure that represents a set of defined flags, each of which has a name
  • The flag package defaults to a global FlagSet object called CommandLine, in init()

Source code analysis

Value type analysis:

  • Look at the class diagram

flag.Parse():

  • The process is simple. The command line parameters are analyzed one by one to see if there are predefined flags
  • If so, set the corresponding value, and then parse the next command line argument

flag.IntVar() flag.BoolVar() flag.UintVar() flag.Int64Var() flag.Uint64Var() flag.StringVar() flag.Float64Var() flag.DurationVar()

  • Finally, falg.var ()
  • Check whether the flag exists according to the k of the passed parameter
  • If it exists, an error is reported. If it does not exist, instantiate a flag and store it

Flag. Visit () series:

  • A collection of convenient flags, for which the specified function is executed
  • There are two kinds of flag sets: one is acceptable to the program, that is, the acceptable flag set specified in the code
  • The other is run time, specified by command line arguments, and may be a subset of the set above

flag.Lookup():

  • Look for k that passes flag, look for flag

flag.Set()

  • Through the k of flag, set the value of flag (manually, after the expected flag is defined)

flag.PrintDefaults():

  • -flag Name value Type Usage Default value Newline

flag.NFlag()

  • Returns the number of flags set by command line arguments

flag.Arg()

  • Returns the number of arguments, which is a command line argument

flag.Args()

  • Returns the full command line argument

flag.Int() flag.Int64() flag.Uint() flag.Uint64() flag.String() flag.Float64() flag.Duration() flag.Bool()

  • A wrapper around flag.boolvar ()

flag.Parsed()

  • Determine if it has been resolved

Supported formats

Source code analysis:

  • The -a=b notation supports all data types
  • -a Default a is true if it is a bool
  • – A b if it’s not a bool is the same thing as a=b, if a is a bool, then b cannot be recognized as a flag

In other words:

  • Minus a is equal to b, the most neutral way to write it, is OK
  • The minus a notation only works for bool
  • The minus a, b notation only works for non-bool types
  • The prefixes – and — have the same effect on the standard Flag library

The official document reads:

- flag-flag = x-flag x // non-boolean flags only The prefix is one dash or two dashes are equivalentCopy the code

Note :-flag only applies to bool. Non-bool needs an error in flag needs argument

After analyzing the source code, check the official documentation

Flag: parsing command line flags (command line flags are flags, and a command line parameter may contain multiple flags)

Usage:

  • Define the expected flags for the program
    • This defined action should be done before parsing, usually in init()
    • The flag package provides the IntVar() /Int() family
    • In addition to defining specified data types, you can also define flags for custom data types using Var()
  • Parsing the flag
    • flag.Parse()
  • Use values passed in as command-line arguments (which is the biggest point of this package)
    • When you define the expected flag, you specify the variable in which the value will be stored after parsing. You can use that variable directly

In addition to the above three core steps, the following auxiliary functions are provided:

  • Get command line argument/get first command line argument/Get number of command line arguments
  • Get the number of command line parameters to set flag /
  • All expected flag information is displayed: Flag name Data Type Usage Default value
  • Manually change the value of flag
  • Executes a function for all or set flags

Additional notes for documentation:

  • Multiple flags can be bound to one variable to achieve long and short flags. The disadvantage is that if the command line parameters specify both long and short flags, only the later ones will prevail
  • The default values of unspecified flags are all zero values in go

conclusion

Flags are sufficient in most scenarios, but the following are not considered in implementation:

  • Long and short flag, not fine enough
  • Parsing: If an unparsed one is encountered, subsequent ones are not parsed

Flag class diagrams and flowcharts of major functions