golang to parse command line flags/switches/arguments

2,083 views
Skip to first unread message

Tong Sun

unread,
Sep 22, 2013, 3:48:35 PM9/22/13
to golan...@googlegroups.com
Hi, 

I'm trying to learn the flag package that implements command-line flag parsing, because its doc is not too clear to me. Here are my questions:

1. The two examples I found, 


both contain a function "usage()", but there is no explicit calling of function "usage()" in the code (only a hint that it will somehow get called), nor the function "usage()" is in the flag package doc. 

2. I'm trying to compare it with the traditional *nix getopt. And seems that getopt will rearrange command line arguments so that switches comes first, while the flag package will not, correct? Also, with getopt, long options may be  abbreviated, as long as the abbreviation is not ambiguous. Is it true for the flag package's behavior?

3. Seems that there is a hidden/undocumented flag -help (http://blog.brandonc.me/2013/04/golang-flag-help-h.html). Why is it undocumented?

Now you can see why I'm scratching my head for this simple package. Again, I'm trying to learn the flag package by example, so could you give me a short example that, 

- have one mandatory switch and an optional one
- will prompt the usage when 
 1. no command line arguments
 2. the mandatory switch is missing
- will prompt the usage when wrong switches are provided
- also throw in handling the additional non-flag arguments (via flag.Args()) please

TIA


Volker Dobler

unread,
Sep 22, 2013, 4:23:21 PM9/22/13
to golan...@googlegroups.com
Am Sonntag, 22. September 2013 21:48:35 UTC+2 schrieb Tong Sun:
1. The two examples I found, 
[...]
both contain a function "usage()", but there is no explicit calling of function "usage()" in the code (only a hint that it will somehow get called), nor the function "usage()" is in the flag package doc. 
 
2. I'm trying to compare it with the traditional *nix getopt. And seems that getopt will rearrange command line arguments so that switches comes first, while the flag package will not, correct? Also, with getopt, long options may be  abbreviated, as long as the abbreviation is not ambiguous. Is it true for the flag package's behavior?

No, you may not abbreviate anything here.
 
3. Seems that there is a hidden/undocumented flag -help (http://blog.brandonc.me/2013/04/golang-flag-help-h.html). Why is it undocumented?

 
Now you can see why I'm scratching my head for this simple package. Again, I'm trying to learn the flag package by example, so could you give me a short example that, 

The example here http://golang.org/pkg/flag/#example_ show what you can do. 
 
- have one mandatory switch and an optional one
There are no mandatory flags in Go.
 
- will prompt the usage when 
 1. no command line arguments
 2. the mandatory switch is missing
Package flag cannot be parametrized to do so: You must write some code yourself.
 
- will prompt the usage when wrong switches are provided
That is the default.
 
- also throw in handling the additional non-flag arguments (via flag.Args()) please
Cannot be done without code. 

V. 

Tong Sun

unread,
Sep 22, 2013, 4:40:56 PM9/22/13
to Volker Dobler, golang-nuts
On Sun, Sep 22, 2013 at 4:23 PM, Volker Dobler <dr.volke...@gmail.com> wrote:
1. The two examples I found, 
[...]
both contain a function "usage()", but there is no explicit calling of function "usage()" in the code (only a hint that it will somehow get called), nor the function "usage()" is in the flag package doc. 


I'm too new to GO to understand what you want me to see. Are you saying that there is a hidden link between var Usage and function "usage()"?
 
3. Seems that there is a hidden/undocumented flag -help (http://blog.brandonc.me/2013/04/golang-flag-help-h.html). Why is it undocumented?


Again, I'm too new to GO to understand what you want me to see. I've read them before I posted my questions. 

Thanks for your help though. 

Tong Sun

unread,
Sep 23, 2013, 11:20:25 AM9/23/13
to Volker Dobler, golang-nuts
On Sun, Sep 22, 2013 at 4:23 PM, Volker Dobler <dr.volke...@gmail.com> wrote:
Am Sonntag, 22. September 2013 21:48:35 UTC+2 schrieb Tong Sun:
1. The two examples I found, 
[...]
both contain a function "usage()", but there is no explicit calling of function "usage()" in the code (only a hint that it will somehow get called), nor the function "usage()" is in the flag package doc. 

Most probably this usage gets called somehow manually in the samples you found.

No, I double checked again, 


As I said, there is no explicit calling of function "usage()" in the code. 

Matt Harden

unread,
Sep 23, 2013, 12:57:03 PM9/23/13
to golan...@googlegroups.com, Volker Dobler
The first example does call usage(). I haven't checked the second.

Ian Lance Taylor

unread,
Sep 23, 2013, 1:22:49 PM9/23/13
to Tong Sun, golang-nuts
On Sun, Sep 22, 2013 at 12:48 PM, Tong Sun <sunto...@gmail.com> wrote:
>
> 2. I'm trying to compare it with the traditional *nix getopt. And seems that
> getopt will rearrange command line arguments so that switches comes first,
> while the flag package will not, correct? Also, with getopt, long options
> may be abbreviated, as long as the abbreviation is not ambiguous. Is it
> true for the flag package's behavior?

For what it's worth, the behaviour you are describing is not the
traditional *nix getopt. It's GNU getopt. It was GNU getopt that
introduced rearranging flags to the front and permitting
abbreviations. The Go flag package is closer to the traditional
getopt.

Ian

Gustavo Niemeyer

unread,
Sep 23, 2013, 2:10:59 PM9/23/13
to Tong Sun, golan...@googlegroups.com
FWIW, Roger Peppe published a fork of the standard flag package a
while ago to behave more like the GNU flag parsing, so juju would stay
backwards compatible:

http://godoc.org/launchpad.net/gnuflag
> --
> 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.



--

gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages