-
Notifications
You must be signed in to change notification settings - Fork 2
/
CmdLine.pas
242 lines (206 loc) · 6.32 KB
/
CmdLine.pas
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{*
CmdLine.pas - a class for parsing Delphi command line options.
-----------
Begin: 2005/05/27
Last revision: $Date: 2009-08-13 19:10:48 $ $Author: rhupalo $
Version number: $Revision: 1.9 $
Code Documentation Tags: Begin 2009-08-10, Last Revision: 2009-08-10
Project: APHI General Purpose Delphi Library
Website: http://www.naadsm.org/opensource/delphi
Author: Aaron Reeves <Aaron.Reeves@colostate.edu>
--------------------------------------------------
Copyright (C) 2005 - 2006 Animal Population Health Institute, Colorado State University
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Based on CCmdLine (a C++ command line parser) written by Chris Losinger.
Please see http://www.codeproject.com/cpp/ccmdline.asp for the original
code.
}
(*
Documentation generation tags begin with {* or ///
Replacing these with (* or // foils the documentation generator
*)
unit CmdLine;
interface
uses
QLists,
QStringMaps
;
type
/// Structures to hold command line parameters (switches ans switch arguments)
TCmdParam = class( TQStringList ) end;
type
/// Parses the command line parameters into a series of switches and switch arguments
TCmdLine = class( TQStringObjectMap )
public
constructor create(); override;
destructor destroy(); override;
function splitLine(): integer;
function hasSwitch( pSwitch: string ): boolean;
function getSafeArgument( pSwitch: string; iIdx: integer; pDefault: string ): string;
function getArgument( pSwitch: string; iIdx: integer ): string;
function getArgumentCount( pSwitch: string ): integer;
protected
function isSwitch( const pParam: string ): boolean;
end
;
implementation
uses
MyStrUtils,
SysUtils
;
/// Parses the command line parameters into a series of switches and switch arguments
constructor TCmdLine.create();
var
curParam: string;
arg: string;
i: integer;
cmd: TCmdParam;
begin
inherited create();
clear();
// Delphi automatically skips the EXE name, so start with the first argument
for i := 0 to ParamCount do //ParamCount is a Delphi function
begin
// If it's a switch, start a new TCmdParam
if( isSwitch( paramStr(i) ) ) then //paramStr is a Delphi function
begin
curParam := paramStr(i);
cmd := TCmdParam.Create();
// Add the TCmdParam to 'self'
self.add( curParam, cmd );
end
else
begin
// it's not a new switch, so it must an argument for the last switch
// ... let's add it
arg := paramStr(i);
cmd := self.value( curParam ) as TCmdParam;
if( nil <> cmd ) then
begin
//dbcout( 'SWITCH ' + curParam + ' arg: ' + arg );
cmd.Append( arg );
end
;
end
;
end
;
end
;
/// Memory cleanup
destructor TCmdLine.destroy();
begin
self.deleteValues();
inherited destroy();
end
;
{*
Counts the number of switches in the commandline
@return Count of switches in the command line
}
function TCmdLine.splitLine(): integer;
begin
result := count;
end
;
{*
Checks if pSwitch is a valid argument (contains a switch)
@param pSwitch string holding the command line switch
@return True if pSwitch appears to be a command parameter
}
function TCmdLine.hasSwitch( pSwitch: string ): boolean;
var
cmd: TCmdParam;
begin
cmd := self.value( pSwitch ) as TCmdParam;
result := ( nil <> cmd );
end
;
{*
Provides a count of the number of arguments in pSwitch
@param pSwitch Command line switch
@return Count of arguments
}
function TCmdLine.getArgumentCount( pSwitch: string ): integer;
var
cmd: TCmdParam;
begin
cmd := self.value( pSwitch ) as TCmdParam;
if( nil = cmd ) then
result := -1
else
result := cmd.Count
;
end
;
{*
Returns the argument at position iIdx in the switch
@param pSwitch command line switch
@param iIdx argument index
@param pDefault Argument returned if requesting iIdx raises an exception
@return Argument text if successful or pDefault string on exception
}
function TCmdLine.getSafeArgument( pSwitch: string; iIdx: integer; pDefault: string ): string;
begin
try
result := getArgument( pSwitch, iIdx );
except
result := pDefault;
end;
end
;
{*
Returns the argument at position iIdx in the switch
@param pSwitch command line switch
@param iIdx argument index
@return Argument text if successful or empty string on exception
@throws Raises an exception if iIdx exceeds the number of arguments
}
function TCmdLine.getArgument( pSwitch: string; iIdx: integer ): string;
var
cmd: TCmdParam;
begin
result := '';
if( hasSwitch( pSwitch ) ) then
begin
cmd := self.value( pSwitch ) as TCmdParam;
if( cmd.Count > iIdx ) then
result := cmd.at( iIdx )
else
raise exception.Create( 'Commandline argument index out of bounds.' )
;
end
else
raise exception.Create( 'Commandline switch not found.' )
;
end
;
{*
Identify switches from negative number arguments, both begin with '-'
@param pParam command line parameter
@return true if the character following the '-' is not numeric
}
function TCmdLine.isSwitch( const pParam: string ): boolean;
begin
(* // Never used??
// switches must non-empty
// must have at least one character after the '-'
if( length( pParam ) <= 1 ) then
result := false
;
*)
// switches always start with '-'
if( charAt( pParam, 0 ) = '-' ) then
begin
// allow negative numbers as arguments.
// ie., don't count them as switches
result := not( isDigit( charAt( pParam, 1 ) ) );
end
else
result := false
;
end
;
end.