[bb] Command line switches by Daz [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:43

Previous topic - Next topic

BlitzBot

Title : Command line switches
Author : Daz
Posted : 1+ years ago

Description : Shows to write a custom command line switch routine

Code :
Code (blitzbasic) Select
;Function which examines the command line allowing
;the use of switches to pass parameters into the
;program in a similar way DOS programs
;use parameters

;To test this program
;Select the Program -> Program Command Line menu option
;Type something into the text box such as
;-f filename.txt /u username -p password -d -z


Dim p$(256) ;Global variable used to hold all the parameters passed in from the command line

;Define a list of all the valid command line switches
Global valid_options$ = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ"

;Define the type structure used to store the processed switches and parameters
Type clopt
Field switch$
Field valid_char%
Field param$
End Type

;Generic Parameter handler function
Function HandleParameters%()
;Get the command line
cl$ = CommandLine()
If Len(Trim(cl$)) = 0 Then
Return -1
End If

;Get all the parameters into an array for easier processing
pn = 1
For counter = 1 To Len(cl$)
c$ = Mid$(cl$, counter, 1)
If c$ = " " Then
pn = pn + 1
Else
p$(pn) = p$(pn) + c$
End If
Next

;Work out the parameters
For counter = 1 To pn
;If the switch starts with a - or a /
param$ = ""
option$ = ""
valid = 0
If Left(p$(counter), 1) = "-" Or Left(p$(counter), 1) = "/" Then
;Make sure the swtich character is in the list of valid switches
option$ = Mid(p$(counter), 2, 1)
If Instr(valid_options$, option$, 1) <> 0 Then
;Check to see whether or not there is a parameter for the selected switch
If Left(p$(counter+1), 1) <> "-" And Left(p$(counter+1), 1) <> "/" Then
param$ = p$(counter+1)
counter = counter + 1
Else
param$ = ""
End If
valid = 1
Else

valid = 0
End If

;Add the switch and parameter data to a new entry in the options structure
o.clopt = New clopt
oswitch$ = option$
ovalid_char% = valid
oparam$ = param$
End If
Next

Return 0
End Function


;---------------------------
; Example of how to use the function

;Call the function
If HandleParameters() = -1 Then
Print "No parameters found on the command line"
Print ""
Else
Print ""
q_found = 0

;Loop through each switch found on the command line
For o.clopt = Each clopt
If ovalid_char% = 0 Then
Print oswitch$ + " is an invalid switch"
Else
Select oswitch$
Case "f"
Print "f switch found with a value of " + oparam$
Case "u"
Print "u switch found with a value of " + oparam$
Case "p"
Print "p swtich found with a value of " + oparam$
password$ = oparam$
Case "P"
Print "P swtich found with a value of " + oparam$
Case "q"
q_found = 1
Case "d"
Print "d swtich found with a value of " + oparam$
End Select
End If
Next

Print ""
Print ""

If q_found  = 0 Then
Print "The q switch is required"
End If
End If


Comments : none...