A value parser converts the string value supplied for a command-line flag, assigns the converted result to an associated Go variable, and returns an error when conversion fails. It separates generic flag extraction from type-specific handling: after `-n=100` is split into a name and string value, an integer parser can set an `int`, while a string parser can set a `string`. A shared `func(string) error` signature lets a flag parser keep parsers of different target types in one map. section 4.2.1
A practical custom design uses higher-order functions. `intVar` and `stringVar` accept pointers to destination variables and return closures. Each closure retains its pointer: `stringVar` assigns directly, while `intVar` calls `strconv.Atoi`, assigns the converted integer, and returns any conversion error. Keeping the target pointer outside the common parser signature makes parsers uniform and independently testable. section 4.2.2
Bind parsers to CLI flags
Bind flag names to closures created for particular configuration fields, then look up and call the matching parser after splitting each argument. For example, `"url"` can use `stringVar(&c.url)`, while `"n"`, `"c"`, and `"rps"` use `intVar` for their integer fields. Report an unknown flag separately; wrap a parser failure with the flag name and invalid value so callers can still inspect the underlying error. section 4.2.3
Conversion is not validation. A built-in integer parser can accept any value that converts to an integer, including values that are meaningless for a program such as negative request counts or zero concurrency. Enforce program rules after basic conversion, or provide a custom parser when validation belongs with the flag value itself. section 4.4
The standard `flag` package represents a flag’s parser through `flag.Value`. A `Value` implementation provides `String() string`, used to obtain the displayed default, and `Set(string) error`, used to parse and update the linked variable. Built-in implementations include string, integer, and boolean value parsers; this narrow interface also permits custom parser types. section 4.4.1
Register a validating parser with Var
Use `FlagSet.Var` to attach a custom `flag.Value` implementation when built-in `IntVar` behavior is insufficient. Initialize the linked variable before calling `Var` to establish its default, because `Var` obtains the displayed default through `String`. During parsing, the flag set calls `Set`; a validation failure becomes an actionable command-line error. A custom positive-integer parser can therefore reject `-n 0` while accepting readable numeric input such as `10_000`. section 4.4.3
Figure 4.4 Calling intVar returns a closure that retains the variable’s pointer. Calling the closure updates the original variable’s value through the pointer.
The closure diagram makes the key binding visible: a returned parser retains a variable pointer and updates the original variable when called. section 4.2.2
