fhcat
fhcat

Reputation: 1243

Golang flags takes any arbitrary arguments

I have a program which wraps another executable. I need to provide some interface so that we my program is called, user can provide any arbitrary arguments which will be passed on to the executable that it wraps. For example:

When user calls

$ myprogram --call -additional -a -b -c 1 -d=true

I would like my program to call

wrapped_executable -a -b -c 1 -d=true

What is the best way to achieve this using flags package?

Upvotes: 0

Views: 585

Answers (1)

colm.anseo
colm.anseo

Reputation: 22147

From the flag docs docs:

Flag parsing stops just before the first non-flag argument ("-" is a non-flag argument) or after the terminator "--".

so when invoking your outer exe, replace -additional with --:

$ myprogram --call --  -a -b -c 1 -d=true

and then to get the arguments after the --:

flag.Parse()
args := flag.Args() // []string{"-a", "-b", "-c", "1", "-d=true"}

Upvotes: 1

Related Questions