0

I have a bunch of arguments that I want to pass to a command multiple times. To decrease the amount of times I write the same arguments, and thus decrease the number of places where I have to maintain those arguments, I figured it would be a good idea to put them all in a string and pass that string. However, the command sees the entire string as a single argument, which naturally causes problems.

Here's a script sample of what I'm tring to achieve. (This is for building GDAL, btw).

$baseOptions = "MSVC_VER=1910 GDAL_HOME='$outputDir\' SWIG='$swig'"

#Building x86
if($x86 -eq $true)
{
    $options = $baseOptions + " BINDIR='$outputDir\x86\bin' LIBDIR='$outputDir\x86\lib' INCDIR='$outputDir\x86\include'"
    if($useLibcurl -eq $true)
    {
        $options += " CURL_DIR='$libcurlDir/x86' CURL_INC='-I$libcurlDir/include' CURL_LIB='$libcurlDir/x86/lib/libcurl.lib wsock32.lib wldap32.lib winmm.lib'"
    }

    #Building x86 release
    if($release -eq $true)
    {
        nmake /f makefile.vc $options
        nmake /f makefile.vc install $options
        nmake /f makefile.vc devinstall $options
    }
    #Repeated for debug...
    #Some more use of $options to create C# wrappers...
}
#Repeated for x64...

Just to build the native part of GDAL for release in x86 I need to run nmake three times. I might not need all the same arguments for each run of nmake, but it's unclear what arguments are needed for which run and keeping track of that would just be an extra thing to maintain. Well, repeat this for debug and then both release and debug for x64 and then a few times to create C# wrappers.

If I can't modify a collection of arguments like this I would also have to duplicate the code for whether or not I want to build GDAL with a dependency on libcurl. The complexity would become massive if I have to add support for other dependent libraries as well.

I've tried to make the whole command into a string in order to expand $options in the following ways to no avail:

. "nmake /f makefile.vc $options"
& "nmake /f makefile.vc $options"

The entire string is of course seen as a command and not a command with arguments...

I've actually found the answer while writing this question, but I decided to ask anyway to help others who might be searching for a solution as frantically as I did. I'll still welcome answers which solve the same problem in a different way! Maybe ones that don't rely on the arguments being collected in a single string?

1 Answer 1

0

I found the solution in the Invoke-Expession command.

Invoke-Expression "nmake /f makefile.vc $options"

With this the string first expands $options and then Invoke-Expression reads the entire string as a command with arguments.

I still welcome other answers that solve the same problem in a different ways!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .