-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgumentsHelper.pbi
214 lines (159 loc) · 5.64 KB
/
ArgumentsHelper.pbi
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;{- Code Header
; ==- Basic Info -================================
; Name: ArgumentsHelper.pbi
; Version: 0.0.6
; Author: Herwin Bozet
;
; ==- Compatibility -=============================
; Compiler version: PureBasic 5.70 (x86/x64)
; Operating system: Windows 10 21H1 (Previous versions untested)
;
; ==- Links & License -===========================
; License: Unlicense
; GitHub: https://github.com/aziascreations/PB-Arguments
;}
;- DocMaker Statements
;@chapter Arguments Helper
;@ Intro text...<br>
;@ <b>Module Namespace</b>: ArgumentsHelper
;- Notes
; The main goal of this include is to add a layer of abstraction between the "Arguments" module and the application.
; by providing wrappers and more complex procedures that handle the complex, boring and repetitive tasks.
;- Compiler Directives
EnableExplicit
XIncludeFile "./Arguments.pbi"
;- Module declaration
DeclareModule ArgumentsHelper
;-> Semver Data
#Version_Major = 0
#Version_Minor = 0
#Version_Patch = 6
#Version_Label$ = ""
#Version$ = "0.0.6"
;-> Procedure Declaration
Declare.i RegisterVerb(Verb.s, Description.s = #Null$, *ParentVerb.Arguments::Verb = #Null)
Declare.i RegisterOption(Token.c, Name.s, Description.s = #Null$, Flags.i = 0, *ParentVerb.Arguments::Verb = #Null)
Declare.b IsVerbUsed(*Verb.Arguments::Verb = #Null)
Declare.b IsOptionUsed(*Option.Arguments::Option)
Declare.b SearchOptionsIfUsed(Token.c = #Null, Name.s = #Null$, *ParentVerb.Arguments::Verb = #Null)
; Small wrappers for the optional verb argument.
; This check is kept outside of the main procedure for performance reasons.
Declare.i GetDefaultOption(*Verb.Arguments::Verb = #Null)
Declare.i GetOptionByToken(DesiredToken.c, *Verb.Arguments::Verb = #Null)
Declare.i GetOptionByName(OptionName.s, *Verb.Arguments::Verb = #Null)
Declare.i GetVerbByName(VerbName.s, *ContainerVerb.Arguments::Verb = #Null)
Declare.s GetOptionValueByIndex(*Option.Arguments::Option, ValueIndex.i = 0)
Declare.i GetOptionValues(*Option.Arguments::Option, List Values())
Declare.i GetOptionValueCount(*Option.Arguments::Option)
;-> Macros
Macro Init() : Arguments::Init() : EndMacro
Macro Finish() : Arguments::Finish() : EndMacro
Macro GetParentVerb(ParentVerb) : Arguments::GetParentVerb(ParentVerb) : EndMacro
Macro ParseAllArguments() : Arguments::ParseArguments() : EndMacro
EndDeclareModule
;- Module
Module ArgumentsHelper
;-> Compiler Directives
EnableExplicit
;-> Procedure Definition
Procedure.i RegisterVerb(Verb.s, Description.s = #Null$, *ParentVerb.Arguments::Verb = #Null)
Protected NewVerb = Arguments::CreateVerb(Verb, Description)
If NewVerb
If Arguments::RegisterVerb(NewVerb, *ParentVerb)
ProcedureReturn NewVerb
Else
Arguments::FreeVerb(NewVerb)
EndIf
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.i RegisterOption(Token.c, Name.s, Description.s = #Null$, Flags.i = 0, *ParentVerb.Arguments::Verb = #Null)
Protected Option = Arguments::CreateOption(Token, Name, Description, Flags)
If Option
If Arguments::RegisterOption(Option, *ParentVerb)
ProcedureReturn Option
Else
Arguments::FreeOption(Option)
EndIf
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.b IsVerbUsed(*Verb.Arguments::Verb = #Null)
If Not *Verb
*Verb = Arguments::*RootVerb
EndIf
ProcedureReturn *Verb\WasUsed
EndProcedure
Procedure.b IsOptionUsed(*Option.Arguments::Option)
If *Option
ProcedureReturn *Option\WasUsed
EndIf
ProcedureReturn #False
EndProcedure
Procedure.b SearchOptionsIfUsed(Token.c = #Null, Name.s = #Null$, *ParentVerb.Arguments::Verb = #Null)
If *ParentVerb = #Null
*ParentVerb = Arguments::GetRootVerb()
EndIf
If *ParentVerb
ForEach *ParentVerb\Options()
If *ParentVerb\Options()\Token <> #Null And Token <> #Null
Debug *ParentVerb\Options()\Token
If *ParentVerb\Options()\Token = Token
ProcedureReturn *ParentVerb\Options()\WasUsed
EndIf
EndIf
If *ParentVerb\Options()\Name <> #Null$ And Name <> #Null$
Debug *ParentVerb\Options()\Name
If *ParentVerb\Options()\Name = Name
ProcedureReturn *ParentVerb\Options()\WasUsed
EndIf
EndIf
Next
EndIf
ProcedureReturn #False
EndProcedure
Procedure.i GetDefaultOption(*Verb.Arguments::Verb = #Null)
If Not *Verb
*Verb = Arguments::*RootVerb
EndIf
ProcedureReturn Arguments::GetDefaultOption(*Verb)
EndProcedure
Procedure.i GetOptionByToken(DesiredToken.c, *Verb.Arguments::Verb = #Null)
If Not *Verb
*Verb = Arguments::*RootVerb
EndIf
ProcedureReturn Arguments::GetOptionByToken(*Verb, DesiredToken)
EndProcedure
Procedure.i GetOptionByName(OptionName.s, *Verb.Arguments::Verb = #Null)
If Not *Verb
*Verb = Arguments::*RootVerb
EndIf
ProcedureReturn Arguments::GetOptionByName(*Verb, OptionName)
EndProcedure
Procedure.i GetVerbByName(VerbName.s, *ContainerVerb.Arguments::Verb = #Null)
If Not *ContainerVerb
*ContainerVerb = Arguments::*RootVerb
EndIf
ProcedureReturn Arguments::GetVerbByName(*ContainerVerb, VerbName)
EndProcedure
Procedure.s GetOptionValueByIndex(*Option.Arguments::Option, ValueIndex.i = 0)
If *Option
If SelectElement(*Option\Arguments(), ValueIndex)
ProcedureReturn *Option\Arguments()
EndIf
EndIf
ProcedureReturn #Null$
EndProcedure
Procedure.i GetOptionValues(*Option.Arguments::Option, List Values())
If *Option
CopyList(*Option\Arguments(), Values())
EndIf
ProcedureReturn
EndProcedure
Procedure.i GetOptionValueCount(*Option.Arguments::Option)
If *Option
ProcedureReturn ListSize(*Option\Arguments())
EndIf
ProcedureReturn 0
EndProcedure
EndModule