Obtaining select command line parameters

From NSIS Wiki

Author: comperio (talk, contrib)


The Script

/*
	This function will search for the requested command-line option
	and return the value.  If the value is not found, the function 
	returns an empty string.
[USAGE]
	${CMDPAR} [TAG] [Variable for result] 
		[Tag] is a unique string to identify the parameter (ie "/B=", etc.)
		[Variable for result] is the variable in which to hold the result
[RULES]
-	Each parameter value must start with the character indicated by PARAM_CHAR.
-	This function will not verify any parameter--this will be up to the script
        developer!
-	Parameters CANNOT be "nested" (a parameter within a parameter)
-	The function takes advantage of the StrCmp function, which is 
	NOT case-sensative.  (In other works "/l" and "/L" will be the same)
-	This function assumes that $CMDLINE will always have quotes around the actual
	SETUP.EXE file (example: "C:\my path\setup.exe" /b=parameter)
-	The return value will be trimmed automatically (no spaces at either end)	
[VARIABLES]
$0 (str):	string indicating the what the format of the command should be (example: "/B")
$1 (int): 	Pointer value indicating the current position in the search string
$2 (str): 	string value of the parameter portion of the command line
$3 (str): 	Final value of the parameter (When $3=$0, stop copying the 
                parameter to $R1)
$4 (bln): 	A 1 or 0 value indicating when to start/stop adding characters
                to the parameter result (1 means start, 0 means stop)
$5 (int):	Total Length of the $CMDLINE variable, including the parameter part
$6 (int):	Single-character string of each character in the parameter field
$R1 (str):	Value of the requested parameter value
$7 (int):	Temporary variable for holding numbers
*/
!macro CMDPAR Tag OutVar
	Push ${TAG}
	Call GetParameters
	Pop ${OutVar}
!macroend
 
!define CMDPAR "!insertmacro CMDPAR"
 
Function GetParameters
	; all parameters must start with PARAM_CHAR.  (Change value as needed.)
	!define PARAM_CHAR "/"	
	Exch $0 ; $0 now contains the TAG string
	Push $R1
	Push $1
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	StrCpy $1 0	; Initialize the pointer variable
	StrLen $5 $CMDLINE
	FindParam:	; Start loop to find the parameter portion of the $CMDLINE
		IntOp $1 $1 + 1
		StrCpy $6 $CMDLINE 1 $1
		StrCmp $6 '"' ExitFindParam 0	; Exit the loop when the last quotes are found
		Goto FindParam
	ExitFindParam:
	IntOp $1 $1 + 2	;  Increment pointer one space to move it one character past the quotes
	IntOp $7 $5 - $1	; Difference between total string length and the length of the parameter portion
	IntCmp $7 0 ParamDone ParamDone 0	;If this value is zero, then no parameters have been defined (exit)
	;MessageBox MB_YESNO|MB_ICONQUESTION "Difference between total string length and the length of the parameter portion:  $7$\n$\r$\n$\rContinue?" IDNO ParamDone
	StrCpy $2 $CMDLINE $7 $1
	;MessageBox MB_OK "Parameter portion: [$2]"
	StrCpy $2 "X$2" ;	 Add one character to the start of the parameter string
	StrCpy $1 0	; Reset the pointer
	StrCpy $4 0	; keep characters from being copied to $R1 until we've found a match to the parameter tag
	FindParamValue:	;Start loop to find the requested parameter value
		IntOp $1 $1 + 1
		strCpy $6 $2 1 $1
		;MessageBox MB_YESNO|MB_ICONQUESTION "Character value: [$6]$\r$\n$$PARAM_CHAR: [${PARAM_CHAR}]$\r$\nContinue?" IDNO PARAMDONE
		StrCmp $6 "" ParamDone 0 ;Exit if this is the end of the parameter string
		StrCmp $6 ${PARAM_CHAR} Reset4 NoReset4	; Reset $4 if this is a new parameter
		Reset4:
		;StrCpy $R1 ""
		StrCpy $4 0
		StrCpy $3 ""
		NoReset4:
		StrCpy $3 "$3$6"
		IntCmp $4 0 NoBuildR1 BuildR1
		BuildR1:
		StrCpy $R1 "$R1$6"
		NoBuildR1:
		;MessageBox MB_YESNO|MB_ICONQUESTION "$$3: [$3]$\r$\n$$0: [$0]$\r$\n$\r$\nContinue?" IDNO PARAMDONE
		StrCmp $3 $0 0 FindParamValue
		StrCpy $4 1
 
	Goto FindParamValue
 
	ParamDone:
	;	This last part of the script will trim the spaces off either end of $R1:
	StrCpy $2 "X$R1"
	strCpy $R1 ""
	StrCpy $1 0
	KeepTrimming:
		IntOp $1 $1 + 1
		strCpy $6 $2 1 $1
		StrCmp $6 "" DoneTrimming 0
		strCmp $6 " " NoCopyCharR1 CopyCharR1
		copyCharR1:
		StrCpy $R1 "$R1$6"
		Goto KeepTrimming
		NoCopyCharR1:
		Goto KeepTrimming
	DoneTrimming:
 
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
	Push $R1
	!undef PARAM_CHAR
FunctionEnd
Personal tools
donate