Parsing Command Line Switches
Posted: Sat Apr 05, 2003 7:20 pm
Code updated For 5.20+
Restored from previous forum. Originally posted by ebs.
A number of my programs accept command line parameters of the form /r, /d10, /o=C:\Windows, or "C:\Program Files". Note that embedded spaces are OK if the parameter is enclosed in quotes, like the last example.
Here is a routine to parse command line parameters of the form /x or /x= (the equals sign is ignored, so these are the same), and one parameter of the form "this could be a path with spaces in it". If there is more than one parameter without a switch, only the first one will be returned.
It has only one procedure: CommandLineParameter(Switch.s). The first time it is called, it parses all command line parameters and stores them in a linked list. It then checks the linked list to see if there is a parameter for the specified switch letter. Parameters are returned as a string; use Val() if you need a numeric value. Note that switches are not case-sensitive; /r and /R are the same.
Try out this example using a command line like: /r=abc /sdef /t=123 "this is a file".
Restored from previous forum. Originally posted by ebs.
A number of my programs accept command line parameters of the form /r, /d10, /o=C:\Windows, or "C:\Program Files". Note that embedded spaces are OK if the parameter is enclosed in quotes, like the last example.
Here is a routine to parse command line parameters of the form /x or /x= (the equals sign is ignored, so these are the same), and one parameter of the form "this could be a path with spaces in it". If there is more than one parameter without a switch, only the first one will be returned.
It has only one procedure: CommandLineParameter(Switch.s). The first time it is called, it parses all command line parameters and stores them in a linked list. It then checks the linked list to see if there is a parameter for the specified switch letter. Parameters are returned as a string; use Val() if you need a numeric value. Note that switches are not case-sensitive; /r and /R are the same.
Try out this example using a command line like: /r=abc /sdef /t=123 "this is a file".
Code: Select all
Declare.s CommandLineParameter(Switch.s)
Structure CmdLineParam
Switch.s
Parameter.s
EndStructure
NewList CmdLineParams.CmdLineParam()
Debug CommandLineParameter("r")
Debug CommandLineParameter("s")
Debug Val(CommandLineParameter("t"))
Debug CommandLineParameter("")
; return command line parameter for specified switch letter
Procedure.s CommandLineParameter(Switch.s)
Shared CmdLineParsed.l
Shared CmdLineParams()
; parse command line on first invocation
If CmdLineParsed = #False
Repeat
; get next parameter
Param.s = ProgramParameter()
If Len(Param)
AddElement(CmdLineParams())
If Left(Param, 1) = "/"
; add switch to element
CmdLineParams()\Switch = UCase(Mid(Param, 2, 1))
; add parameter to element (ignore "=" if present)
Parameter.s = Mid(RemoveString(Param, "="), 3, 255)
If Len(Parameter)
CmdLineParams()\Parameter = Parameter
Else
; make parameter a space if switch only
CmdLineParams()\Parameter = " "
EndIf
Else
; one parameter without switch allowed (can contain spaces if in quotes)
CmdLineParams()\Switch = ""
CmdLineParams()\Parameter = Param
EndIf
EndIf
Until Len(Param) = 0
CmdLineParsed = #True
EndIf
; search for specified command line switch letter
ResetList(CmdLineParams())
UCSwitch.s = UCase(Switch)
While NextElement(CmdLineParams())
If CmdLineParams()\Switch = UCSwitch
; switch found - return parameter
ProcedureReturn CmdLineParams()\Parameter
EndIf
Wend
; switch not found
ProcedureReturn ""
EndProcedure