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?
- The
flag
-package is order-agnostic. It cannot help you with this unless you put-a -b -c 1 -d=true
in quotes.Zyl– Zyl2021-10-14 13:51:05 +00:00CommentedOct 14, 2021 at 13:51 - This is pretty easy if you run it using the shell correctly as
myprogram -call -additional "-a -b -c 1 -d=true"
. Shells break up CLI arguments on spaces.Adrian– Adrian2021-10-14 13:51:16 +00:00CommentedOct 14, 2021 at 13:51
1 Answer1
From theflag docs docs:
Flag parsing stops just before the first non-flag argument ("-" is anon-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"}
Comments
Explore related questions
See similar questions with these tags.