Non-standard command-line flag parsing in Go.

3,204 views
Skip to first unread message

John Jeffery

unread,
Apr 2, 2013, 6:20:47 AM4/2/13
to golan...@googlegroups.com
I was wondering if anyone can explain why the Go flag package uses a different convention to the traditional Unix command line flags?

I don't suppose there is anything really wrong with the Go convention, but at first blush it seems to be different for no particularly good reason. Perhaps there is a story behind its design?

Cheers -- John

Jan Mercl

unread,
Apr 2, 2013, 6:32:27 AM4/2/13
to John Jeffery, golang-nuts
On Tue, Apr 2, 2013 at 12:20 PM, John Jeffery <jjef...@sp.com.au> wrote:
> I was wondering if anyone can explain why the Go flag package uses a
> different convention to the traditional Unix command line flags?

According to eg. [1] it seems like the opposite is actually the case.

> Traditionally, UNIX command-line options consist of a dash, followed by one or more lowercase letters.
> The GNU utilities added a double-dash, followed by a complete word or compound word.

-j

[1]: http://tldp.org/LDP/abs/html/standard-options.html

Dan Kortschak

unread,
Apr 2, 2013, 6:53:18 AM4/2/13
to Jan Mercl, John Jeffery, golang-nuts
The single dash followed by one or more lowercase letters is not the
same as Go flag use, which is a single dash followed by a word (which
may be one or more letters long).

I remember Rob posting on this topic about a year ago, but don't
remember thread or link. My recollection of what he said about it was
that there are too many difference ways of doing things with the GNU
option system (yes, this doesn't really the OP).

Jan Mercl

unread,
Apr 2, 2013, 6:57:54 AM4/2/13
to Dan Kortschak, John Jeffery, golang-nuts
On Tue, Apr 2, 2013 at 12:53 PM, Dan Kortschak
<dan.ko...@adelaide.edu.au> wrote:
> The single dash followed by one or more lowercase letters is not the
> same as Go flag use, which is a single dash followed by a word (which
> may be one or more letters long).

You're right. I messed lexical grammar and semantic: -blah is option
'blah' in Go, but equivalent of -b -l -a -h elsewhere.

-j

roger peppe

unread,
Apr 2, 2013, 8:08:46 AM4/2/13
to John Jeffery, golang-nuts
If you want the GNU syntax, you can use launchpad.net/gnuflag which implements
an API that's almost identical to the standard flag package.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Rob Pike

unread,
Apr 2, 2013, 9:50:36 AM4/2/13
to roger peppe, John Jeffery, golang-nuts
As the author of the flag package, I can explain. It's loosely based on Google's flag package, although greatly simplified (and I mean greatly). I wanted a single, straightforward syntax for flags, nothing more, nothing less.

-rob

Kevin Gillette

unread,
Apr 2, 2013, 12:43:17 PM4/2/13
to golan...@googlegroups.com
It seems like a per package faq would be useful, stitched by godoc on demand into the online documentation from some well formed file residing in $GOROOT

John Jeffery

unread,
Apr 2, 2013, 5:58:41 PM4/2/13
to golan...@googlegroups.com
Thanks for the explanation Rob. I was curious as to the origins of the package.

Nate Finch

unread,
Apr 3, 2013, 8:23:38 AM4/3/13
to golan...@googlegroups.com
FWIW, I've had good luck using this package for GNU style flags: https://github.com/jessevdk/go-flags

It's very easy to set up your command line flags, and produces nicely formatted help text, with very little boilerplate necessary.

Here's an example of how I used it in gocog, a command line utility I wrote:

This is the struct that defines the command line API: https://github.com/natefinch/gocog/blob/master/processor/options.go#L3
This is how I use it to process flags: https://github.com/natefinch/gocog/blob/master/gocog.go#L26

You give the struct the defaults you want, call process on the command line, and then the struct has all the values... pass it around as necessary.
Reply all
Reply to author
Forward
0 new messages