Tigraine

Daniel Hoelbling-Inzko talks about programming

Parsing flags and arguments

Posted by Daniel Hölbling on March 14, 2023

Usually when we start a new Go application the first stop is to install Viper and Cobra to handle all the commandline input/config flag needs we have.

But sometimes it's just a small program that should just do one thing and you forego all the ceremony and just use os.Args for parsing the commandline arguments. And next you think maybe a -debug flag for better logging would be nice, and you add vanilla flag.Parse() to it. All easy in Go, but one thing I wasn't aware of is that os.Args does contain all the flags so it becomes annoying to use flag and os.Args together.

Until you realise the Go standard library has also thought of that and there is flag.Args() that is equivalent to os.Args except minus the flags that are already parsed (so call it after flag.Parse() and with less hassle.

Filed under golang
comments powered by Disqus

My Photography business

Projects

dynamic css for .NET

Archives

more