-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgramArgs.bas
113 lines (83 loc) · 4 KB
/
ProgramArgs.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'-----------------------------------------------------------------------------------------------------------------------
' Program arguments parsing library
' Copyright (c) 2024 Samuel Gomes
'-----------------------------------------------------------------------------------------------------------------------
$INCLUDEONCE
'$INCLUDE:'Common.bi'
'$INCLUDE:'Types.bi'
'-----------------------------------------------------------------------------------------------------------------------
' TEST CODE
'-----------------------------------------------------------------------------------------------------------------------
'$DEBUG
'PRINT "Program executable path name is:"
'PRINT GetProgramExecutablePathName
'PRINT
'DIM AS LONG argName, argIndex: argIndex = 1 ' start with the first argument
'DO
' argName = GetProgramArgument("whbsx", argIndex)
' SELECT CASE argName
' CASE -1
' EXIT DO
' CASE KEY_LOWER_W
' argIndex = argIndex + 1 ' value at next index
' PRINT "width = "; COMMAND$(argIndex)
' CASE KEY_LOWER_H
' argIndex = argIndex + 1 ' value at next index
' PRINT "height = "; COMMAND$(argIndex)
' CASE KEY_LOWER_B
' argIndex = argIndex + 1 ' value at next index
' PRINT "bpp = "; COMMAND$(argIndex)
' CASE KEY_LOWER_S
' PRINT "Silent operation"
' CASE KEY_LOWER_X
' PRINT "Secret x argument found!"
' CASE ELSE
' PRINT "Handle "; COMMAND$(argIndex)
' END SELECT
' argIndex = argIndex + 1 ' move to the next index
'LOOP UNTIL argName = -1
'argIndex = GetProgramArgumentIndex(KEY_LOWER_X)
'IF argIndex > 0 THEN
' PRINT "Secret x argument found!"
'END IF
'END
'-------------------------------------------------------------------------------------------------------------------
' This works like a really simple version of getopt
' arguments is a string containing a list of valid arguments (e.g. "gensda") where each character is an argument name
' argumentIndex is the index where the function should check
' Returns the ASCII value of the argument name found at index. 0 if something else was found. -1 if end of list was reached
FUNCTION GetProgramArgument% (arguments AS STRING, argumentIndex AS LONG)
DIM currentArgument AS STRING, argument AS _UNSIGNED _BYTE
IF argumentIndex > _COMMANDCOUNT THEN ' we've reached the end
GetProgramArgument = -1 ' signal end of arguments
EXIT FUNCTION
END IF
currentArgument = COMMAND$(argumentIndex) ' get the argument at index
IF LEN(currentArgument) = 2 THEN ' proceed only if we have 2 characters at index
argument = ASC(currentArgument, 2)
IF (KEY_SLASH = ASC(currentArgument, 1) OR KEY_MINUS = ASC(currentArgument, 1)) AND NOT _FILEEXISTS(currentArgument) AND NOT _DIREXISTS(currentArgument) AND INSTR(arguments, CHR$(argument)) > 0 THEN
GetProgramArgument = argument ' return the argument name
EXIT FUNCTION ' avoid "unknown" path below
END IF
END IF
GetProgramArgument = NULL ' signal we have something unknown
END FUNCTION
' Checks if a parameter is present in the command line
' Returns the position of the argument or -1 if it was not found
FUNCTION GetProgramArgumentIndex& (argument AS _UNSIGNED _BYTE)
DIM i AS LONG, currentArgument AS STRING
FOR i = 1 TO _COMMANDCOUNT
currentArgument = COMMAND$(i)
IF LEN(currentArgument) = 2 THEN ' proceed only if we have 2 characters at index
IF (KEY_SLASH = ASC(currentArgument, 1) OR KEY_MINUS = ASC(currentArgument, 1)) AND NOT _FILEEXISTS(currentArgument) AND NOT _DIREXISTS(currentArgument) AND ASC(currentArgument, 2) = argument THEN
GetProgramArgumentIndex = i
EXIT FUNCTION
END IF
END IF
NEXT
GetProgramArgumentIndex = -1 ' return invalid index
END FUNCTION
' Returns the running executable's path name
FUNCTION GetProgramExecutablePathName$
GetProgramExecutablePathName = COMMAND$(NULL)
END FUNCTION