Adding a protoc plugin option
You might already have an idea of how to add options to the plugin. If you remember, we created an instance of protogen.Options
in our main plugin. This Options
object can work with the flags
package in the standard library. We simply need to define our flags and pass the Set
function to the Options
instance. This looks like this (cmd/protoc-gen-check/main.go
):
import ( "flags" //... ) func main() { var flags flag.FlagSet phoneRegexp := flags.String("phone_regexp", "", "custom regex for phone checking.") opts := protogen.Options{ ParamFunc: flags.Set, } //... }
If you are not familiar with the flags package from the standard library, we define a flag of type string
, the name phone_regexp
, and the default value of the empty string.
We can now pass the phoneRegexp
variable to our generateFile...