Archive: Did I reinvent the wheel?


Did I reinvent the wheel?
So I currently use the getparameters function to get the command line arguments. That's fine and dandy if I only have one argument, but if I have more I didn't know how to deal with it. Is there an easy way to split out the parameters? I didn't think there was, so I wrote the following function and then thought maybe that functionality already existed. Does it?


; GetNextParameter
; input, $parameters
; output, puts first parameter on stack and replaces $parameters with the beginning of the next param to the end of params
; modifies no other variables.

Function GetNextParameter

Push $R0 ;Push all var's we're using onto the stack so we can get them back later
Push $R1
Push $R2
Push $R3

StrCpy $R2 1 ;Put 1 in $R2 because we'll start at Char 1
StrLen $R3 $parameters ;Put the length of parameters in $R3 for use
StrCmp $R3 "0" noParams ;If parameters was passed empty, we don't have any parameters left

killLeadingSpaces:
;Check for space at beginning - if so, remove it
StrCpy $R0 $parameters $R2 ;Put the first character of the parameters into $R0
StrCmp $R0 ' ' 0 loop ;If it starts with a space, remove it, if not, continue
StrCpy $parameters $parameters "" 1 ;Remove the space from the beginning
StrLen $R3 $parameters ;New length
StrCmp $R3 "0" 0 killLeadingSpaces ;If it's down to length 0, there aren't any params
noParams:
StrCpy $R0 "NoParams" ;Let 'em know
Goto finished

loop:
IntOp $R2 $R2 + 1 ;Add one to where we're searching
StrCpy $R0 $parameters 1 $R2 ;Get that character so we can check it out
StrCmp $R2 $R3 gotit ;If it's the last param, the length and where we're at will match up
StrCmp $R0 ' ' gotit loop ;If we're on a space, we've got this one - finish up

gotit:
StrCpy $R0 $parameters $R2 ;Put this parameter in $R0
StrCpy $parameters $parameters "" $R2 ;reset parameters var to what's left
Goto finished

finished:
Pop $R3
Pop $R2
Pop $R1
Exch $R0

FunctionEnd


Thanks,
Eric

See http://nsis.sourceforge.net/Get_comm...ameter_by_name

This can be used to get parameters in the "/PARAM=VALUE" format.
You specify which parameter you want to get and you set the default.
If the parameter is not set, the default is returned...


Nice thanks! A little less error prone than mine...


What is wrong with GetOptions?

myfile.exe /arg=hello

${GetParameters} $0
${GetOptions} $0 `/arg=` $R0

$R0 = hello

It's in the NSIS doc under File functions header along with GetParameters. It also sets the error flag if the parameter is missing. For example you could use /arg without a value, in which case your code may be:

${GetParameters} $0
ClearErrors
${GetOptions} $0 `/arg` $R0
${Unless} ${Errors}
...
${EndUnless}

Stu


Looks even better. Guess the answer to the subject of this post is a resounding YES. I reinvented the wheel but didn't put enough spokes in it to support maintaining the original spherical shape...


I think there is a little syntax difference:

With ${GetOptions} you must pass it like this:
setup.exe /ARG1=C:\Program Files\My App /ARG2=C:\Program Files\My App 2

But with "GetParameterValue" you must pass it like this:
setup.exe /ARG1="C:\Program Files\My App" /ARG2="C:\Program Files\My App 2"


You have to use quotes no matter what method you use when the value contains spaces. Otherwise your /ARG1 would have a value of "C:\Program"

Stu


From the NSIS User Manual:

Example2:

Section
${GetOptions} "-INSTDIR=C:\Program Files\Common Files -SILENT=yes" "-INSTDIR=" $R0
;$R0=C:\Program Files\Common Files
SectionEnd
Either the example is wrong or you are mistaken, Afrow :confused:

My bad. GetOptions works with or without quotes.

Stu


Here is an example:
http://img411.imageshack.us/img411/6...optionsph0.png

So there is the difference between GetOptions and GetParameterValue.

The behavior of GetOptions is very unusual though :igor:


Say you have the following:

setup.exe doggie1 /ARG1="Jojo Likes Boxing"

Is there a way to grab doggie1 separate from Jojo Likes Boxing easily. In other words, I'm already grabbing one parameter with the getParameters function. It seems that if I continue to use the getParameters function with for doggie1, it will actually end up being 'doggie1 /ARG1="JojoLikesBoxing"' and I'll have to, instead, customize getParameters to only go to the first space. Am I making this harder than it really is?

Thanks,
Eric