7

Define custom command-line flag types in Go 1.19

 2 years ago
source link: https://blog.devgenius.io/define-custom-command-line-flag-types-in-go-1-19-e9a9c8838c5f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Define custom command-line flag types in Go 1.19

Command flags provide a method to set options for a program. Go has great support for processing flags. With the release of Go 1.19, a new addition has been made to the Go flag package. The flag package can now process custom struct types. This means that programers can stop writing code to post-process command flags to instantiate custom struct types. In this post, I’ll detail my approach implementing the encoding.TextUnmarshaler and encoding.TextMarshaler interface.

The basics

To start, I’ll define a struct type named Person. The struct will have 2 fields, the fields will be named FirstName and LastName . I chose 2 fields to demonstrate how one string will populate 2 struct fields. Here is the struct definition :

1*HASV-AR7YLr49BIwPj2-ug.png

The next step I’ll take will be to add 2 methods to type Person. This is to satisfy both encoding.TextMarshaler and encoding.TextUnmarshaler interfaces. This will be the area to specify how a string becomes your custom type, and how your type becomes a string. Here is my implementation :

1*UPIrtw_IDnM1PmCVI4oBUw.png

While defining UnmarshalText , it was important to return and look for errors. In this case, after splitting the string, I verify the array length. If one name was provided, the length would be 1, resulting in an error prompting for both names. If you noticed, p is referred to as a pointer, this is to ensure the field data being set persists. Now that the interfaces are satisfied, it was time to test the implementation.

Implementation

Defining and accessing flags with custom types is quite simple. First, you’ll need to define a variable with the desired type. The variable will also hold the flag data after parsing. The next step would be to invoke flag.TextVar . TextVar is the function that will define a flag with the custom data type. Here is an implementation to process flag person as type Person :

1*_gF-aDYj1RXK9QghtFxZOQ.png

Here is the code on the terminal :

1*xcRWonEtrpCHC3kR0QiIjA.gif

In this gif, I show possible use cases. I deliberately made an error to show the error message I wrote earlier, and invoked the program with flag -h to show the generated documentation.

Conclusion

The flag package is a great way to abstract retrieving user defined program options. It is a great way to generate command-line documentation while writing code too. This new addition adds static types for command-line flags, technically. It also enables programmers to define a string input format that is appealing to their users.

Sources


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK