Command line flags to maps

2,481 views
Skip to first unread message

Scott Turnbull

unread,
Aug 14, 2013, 12:42:09 PM8/14/13
to golan...@googlegroups.com
Hello,

I'm looking for a way to compile a go app that accepts pretended command line arguments that I can turn into maps

So I'd like to be able to type 'go mybinary -info:title My Title -info:author MyAuthor'

And have those flags in a map in my gocode info[title] etc.

I'd like to be able to set arbitray keys this way so a user could provide any argument with a flag '-info:<key> <value>'

Is there a way to do this in go?  I don't see anything in the flag library that would let me do this.

Thanks for any insight,

- Scott

Kyle Lemons

unread,
Aug 14, 2013, 1:12:09 PM8/14/13
to Scott Turnbull, golang-nuts
You could write such logic yourself, sure, but the flag library has no notion of such things.

os.Args has the command-line arguments if you wish to parse them yourself.


--
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.
 
 

Scott Turnbull

unread,
Aug 14, 2013, 2:22:48 PM8/14/13
to golan...@googlegroups.com, Scott Turnbull
This represents a bit of a problem for me because I'm using flags already and anytime I try to provide a undefined flag I get an error.  I think what I need is a method that would pull bag any undefined flags in the command line arguments and parse those as needed but I can't find anyway around flag.Parse returning an error "flag provided but not defined: " error.

Is there a way around this I'm missing?

Matthew Kane

unread,
Aug 14, 2013, 2:35:51 PM8/14/13
to Scott Turnbull, golang-nuts
I think you could switch to using a custom FlagSet without too much
change in your code. You can preprocess os.Args and pull your flags to
be mapped into your map without using the flag package. Something
like...

newargs := []string{}
for i := range os.Args[1:] {
if i[0:6] == "-info:" {
//add to map
} else {
newargs = append(newargs, os.Args[i])
}
}

yourflagset.Parse(newargs)
--
matt kane
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

lawle...@gmail.com

unread,
Aug 14, 2013, 11:47:43 PM8/14/13
to golan...@googlegroups.com
Scott,

The flag library stops parsing after it encounters the sequence "--" ( without quotes ).  You could arrange you code so that you manually parse everything after the -- sequence for your custom flags.

Please consider this program that I've named flagcustom.go:


I have specified two required parameters -a and -b.  If those command-line flags are present, I then look for any leftover arguments looking for "-info".  If I find "-info" I use args+1 as a key and args+2 as a value.

Here are some samples of the output of flagcustom:

flagcustom
  -a="": parameter a
  -b="": parameter b

flagcustom -a red -b green
a is red
b is green
map args are map[]

flagcustom -a red -b green -info sky blue
flag provided but not defined: -info
Usage of flagcustom:
  -a="": parameter a
  -b="": parameter b

flagcustom -a red -b green -- -info sky blue
a is red
b is green
map args are map[%!V(string=sky):%!V(string=blue)]

flagcustom -a red -b green -- -info sky blue -info 2b "or not to be"
a is red
b is green
map args are map[%!V(string=sky):%!V(string=blue) %!V(string=2b):%!V(string=or not to be)]

Note that the custom parsing in the for-loop is far from perfect.  It doesn't check to see if you have the correct number of arguments.  I also used my own format similar to yours.  You could examine each arg to see if the prefix is "-info:" and could then parse the key from the remainder.

roger peppe

unread,
Aug 15, 2013, 5:44:07 AM8/15/13
to Scott Turnbull, golang-nuts
On 14 August 2013 17:42, Scott Turnbull <stream...@gmail.com> wrote:
> Hello,
>
> I'm looking for a way to compile a go app that accepts pretended command
> line arguments that I can turn into maps
>
> So I'd like to be able to type 'go mybinary -info:title My Title
> -info:author MyAuthor'

This seems like a fairly unconventional way to use flags.
Couldn't you do something like this instead?

mybinary -info title='My Title' -info author=MyAuthor

That would be easy to implement using the flags package.
Reply all
Reply to author
Forward
0 new messages