flags package: shared flags and flag sets

188 views
Skip to first unread message

Chris Burkert

unread,
May 6, 2020, 8:08:25 AM5/6/20
to golang-nuts
Dear all,

I'd like to mix shared flags with flags specific to flag sets (cooltool <globalFlags> <subcommand> <commandFlags>). However I struggle to parse the shared flags only and pass the rest to the flagset for parsing. Here is what I came up with:

https://play.golang.org/p/Jazn3aSX9-d

Do I have to pick a different flag library or can this be done with package flag from the standard library?

Thanks - Chris

Tamás Gulácsi

unread,
May 6, 2020, 10:27:27 AM5/6/20
to golang-nuts

mural

unread,
May 7, 2020, 1:29:26 AM5/7/20
to golang-nuts

You can then use it like './mycli -d stream -rt'

The '-d' option is a shared flag, and `-rt' is dedicated for the subcommand 'stream'.

Chris Burkert

unread,
May 7, 2020, 5:26:29 AM5/7/20
to mural, golang-nuts
Thank you very much.
This is exactly what I was looking for. A simple way without the need for additional packages.
Highly appreciated!

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1c81e2be-0920-4872-887e-19981a0543d1%40googlegroups.com.

Manlio Perillo

unread,
May 7, 2020, 12:07:51 PM5/7/20
to golang-nuts
Not sure if it can help, but look at:

It is adapted from cmd/go subcommand code and use the standard flag package.


Manlio 
Reply all
Reply to author
Forward
0 new messages